-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulatedAnnealing.cpp
749 lines (671 loc) · 23.2 KB
/
simulatedAnnealing.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#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 "timeManager.hpp"
#include <CGAL/Kd_tree.h>
#include <CGAL/Search_traits_2.h>
#include <CGAL/Fuzzy_iso_box.h>
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, =2:min
//flagSub=1: called for subdivison-have to maintain certain edges =0: no subdivision
int globalStep(Polygon* polygon, int typeOfOptimization, double L, int* finalArea, int countPoints, int initialEnergy, int chArea, int flagSub=0){
if(polygon->is_clockwise_oriented()==0){
polygon->reverse_orientation();
}
Segment left,right;
Point mostLeft;
Point mostRight;
int totalIterations=0;
if(flagSub){
mostLeft =*polygon->left_vertex();
mostRight =*polygon->right_vertex();
for(EdgeIterator ei=polygon->edges_begin();ei!=polygon->edges_end();ei++){
if(checkCutOf())
return -10;
if(ei->point(1)==mostLeft){
left=*ei;
}
if(ei->point(0)==mostRight){
right=*ei;
}
}
}
double T=1;
int currEnergy;
int prevEnergy=initialEnergy;
while (T>=0){
if(checkCutOf())
return -10;
//put an upper bound on how many random choices global step can make so that we avoid an infinite loop
if(totalIterations > 2*L){
return 0;
}
//randomly find points q,s
int currArea;
int q1 = rand()%(countPoints);
int s1;
int flag;
do{
if(checkCutOf())
return -10;
flag=0;
int random=rand()%(countPoints);
if(q1==random){
flag=1;
}
if (flag==0){
s1=random;
}
}while(flag==1);
int tmp=0;
VertexIterator q, s, p, t,r;
int tr=0;
for (VertexIterator vi = polygon->vertices_begin(); vi != polygon->vertices_end(); ++vi){
tr++;
}
for (VertexIterator vi = polygon->vertices_begin(); vi != polygon->vertices_end(); ++vi){
if(checkCutOf())
return -10;
if(tmp == q1){
q = vi;
if (q==polygon->vertices_end()-1)
r=polygon->vertices_begin();
else
r=q+1;
if (q==polygon->vertices_begin())
p=polygon->vertices_end()-1;
else
p=q-1;
}
else if(tmp == s1){
s = vi;
if (s==polygon->vertices_end()-1)
t=polygon->vertices_begin();
else
t=s+1;
}
tmp++;
}
EdgeIterator st;//edge to be broken
EdgeIterator ei;
for (ei = polygon->edges_begin(); ei != polygon->edges_end(); ++ei){
if(checkCutOf())
return -10;
if(ei->point(0) == *s && ei->point(1) == *t){
st = ei;
break;
}
}
//check validity
int valid=0;
if(checkPath(polygon, q, q, st) == 0)
valid=1;
//check if st is one of the marked edges
if(flagSub){
if(*s == mostRight)
valid=0;
if(*t == mostLeft)
valid=0;
if(mostRight == *q)
valid=0;
if(mostLeft == *q)
valid=0;
if(mostRight == *p)
valid=0;
if(mostLeft == *p)
valid=0;
if(mostRight == *r )
valid=0;
if(mostLeft == *r)
valid=0;
}
if(valid){
//apply change to temporary polygon so that we can get the new area
VertexIterator q2Temp;
VertexIterator t2Temp = t;
Polygon temporary(*polygon);
for(VertexIterator vi=temporary.vertices_begin();vi!=temporary.vertices_end();vi++){
if(checkCutOf())
return -10;
if (*vi==*q){
q2Temp=vi;
}
if ( *q==*t){
t2Temp=vi;
}
}
Point tmp = *q2Temp;
Point tmp2 = *t2Temp;
temporary.erase(q2Temp);
for (VertexIterator vi = temporary.vertices_begin(); vi != temporary.vertices_end(); ++vi){
if(checkCutOf())
return -10;
if(*vi == tmp2){
temporary.insert(vi, tmp);
break;
}
}
currArea = abs(temporary.area());
if(typeOfOptimization == 1)
currEnergy = maxEnergy(countPoints, currArea, chArea);
else if(typeOfOptimization == 2)
currEnergy = minEnergy(countPoints, currArea, chArea);
double DE = currEnergy - prevEnergy;
if(DE < 0.0 || Metropolis(DE,T)){//make function for metropolis
Point tmp = *q;
Point tmp2 = *t;
polygon->erase(q);
for (VertexIterator vi = polygon->vertices_begin(); vi != polygon->vertices_end(); ++vi){
if(*vi == tmp2){
polygon->insert(vi, tmp);
break;
}
}
}
T=T-1/L;
prevEnergy = currEnergy;
*finalArea=currArea;
}
totalIterations++;
}
return 0;
}
VertexIterator localAlgorithm(Polygon* polygon, Tree* kd,int countPoints,int* interation,int maxL){
vector<int> seen;
VertexIterator p,q,r,s;
// find a random point until there is no problem
while(1){
(*interation)++;
// find a random point that you have not seen yet
int notSeen=0;
int pos;
while(notSeen==0){
pos=rand()%countPoints;
int i;
for(i=0;i<seen.size();i++){
if (seen.at(i)==pos)
break;
}
if(i==seen.size()){
notSeen=1;
seen.push_back(pos);
}
if(maxL>=seen.size()){
(*interation)--;
break;
}
}
q=polygon->vertices_begin()+pos;
// find the next point
if (q==polygon->vertices_end()-1)
r=polygon->vertices_begin();
else
r=q+1;
if (q==polygon->vertices_begin())
p=polygon->vertices_end()-1;
else
p=q-1;
// find the next point
if (r==polygon->vertices_end()-1)
s=polygon->vertices_begin();
else
s=r+1;
Segment qs=Segment(*q,*s);
Segment pr=Segment(*p,*r);
if(intersection(qs,pr))
continue;
// do the search
int minX=q->x();
int maxX=q->x();
int minY=q->y();
int maxY=q->y();
if(minX>s->x())
minX=s->x();
else if(maxX<s->x())
maxX=s->x();
if(minX>r->x())
minX=r->x();
else if(maxX<r->x())
maxX=r->x();
if(minX>p->x())
minX=p->x();
else if(maxX<p->x())
maxX=p->x();
if(minY>s->y())
minY=s->y();
else if(maxY<s->y())
maxY=s->y();
if(minY>r->y())
minY=r->y();
else if(maxY<r->y())
maxY=r->y();
if(minY>p->y())
minY=p->y();
else if(maxY<p->y())
maxY=p->y();
Fuzzy_box default_range1(Point(minX,minY),Point(maxX,maxY));
std::vector<Point> result;
kd->search(std::back_inserter( result ), default_range1);
int flag=0;
// check if there is a problem or not
for(int i=0;i<result.size();i++){
// if(checkCutOf())
// return -10;
Point x=result.at(i);
if(x==*r || x==*q || x==*p){
continue;
}
for(EdgeIterator ei=polygon->edges_begin();ei!=polygon->edges_end();ei++){
EdgeIterator seen=polygon->edges_end();
if(x ==*s && *s==ei->point(1))
continue;
if(x ==*p && *p==ei->point(0))
continue;
if(x == ei->point(0) || x == ei->point(1))
seen = ei;
else
continue;
if(seen!=polygon->edges_end()){
if(intersection(pr,*seen)){
flag=1;
break;
}
}
}
}
if (flag==1)
continue;
// check if there is a problem or not
flag=0;
for(int i=0;i<result.size();i++){
Point x=result.at(i);
if(x==*q || x==*r || x==*s){
continue;
}
for(EdgeIterator ei=polygon->edges_begin();ei!=polygon->edges_end();ei++){
EdgeIterator seen=polygon->edges_end();
if(x ==*s && *s==ei->point(1))
continue;
if(x ==*p && *p==ei->point(0))
continue;
if(x == ei->point(0) || x == ei->point(1))
seen = ei;
else
continue;
if(seen!=polygon->edges_end()){
if(intersection(qs,*seen)){
flag=1;
break;
}
}
}
}
if (flag==1)
continue;
break;
}
return q;
}
int localMinimum(Polygon* polygon,int typeOfOptimization, double L, int* finalArea, int countPoints, int initialEnergy, int chArea){
// change to clockwise
if(polygon->is_clockwise_oriented()==0){
polygon->reverse_orientation();
}
Tree kd;
for(VertexIterator vi=polygon->vertices_begin(); vi!=polygon->vertices_end();vi++)
kd.insert(*vi);
double T=1;
int currEnergy;
int prevEnergy=initialEnergy;
int countInterator=0;
while (T>=0){
if(checkCutOf())
return -10;
if(countInterator>=2*L){
break;
}
VertexIterator q=localAlgorithm(polygon,&kd,countPoints,&countInterator,2*L);
countInterator++;
if(checkCutOf())
return -10;
if(q==polygon->vertices_end()){
continue;
}
VertexIterator rTemp;
Polygon temporary(*polygon);
int count=0;
for(VertexIterator vi=temporary.vertices_begin();vi!=temporary.vertices_end();vi++){
if(checkCutOf())
return -10;
if (*vi==*q && vi==(temporary.vertices_end()-1)){
rTemp=temporary.vertices_begin();
}
else if ( *q==*vi){
rTemp=vi+1;
}
count++;
}
VertexIterator r1;
if (q==polygon->vertices_end()-1)
r1=polygon->vertices_begin();
else
r1=q+1;
Point temp=*rTemp;
Point x=*q;
temporary.erase(rTemp);
for(VertexIterator vi=temporary.vertices_begin();vi!=temporary.vertices_end();vi++){
if(checkCutOf())
return -10;
if(*vi==x){
temporary.insert(vi,temp);
break;
}
}
// find the energy
int currArea=abs(temporary.area());
if(typeOfOptimization == 1)
currEnergy = maxEnergy(countPoints, currArea, chArea);
else if(typeOfOptimization == 2)
currEnergy = minEnergy(countPoints, currArea, chArea);
// do the transition
double DE = currEnergy - prevEnergy;
if(DE < 0 || Metropolis(DE,T)){//make function for metropolis
Point temp=*r1;
Point x=*q;
polygon->erase(r1);
for(VertexIterator vi=polygon->vertices_begin();vi!=polygon->vertices_end();vi++){
if(*vi==x){
polygon->insert(vi,temp);
break;
}
}
}
if(polygon->is_simple()==0)
break;
T=T-1/L;
*finalArea=currArea;
prevEnergy = currEnergy;
}
return 0;
}
//typeOfOptimization=1: max, =2:min
//typeOfStep=1: local step, =2: global step
int simulated_annealing(Polygon* polygon, int typeOfOptimization, double L, int* finalArea,int countPoints, int typeOfStep, int initialEnergy, int chArea){
//local step
if(typeOfStep==1){
return localMinimum(polygon,typeOfOptimization, L, finalArea, countPoints, initialEnergy, chArea);
}
//global step
else if(typeOfStep==2){
return globalStep(polygon, typeOfOptimization, L, finalArea, countPoints, initialEnergy, chArea);
}
return 0;
}
//greedyAlgo=1: incremental, greedyAlgo=2: convex hull
//greedyEdge=1: random, =2:min, =3:max
//m=[10,100]
int subdivision(Polygon* polygon, vector<Point>* points, int typeOfOptimization, double L, int *finalArea, int countPoints, int chArea, int greedyAlgo, int greedyEdge, int m, int* initialArea){
sortPoints(points, 2);
double area;
int howManyToAdd=m-1;
int k = (int)ceil((double)(countPoints)/(double)(howManyToAdd));
vector<Polygon> polygons(k);
vector<Point> nextPoints;
int offset=0;
int runs=0;
for(int i=0; i<k; i++){
if(checkCutOf())
return -10;
// no more k
if(offset>=countPoints-1)
break;
runs++;
vector<Point> current;
// check if the last point that you are going to add
// satisfies the conditions
// countPoints-1 the last point and -3 as we do not want the last subset to be size =3
int last;
for(last=offset+howManyToAdd;last<countPoints-1-3;last++){
if(checkCutOf())
return -10;
// condition 1
int flag=0;
for(int j=offset;j<offset+howManyToAdd;j++){
if(j==countPoints)
break;
if(points->at(j).y()<points->at(last).y()){
flag=1;
break;
}
}
// it does not satisfy the condition
if(!flag)
continue;
// condition 2
flag=0;
for(int j=offset+howManyToAdd+1;j<offset+2*howManyToAdd;j++){
if(checkCutOf())
return -10;
if(j==countPoints)
break;
if(points->at(last).y()>points->at(j).y()){
flag=1;
break;
}
}
// it does not satisfy the condition
if(!flag)
continue;
// it satisfy the condition
break;
}
int lastSet=0;
if(last>=countPoints-1-3){
last=countPoints-1;
lastSet=1;
}
while(offset<=last){
if(checkCutOf())
return -10;
current.push_back(points->at(offset));
offset++;
}
if (countPoints - offset < (m/2)){
while(offset<=countPoints-1){
current.push_back(points->at(offset));
offset++;
}
lastSet=1;
}
else
offset--;
Segment segments[2];
Point left=current.at(0);
Point right=current.at(current.size()-1);
int flagFirst=1;
for(int i=1;i<current.size()-1;i++){
if(checkCutOf())
return -10;
if(current.at(i).y()<left.y()&&flagFirst){
segments[0]=Segment(left,current.at(i));
flagFirst=0;
}
else if(current.at(i).y()<right.y()){
segments[1]=Segment(current.at(i),right);
}
}
//we call the greedy algorithm requested from the user
//if incremental fails we call convex hull, if convex hull ails the porgram terminates, you have to run it again as edge selection is random
//there is a different version called if we create the polygon of the last set as then we do not care about the existence of the rightmost edge
if(greedyAlgo==1){
vector<Point>temp=current;
int result;
if(lastSet)
result=incremental(&polygons.at(i), &temp, 2, greedyEdge, &area, 1, segments, 1);
else
result=incremental(&polygons.at(i), &temp, 2, greedyEdge, &area, 1, segments);
if(result){
VertexIterator vi=polygons.at(i).vertices_begin();
while(vi!=polygons.at(i).vertices_end())
polygons.at(i).erase(vi);
int result;
if(lastSet)
result=convexHull(&polygons.at(i), ¤t, greedyEdge, &area, 1, segments, 1);
else
result=convexHull(&polygons.at(i), ¤t, greedyEdge, &area, 1, segments);
while(result==5){
polygons.at(i).clear();
if(lastSet)
result=convexHull(&polygons.at(i), ¤t, greedyEdge, &area, 1, segments, 1);
else
result=convexHull(&polygons.at(i), ¤t, greedyEdge, &area, 1, segments);
}
}
}
else{
int result=convexHull(&polygons.at(i), ¤t, greedyEdge, &area, 1, segments);
int counter=0;
while(result==5){
if(counter >=10)
return 5;
polygons.at(i).clear();
result=convexHull(&polygons.at(i), ¤t, greedyEdge, &area, 1, segments);
counter++;
}
}
int o=0;
for (VertexIterator vi = polygons.at(i).vertices_begin(); vi != polygons.at(i).vertices_end(); ++vi){
o++;
}
Point mostLeft, mostRight;
mostLeft = current.at(0);
mostRight = current.at(current.size()-1);
//apply global step to each subset
// find KP to computer KP area for the energy function
vector<Point> KP2;
Polygon tmp;
const Polygon::Vertices& range = polygons.at(i).vertices();
CGAL::convex_hull_2(range.begin(), range.end(), back_inserter(KP2));
for(int w=0; w<KP2.size(); w++)
tmp.push_back(KP2.at(w));
int chArea2 = abs(tmp.area());
//calculate initial energy of the greedy solution
int initialEnergy;
int pArea = abs(polygons.at(i).area());
if(typeOfOptimization == 1)
initialEnergy = maxEnergy(countPoints, pArea, chArea2);
else if(typeOfOptimization == 2)
initialEnergy = minEnergy(countPoints, pArea, chArea2);
//apply global step
int resgs = globalStep(&polygons.at(i), typeOfOptimization, L, finalArea, current.size(), initialEnergy, chArea2, 1);
if(resgs == -10)
return -10;
}
// just insert the first polygon
for(VertexIterator vi=polygons.at(0).vertices_begin();vi!=polygons.at(0).vertices_end();vi++){
polygon->push_back(*vi);
}
if(polygon->is_clockwise_oriented()==0){
polygon->reverse_orientation();
}
// for the other polygons
for(int i=1;i<runs;i++){
if(checkCutOf())
return -10;
// take the current polygon
Polygon* current=&polygons.at(i);
if(current->is_clockwise_oriented()==0){
current->reverse_orientation();
}
// find p and q
Point q, r;
VertexIterator p,next;
for(VertexIterator vAll=polygon->vertices_begin();vAll!=polygon->vertices_end();vAll++){
for(VertexIterator vCurrent=current->vertices_begin();vCurrent!=current->vertices_end();vCurrent++){
if(checkCutOf())
return -10;
if(*vAll==*vCurrent ){
// we found q=vALL=vCurrent
q=*vCurrent;
// if it is the last vertex
if(vAll==polygon->vertices_end()-1){
p=polygon->vertices_begin();
}
else{
p=vAll+1;
}
// find next position of vCurrent
if(vCurrent==current->vertices_end()-1){
next=current->vertices_begin();
}
else{
next=vCurrent+1;
}
}
}
}
int count=0;
Point temp=*p;
int flagQ=0;
// [next,finish) break to q
for(VertexIterator vCurrent2=next;vCurrent2!=current->vertices_end();vCurrent2++){
if(*vCurrent2==q){
flagQ=1;
break;
}
for(VertexIterator vAll=polygon->vertices_begin();vAll!=polygon->vertices_end();vAll++){
if(*vAll==temp){
polygon->insert(vAll,*vCurrent2);
break;
}
}
count++;
}
// from [start,q)
if(flagQ==0){
for(VertexIterator vCurrent2=current->vertices_begin();*vCurrent2!=q;vCurrent2++){
for(VertexIterator vAll=polygon->vertices_begin();vAll!=polygon->vertices_end();vAll++){
if(checkCutOf())
return -10;
if(*vAll==temp){
polygon->insert(vAll,*vCurrent2);
break;
}
}
count++;
}
}
}
//apply local step
int initialEnergy;
*initialArea = abs(polygon->area());
if(typeOfOptimization == 1)
initialEnergy = maxEnergy(countPoints, *initialArea, chArea);
else if(typeOfOptimization == 2)
initialEnergy = minEnergy(countPoints, *initialArea, chArea);
if(checkCutOf())
return -10;
return localMinimum(polygon,typeOfOptimization,L,finalArea,countPoints,initialEnergy,chArea);
}