/
MmhphApp.cpp
253 lines (221 loc) · 7.53 KB
/
MmhphApp.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/******************************************************************
*
* File: BbaApp.cpp
* Author: Ken Zyma
*
* @All rights reserved
* Kutztown University, PA, U.S.A
*
* ACO MMKP Application.
*
*
*******************************************************************/
#include <iostream>
#include <stdlib.h> //atoi
#include <time.h>
#include <iomanip>
#include <random>
#include "MMKPDataSet.h"
#include "MMKPSolution.h"
#include "MMKP_TLBO.h"
#include "MMKP_COA.h"
#include "MMKP_GA.h"
#include "MMKPPopulationGenerators.h"
bool doesContain(std::vector<MMKPSolution> population,MMKPSolution sol);
int main(int argc, char* argv[]){
std::string folder = "orlib_data";
std::string file = "I01";
int problem = 1;
int popSize = 30;
int genSize = 40;
unsigned int seed = 1234;
std::string mods = "";
if(argc==7){
folder = argv[1];
file = argv[2];
problem = atoi(argv[3]);
mods = argv[4];
popSize = atoi(argv[5]);
genSize = atoi(argv[6]);
}else if(argc==8){
folder = argv[1];
file = argv[2];
problem = atoi(argv[3]);
mods = argv[4];
popSize = atoi(argv[5]);
genSize = atoi(argv[6]);
seed = atoi(argv[7]);
}else{
std::cout<<"usage: filename <folder><name><number><popSize><genSize>\n";
return 0;
}
std::ifstream fileStream;
MMKPDataSet dataSet;
clock_t t1,t2;
float runtime;
fileStream.open(folder+std::string("/")+file);
/* READ INPUT */
if(fileStream.is_open()){
//build MMKPDataSet object
if(folder=="orlib_data"){
OrLib_Read readInput;
dataSet = readInput(fileStream);
}else if(folder=="HiremathHill_data"){
HiremathHill_Read readInput;
dataSet = readInput(fileStream,problem);
}else{
std::cout<<"Error, unrecognized folder name."<<std::endl;
}
}else{
std::cerr<<std::string("File ")+file+std::string(" failed to open.")
<<std::endl;
}
fileStream.close();
//generate initial population
GenerateRandomizedPopulationNoDups generatePopulation;
std::vector<MMKPSolution> population
= generatePopulation(dataSet,popSize);
//algs used
MMKP_TLBO tlbo(dataSet);
MMKP_COA coa(dataSet);
MMKP_GA ga(dataSet);
tlbo.quickSort(population,0,population.size()-1);
MMKPSolution optimalSolution;
for(int i=0;i<population.size();i++){
if(dataSet.isFeasible(population[i])){
optimalSolution = population[i];
break;
}
}
MMKPSolution initialTeacher = optimalSolution;
t1=clock();
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 2);
int eliteSolutionSize = 5;
std::vector<MMKPSolution> eliteSolutions;
int count = 0;
for(int i=0;i<population.size();i++){
dataSet.updateSolution(population[i]);
if(dataSet.isFeasible(population[i])){
eliteSolutions.push_back(population[i]);
count++;
}
if(count > eliteSolutionSize){
break;
}
}
int convergenceGen = 0;
std::vector<std::tuple<int, float> > convData;
int functionEvalCounter = 0;
for(int i=0;i<genSize;i++){
//divide population by distribution
std::vector<MMKPSolution> pop1;
std::vector<MMKPSolution> pop2;
std::vector<MMKPSolution> pop3;
for(int j=0;j<population.size();j++){
int p = dis(gen);
switch(p){
case 0:
pop1.push_back(population[j]);
break;
case 1:
pop2.push_back(population[j]);
break;
case 2:
pop3.push_back(population[j]);
break;
}
}
//replace worse 3 solutions of each population with
//elite solutions
tlbo.quickSort(pop1,0,pop1.size()-1);
tlbo.quickSort(pop2,0,pop2.size()-1);
tlbo.quickSort(pop3,0,pop3.size()-1);
for(int j=0;j<eliteSolutions.size();j++){
pop1[((pop1.size()-1)-j)] = eliteSolutions[j];
pop2[((pop2.size()-1)-j)] = eliteSolutions[j];
pop3[((pop3.size()-1)-j)] = eliteSolutions[j];
}
pop1 = tlbo.runOneGeneration(pop1);
functionEvalCounter += pop1.size()*2;
pop2 = coa.runOneGeneration(pop2);
functionEvalCounter += pop2.size()*2;
pop3 = ga.runOneGeneration(pop3);
functionEvalCounter += pop3.size();
population.clear();
population.reserve(pop1.size() + pop2.size() + pop3.size());
population.insert(population.end(), pop1.begin(), pop1.end());
population.insert(population.end(), pop2.begin(), pop2.end());
population.insert(population.end(), pop3.begin(), pop3.end());
//get and save best solution
//and update our elite list
count = 0;
tlbo.quickSort(population,0,population.size()-1);
for(int j=0;j<population.size();j++){
dataSet.updateSolution(population[j]);
if(dataSet.isFeasible(population[j])){
if(population[j].getProfit()
> optimalSolution.getProfit()){
optimalSolution = population[j];
convergenceGen = i+1;
}
if(!(doesContain(eliteSolutions,population[j]))){
eliteSolutions[count] = population[j];
count++;
}
if(count > eliteSolutionSize){
break;
}
}
}
ReactiveLocalSearch RLS(dataSet);
//run local search on elite solutions
eliteSolutions = RLS(eliteSolutions);
//update conv. vector
std::tuple<int, float> temp(functionEvalCounter,optimalSolution.getProfit());
convData.push_back(temp);
}
t2 = clock();
runtime = ((float)t2-(float)t1)/(double) CLOCKS_PER_SEC;
std::cout<<"Problem: "<<std::endl;
std::cout<<folder<<std::string("/")<<file<<std::endl;
std::cout<<"Problem Number:"<<std::endl;
std::cout<<problem<<std::endl;
std::cout<<"Initial Profit:"<<std::endl;
std::cout<<initialTeacher.getProfit()<<std::endl;
std::cout<<"Profit:"<<std::endl;
if(dataSet.isFeasible(optimalSolution)){
std::cout<<optimalSolution.getProfit()<<std::endl;
}else{
std::cout<<0<<std::endl;
}
std::cout<<"Runtime:"<<std::endl;
std::cout<<runtime<<std::endl;
std::cout<<"Sol Found in _ Iterations"<<std::endl;
std::cout<<convergenceGen<<std::endl;
std::cout<<"Convergence Count:"<<std::endl;
std::cout<<convData.size()<<std::endl;
for(int i=0;i<convData.size();i++){
std::cout<<std::get<0>(convData[i])<<std::endl;
std::cout<<std::get<1>(convData[i])<<std::endl;
}
std::cout<<"Num of Classes:"<<std::endl;
std::cout<<optimalSolution.size()<<std::endl; //num of classes
for(int i=0;i<optimalSolution.size();i++){
for(int j=0;j<optimalSolution[i].size();j++){
std::cout<<optimalSolution[i][j]<<" ";
}
std::cout<<std::endl;
}
std::cout<<std::endl;
return 0;
}
bool doesContain(std::vector<MMKPSolution> population,MMKPSolution sol){
for(int i=0;i<population.size();i++){
if(population[i].getProfit() == sol.getProfit()){
return true;
}
}
return false;
}