-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem.cpp
206 lines (172 loc) · 6.49 KB
/
Problem.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
#include "Problem.h"
using namespace std;
Problem::Problem(struct node bidsArray[], int numBids, int numCompanies, int numRegions) {
// get problem data and company size from bidsArray
this->problemData = new vector<struct node>[numCompanies];
this->maxBids = new int[numCompanies];
this->numCompanies = numCompanies;
this->numRegions = numRegions;
this->validStoreSize = 100;
//Add no bid node for each company
for (int i = 0; i < numCompanies; i++) {
struct node emptyNode;
emptyNode.cid = i;
emptyNode.price = 0;
emptyNode.norc = 0;
this->problemData[i].push_back(emptyNode);
}
//Arrange input data
for (int i = 0; i < numBids; i++) {
int cid = bidsArray[i].cid;
(this->problemData[cid]).push_back(bidsArray[i]);
}
//Initialize maxBids array for all companies
for (int i = 0; i < numCompanies; i++) {
this->maxBids[i] = (this->problemData[i]).size();
}
}
State Problem::getInitialState() {
return this->initialState;
}
int* Problem::getMaxBids() {
return this->maxBids;
}
vss Problem::getValidStore() {
return this->validStateStore;
}
void Problem::clearValidStore() {
this->validStateStore = vss();
}
double Problem::getStateCost(vector<int> bidNos) {
double totalCost = 0;
for (int i = 0; i < this->numCompanies; i++) {
int curBidNumber = bidNos[i];
double curcost = this->problemData[i][curBidNumber].price;
totalCost += curcost;
}
return (totalCost);// - 1000000*getStateConflicts(bidNos));
}
int Problem::getStateConflicts(vector<int> bidNos) {
vector<int> conflictArray(this->numRegions, 0);
int totalConflicts = 0;
for (int i = 0; i < this->numCompanies; i++) {
int curBidNumber = bidNos[i];
// cout<<"Current bid no.: "<<curBidNumber<<endl;
int *curRegions = this->problemData[i][curBidNumber].region;
int curRegionsSize = this->problemData[i][curBidNumber].norc;
// cout<<"Region size: "<<curRegionsSize<<endl;
for (int j = 0; j < curRegionsSize; j++) {
if (conflictArray[curRegions[j]] != 0) {
totalConflicts++;
}
else {
conflictArray[curRegions[j]] = 1;
}
}
}
// cout<<"Conflicts found: "<<totalConflicts<<endl;
return totalConflicts;
}
State Problem::getStateFromBidNumbers(vector<int> bidNos) {
// cout<<"Getting state"<<endl;
double totalCost = getStateCost(bidNos);
// cout<<"Cost: "<<to_string(totalCost)<<", ";
int totalConflicts = getStateConflicts(bidNos);
// cout<<"Conflicts: "<<totalConflicts<<endl;
return State(bidNos, totalCost, totalConflicts);
}
State Problem::generateRandomState() {
vector<int> bidNos;
for (int i = 0; i < this->numCompanies; i++) {
int r = (rand()) % (this->maxBids[i]);
bidNos.push_back(r);
}
State state = getStateFromBidNumbers(bidNos);
this->initialState = state;
return state;
}
void Problem::setInitialState(State initialState) {
this->initialState = initialState;
}
vector<State> Problem::getNeighbours(State currentState) {
vector<int> bidNumbers = currentState.getBidNumbers();
vector<State> neighbours;
// Push all adjacent neighbours to vector
// TODO: find way to do operations without using O(B*C) space
for (int i = 0; i < this->numCompanies; ++i) {
int currentBid = bidNumbers[i];
for (int j = 0; j < this->maxBids[i]; ++j) {
if (j != currentBid) {
vector<int> newBidNumbers(bidNumbers);
newBidNumbers[i] = j;
State neighbourState = getStateFromBidNumbers(newBidNumbers);
neighbours.push_back(neighbourState);
}
}
}
return neighbours;
}
bool compareStateDesc(State state1, State state2) {
return state1.getFitness() > state2.getFitness();
}
vector<State> Problem::localGreedyExpander(vector<State> fringe, int fringeSize) {
sort(fringe.begin(), fringe.end(), compareStateDesc);
// Resize fringe greedily
fringe.resize(fringeSize, generateRandomState()); // fill fringe with random states if size less than fringeSize
return fringe;
}
std::string Problem::getStringFromState(State state) {
string stateString = "{";
vector<int> bidNos = state.getBidNumbers();
for (int i = 0; i < bidNos.size(); ++i) {
int curBidNumber = bidNos[i];
if(curBidNumber>0){
string currString = "[";
int *curRegions = this->problemData[i][curBidNumber].region;
int curRegionsSize = this->problemData[i][curBidNumber].norc;
for (int j = 0; j < curRegionsSize; j++) {
currString.append(to_string(curRegions[j]) + " ");
}
currString.replace(currString.length()-1, 1, "]");
stateString.append("(").append(to_string(i)).append(",").append(" "+to_string(curBidNumber)+": ").append(currString).append(")");
}
}
stateString.append("}").append(to_string(getStateCost(state.getBidNumbers())));
return stateString;
}
vector<State> Problem::fringeExpander(vector<State> fringe, int fringeSize, int expanderCode) {
// Initialize new fringe with old fringe
vector<State> newfringe(fringe);
cout<<"Expanding fringe"<<endl;
// TODO: improve method to form new fringe without using enormous space each time
// Algorithm has much less running time for fringeSize=1 -> Re-implement
for (auto it = fringe.begin(); it < fringe.end(); it++) {
vector<State> neighbours = getNeighbours(*it);
newfringe.insert(newfringe.end(), neighbours.begin(), neighbours.end());
}
// Update validStateStore if valid state found among neighbours
for (auto it = newfringe.begin(); it < newfringe.end(); it++) {
State state = *it;
if (state.isValid()) {
validStateStore.push(state);
}
if (validStateStore.size() > validStoreSize) {
// validStateStore.pop(); //Question: Won't this remove the best states???????
}
}
// Carry out operation according to expanderCode
/*
* Codes for expander functions
* 0 : greedy expander
*/
vector<State> expandedfringe;
switch (expanderCode) {
case 0:
expandedfringe = localGreedyExpander(newfringe, fringeSize);
break;
default:
expandedfringe = localGreedyExpander(newfringe, fringeSize);
break;
}
return expandedfringe;
}