-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalSearch.cpp
More file actions
236 lines (216 loc) · 9.55 KB
/
Copy pathlocalSearch.cpp
File metadata and controls
236 lines (216 loc) · 9.55 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
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
#include <iostream>
#include <cstdlib>
#include <vector>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/intersections.h>
#include <CGAL/convex_hull_2.h>
#include <CGAL/number_utils.h>
#include <CGAL/squared_distance_2.h>
#include "geoUtil.hpp"
#include "genericUtil.hpp"
#include "edgeChange.hpp"
#include "incremental.hpp"
#include "convexHull.hpp"
#include <CGAL/Kd_tree.h>
#include <CGAL/Search_traits_2.h>
#include <CGAL/Fuzzy_iso_box.h>
#include "timeManager.hpp"
using namespace std;
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Point_2<K> Point;
typedef CGAL::Segment_2<K> Segment;
typedef CGAL::Polygon_2<K> Polygon;
typedef K::Intersect_2 Intersect;
typedef Polygon::Vertex_iterator VertexIterator;
typedef Polygon::Edge_const_iterator EdgeIterator;
typedef CGAL::CartesianKernelFunctors::Intersect_2<K> Intersect;
typedef CGAL::Search_traits_2<K> Traits;
typedef CGAL::Kd_tree<Traits> Tree;
typedef CGAL::Fuzzy_iso_box<Traits> Fuzzy_box;
//typeOfOptimization=1: max area, typeOfOptimization=2: min area
int localSearch(Polygon* polygon, int typeOfOptimization, double threshold, int L, int* finalArea,int countPoints){
// change to clockwise
if(polygon->is_clockwise_oriented()==0){
polygon->reverse_orientation();
}
double DA=threshold+1;
int countDA=0;
while(DA>=threshold){
if(checkCutOf())
return -10;
int initialArea=abs(polygon->area());
vector<EdgeChange*> changes;
if (countPoints<=10){
for(EdgeIterator ei=polygon->edges_begin();ei!=polygon->edges_end();ei++){
for (VertexIterator vi = polygon->vertices_begin(); vi != polygon->vertices_end(); ++vi){
if(checkCutOf())
return -10;
int stop=0;
if(checkPath(polygon,vi,vi,ei))
continue;
vector<Point> path;
path.push_back(*vi);
double area=calculateNewArea(polygon,*ei,path.front(),path.back(),&path);
EdgeChange* newChange = new EdgeChange(path.front(),path.back(),*ei,area);
changes.push_back(newChange);
// until we have gone out of bounds do this
for (VertexIterator vi2 = vi+1; vi2 != polygon->vertices_end(); ++vi2){
if(checkCutOf())
return -10;
path.push_back(*vi2);
// check if the path is ok with the size
if(path.size()>L){
break;
}
if(ei->point(0)==*vi2||ei->point(1)==*vi2){
stop=1;
break;
}
if(checkPath(polygon,vi,vi2,ei))
continue;
double area=calculateNewArea(polygon,*ei,path.front(),path.back(),&path);
EdgeChange* newChange = new EdgeChange(path.front(),path.back(),*ei,area);
changes.push_back(newChange);
}
if(stop)
continue;
// do this when we have to start from the starting vertex again
for (VertexIterator vi2 = polygon->vertices_begin(); vi2 != vi; ++vi2){
if(checkCutOf())
return -10;
path.push_back(*vi2);
// check if the path is ok with the size
if(path.size()>L){
break;
}
if(ei->point(0)==*vi2||ei->point(1)==*vi2){
stop=1;
break;
}
if(checkPath(polygon,vi,vi2,ei))
continue;
double area=calculateNewArea(polygon,*ei,path.front(),path.back(),&path);
EdgeChange* newChange = new EdgeChange(path.front(),path.back(),*ei,area);
changes.push_back(newChange);
}
if(stop)
continue;
}
}
}
else{
vector<EdgeIterator>see;
srand(time(NULL));
while(see.size()<10){
int flag;
do{
if(checkCutOf())
return -10;
flag=0;
int random=rand()%(countPoints);
for(int i=0;i<see.size();i++){
if(see.at(i)==polygon->edges_begin()+random){
flag=1;
break;
}
}
if (flag==0){
see.push_back(polygon->edges_begin()+random);
}
}while(flag==1);
}
for(int i=0; i<see.size(); i++){
//EdgeIterator ei=see.back();
EdgeIterator ei=see.at(i);
for (VertexIterator vi = polygon->vertices_begin(); vi != polygon->vertices_end(); ++vi){
if(checkCutOf())
return -10;
int stop=0;
if(checkPath(polygon,vi,vi,ei))
continue;
vector<Point> path;
path.push_back(*vi);
double area=calculateNewArea(polygon,*ei,path.front(),path.back(),&path);
EdgeChange* newChange = new EdgeChange(path.front(),path.back(),*ei,area);
changes.push_back(newChange);
// until we have gone out of bounds do this
for (VertexIterator vi2 = vi+1; vi2 != polygon->vertices_end(); ++vi2){
if(checkCutOf())
return -10;
path.push_back(*vi2);
// check if the path is ok with the size
if(path.size()>L){
stop=1;
break;
}
if(ei->point(0)==*vi2||ei->point(1)==*vi2){
stop=1;
break;
}
if(checkPath(polygon,vi,vi2,ei))
continue;
double area=calculateNewArea(polygon,*ei,path.front(),path.back(),&path);
EdgeChange* newChange = new EdgeChange(path.front(),path.back(),*ei,area);
changes.push_back(newChange);
}
if(stop)
continue;
// do this when we have to start from the starting vertex again
for (VertexIterator vi2 = polygon->vertices_begin(); vi2 != vi; ++vi2){
if(checkCutOf())
return -10;
path.push_back(*vi2);
// check if the path is ok with the size
if(path.size()>L){
break;
}
if(ei->point(0)==*vi2||ei->point(1)==*vi2){
stop=1;
break;
}
if(checkPath(polygon,vi,vi2,ei))
continue;
double area=calculateNewArea(polygon,*ei,path.front(),path.back(),&path);
EdgeChange* newChange = new EdgeChange(path.front(),path.back(),*ei,area);
changes.push_back(newChange);
}
if(stop)
continue;
}
}
//}
}
if(changes.size() == 0){//no new changes could be found
return -1;
}
int temp=changes.at(0)->getArea();
EdgeChange* theChange=changes.at(0);
for(int i=1;i<changes.size();i++){
if(checkCutOf())
return -10;
// max
if (typeOfOptimization==1 && temp<changes.at(i)->getArea() ){
temp=changes.at(i)->getArea();
theChange=changes.at(i);
}
// min
else if (typeOfOptimization==2 && temp>changes.at(i)->getArea()){
temp=changes.at(i)->getArea();
theChange=changes.at(i);
}
}
if(typeOfOptimization == 1){
if(theChange->getArea() < 0)
return 0;
}
else if(typeOfOptimization == 2){
if(theChange->getArea() > 0)
return 0;
}
changeEdge(polygon,theChange, countPoints);
*finalArea = abs(polygon->area());
DA = abs(theChange->getArea());
}
return 0;
}