-
Notifications
You must be signed in to change notification settings - Fork 2
/
Frontier_List.cpp
193 lines (167 loc) · 6.58 KB
/
Frontier_List.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
//
// Created by Jonathan Gomes Selman on 8/6/17.
//
#include "Frontier_List.h"
Frontier_List::Frontier_List() {
// Create a dummy header for simplicity of traversal
// and for simplicity of adding and deleting values.
head = new Pareto_Solution(-1, {}, {}, {});
num_solutions = 0;
}
Frontier_List::~Frontier_List() {
destructor_helper();
}
void Frontier_List::destructor_helper() {
Pareto_Solution* curr = head;
// Free memory associated with each solution
while (curr != nullptr) {
Pareto_Solution* trash = curr;
curr = curr->next;
delete trash;
}
}
Frontier_List::Frontier_List(const Frontier_List &src) {
// We need to create a copy of each node
this->head = new Pareto_Solution(-1, {}, {}, {});
Pareto_Solution *copy = this->head;
for (Pareto_Solution *curr = src.get_head(); curr != nullptr; curr = curr->next) {
// Create a new copy of the node - utilize default copy for node as all the member variable have copy functions
copy->next = new Pareto_Solution(*curr);
copy = copy->next;
}
this->num_solutions = src.num_solutions;
}
Frontier_List &Frontier_List::operator=(const Frontier_List &src) {
// Generate new list
Pareto_Solution *tmpHead = new Pareto_Solution(-1, {}, {}, {});
Pareto_Solution *copy = tmpHead;
for (Pareto_Solution *curr = src.get_head(); curr != nullptr; curr = curr->next) {
copy->next = new Pareto_Solution(*curr);
copy = copy->next;
}
// Delete old list
destructor_helper();
// Set the head to the new list
this->head = tmpHead;
this->num_solutions = src.num_solutions;
return *this;
}
Pareto_Solution *Frontier_List::get_head() const{
// We want to skip over the dummy head
return this->head->next;
}
void Frontier_List::add_n_squared(const std::vector<std::pair<double, double> > &criteria, int nodeID,
const std::vector<int> &dam_decisions,
const std::vector<Pareto_Solution *> &pareto_decisions) {
Pareto_Solution *add_node = new Pareto_Solution(nodeID, criteria, dam_decisions, pareto_decisions);
Pareto_Solution *curr = this->head->next;
Pareto_Solution *prev = this->head;
while (curr != nullptr) {
int compare = add_node->is_dominant(curr);
if (compare == 2) { // They are the same so random decide which to keep --- May only want to do this when rounding!!!!!!
// Generate randomly distributed number
int rand_num = rand() % 2; // Only want to do if epsilon != 0 i.e. we are rounding!!!!
if (rand_num == 0) { // Remove the current and replace it with the new node
prev->next = add_node;
add_node->next = curr->next;
delete(curr);
} else {
delete(add_node);
}
return;
} else if (compare == 1) { // Dominates so remove
prev->next = curr->next;
num_solutions--;
delete(curr);
} else if (compare == -1) { // New is dominated so don't add
delete(add_node);
return;
} else { // Non-dominant so we move forward in list
prev = curr;
}
curr = curr->next;
}
// If we are here then its time to add
num_solutions++;
prev->next = add_node;
}
int Frontier_List::getNum_solutions() const {
return num_solutions;
}
void Frontier_List::add_n_squared(const std::pair<double, double> *criteria, int nodeID,
const std::vector<int> &dam_decisions,
const std::vector<Pareto_Solution *> &pareto_decisions, int num_solutions,
bool use_strict_compare) {
Pareto_Solution *add_node = new Pareto_Solution(nodeID, criteria, dam_decisions, pareto_decisions, num_solutions);
Pareto_Solution *curr = this->head->next;
Pareto_Solution *prev = this->head;
while (curr != nullptr) {
int compare;
if (use_strict_compare) {
compare = add_node->is_dominant_strong(curr);
} else {
compare = add_node->is_dominant_2(curr);
}
if (compare == 2) { // They are the same so random decide which to keep --- May only want to do this when rounding!!!!!!
int rand_num = rand() % 2; // Only want to do if epsilon != 0 i.e. we are rounding!!!!
if (rand_num == 0) { // Remove the current and replace it with the new node
prev->next = add_node;
add_node->next = curr->next;
delete(curr);
} else {
delete(add_node);
}
return;
} else if (compare == 1) { // Dominates so remove
prev->next = curr->next;
this->num_solutions--;
delete(curr);
} else if (compare == -1) { // New is dominated so don't add
delete(add_node);
return;
} else { // Non-dominant so we move forward in list
prev = curr;
}
curr = curr->next;
}
// If we are here then its time to add
this->num_solutions++;
prev->next = add_node;
}
void Frontier_List::add_n_squared_created(Pareto_Solution *add_node, bool strict_compare) {
Pareto_Solution *curr = this->head->next;
Pareto_Solution *prev = this->head;
while (curr != nullptr) {
int compare;
// Will fix later
if (strict_compare) {
compare = add_node->is_dominant_true(curr);
} else {
compare = add_node->is_dominant_true(curr);
}
if (compare == 2) { // They are the same so random decide which to keep --- May only want to do this when rounding!!!!!!
int rand_num = rand() % 2; // Only want to do if epsilon != 0 i.e. we are rounding!!!!
if (rand_num == 0) { // Remove the current and replace it with the new node
prev->next = add_node;
add_node->next = curr->next;
delete(curr);
} else {
delete(add_node);
}
return;
} else if (compare == 1) { // Dominates so remove
prev->next = curr->next;
this->num_solutions--;
delete(curr);
} else if (compare == -1) { // New is dominated so don't add
delete(add_node);
return;
} else { // Non-dominant so we move forward in list
prev = curr;
}
curr = curr->next;
}
// If we are here then its time to add
this->num_solutions++;
prev->next = add_node;
}