-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
104 lines (82 loc) · 2.84 KB
/
main.cpp
File metadata and controls
104 lines (82 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "bits/stdc++.h"
#include "KnapsackProblem/KnapsackGA.h"
#include "CurveFittingProblem//CurveFittingGA.h"
#include "KnapsackProblem/KnapsackValidation.h"
using namespace std;
const int pop_sz = 100;
/*
* Function to get the inputFileSample.txt of the Knapsack problem
*/
void KnapsackGAInput(int test, double &AccuracyRatio) {
int numberOfItems, capacity;
vector<int> weight, benefit;
cin >> numberOfItems >> capacity;
weight.resize(numberOfItems), benefit.resize(numberOfItems);
for (int i = 0; i < numberOfItems; i++) {
cin >> weight[i] >> benefit[i];
}
KnapsackGA testCase(15000, pop_sz, numberOfItems, capacity, weight, benefit);
vector<int> ans = testCase.run().first;
cout << "Case #" << test << "\n";
int GASolution = 0;
for (int i = 0; i < ans.size(); ++i) {
if (ans[i])
GASolution += benefit[i];
}
int DpSolution = KnapsackValidation().getOptimalSolution(capacity, numberOfItems, weight, benefit); //get DP solution
cout << "DP Solution: " << DpSolution << " GA Solution: " << GASolution << "\n" << endl;
cout << "Taken Items:" << "\n";
for (int i = 0; i < ans.size(); ++i) {
if (ans[i])
cout << weight[i] << " " << benefit[i] << "\n";
}
cout << "\n";
AccuracyRatio += (GASolution / (double) DpSolution);
}
/*
* Function to get the inputFileSample.txt of the Curve Fitting problem
*/
void CurveFittingInput(int test) {
vector<Point> points;
int numberOfPoints, degree;
cin >> numberOfPoints >> degree;
for (int i = 0; i < numberOfPoints; ++i) {
double x, y;
cin >> x >> y;
points.emplace_back(x, y);
}
CurveFittingGA testCase(10000, pop_sz, degree + 1, points, numberOfPoints);
pair<vector<double>, double> x = testCase.run();
cout << "Case #" << test << "\n";
cout << "MSE = " << fixed << 1.00 / x.second << "\n";
cout << "Coefficients:" << "\n";
for (int i = 0; i < x.first.size(); ++i) {
cout << fixed << setprecision(5) << x.first[i] << "X^" << i << " " << "+\n"[i + 1 == x.first.size()]
<< " \n"[i + 1 == x.first.size()];
}
}
int main() {
freopen("inputFileSample.txt", "r", stdin);
freopen("outputFile.txt", "w", stdout);
srand(time(NULL));
cout << "Press 1 for Knapsack algorithm\nPress 2 for CurveFittingProblem algorithm" << "\n";
int selection;
cin >> selection;
int test, currTest = 1;
double accuracyRatio = 0;
cin >> test;
while (test--) {
if (selection == 1)
{
KnapsackGAInput(currTest, accuracyRatio);
if (test == 0) {
cout << "Accuracy: " << accuracyRatio / currTest * 100 << "\n";
}
} else if (selection == 2)
{
CurveFittingInput(currTest);
}
currTest++;
}
return 0;
}