-
Notifications
You must be signed in to change notification settings - Fork 5
/
simanneal.cpp
845 lines (744 loc) · 23.3 KB
/
simanneal.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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <map>
#include <algorithm>
#include <cstdlib>
#include<cmath>
using namespace std;
int new_cost;
int row;
int col;
int tot;
int cost=0;
int limit=10000;
string x1_y1,x2_y2;
int cnt=0;
int curr_cell=0;
int Wx,Wy;
map <string, bool> duplicate_cell_location; //holds a true value for a key of unique location id, if the location is occupied by a cell
ofstream logfile("Team09_results.txt");
ofstream results("result.csv");
vector<string> split(string strToSplit, char delimeter)
{
stringstream ss(strToSplit);
string item;
vector<string> splittedStrings;
while (getline(ss, item, delimeter))
{
splittedStrings.push_back(item);
}
return splittedStrings;
}
int toint(string strToint)
{
stringstream ss(strToint);
int item = 0;
ss>>item;
return item;
}
string toStr ( int X, int Y )
{
ostringstream ss;
ss << X<<","<<Y;
return ss.str();
}
/*function to tune the aspect ratio close to 1*/
int selectAR(int &row, int &col, int &tot, int &blanks)
{
int row1,row2,col1,col2, blanks1, blanks2;
float AR;
row1 = row-1;
row2 = row+1;
/*tuning rows*/
do
{
col1 = ceil((float)tot/row1);
AR = ((8*round(row1))+8)/(float)col1;
blanks1 =(row1*col1)-tot;
if((AR>=0.96) && (AR<=1.04))
{
if(blanks1 < blanks)
{
row = row1;
col = col1;
blanks = blanks1;
}
}
row1=row1-1;
}
while(AR>=0.96);
/*tuning columns*/
do
{
col2 = ceil((float)tot/row2);
AR = ((8*round(row2))+8)/(float)col2;
blanks2 = (row2*col2)-tot;
if((AR>=0.96) && (AR<=1.04))
{
if(blanks2 < blanks)
{
row = row2;
col = col2;
blanks = blanks2;
}
}
row2=row2+1;
}
while(AR<= 1.04);
}
class net
{
public:
//May change after the move
map <int,int>x_coord;
map <int,int>y_coord;
int length;
//Will not change even after the move
map <int,bool>cell_list; // key: cell_no, value: true if that cell present on that cell
int cell_count;
//CONSTRUCTOR
net()
{
cell_count = 0;
cell_list.clear();
x_coord.clear();
y_coord.clear();
length = 0;
}
};
class node
{
public:
//Will not change even after the move
vector <int> net_list;
//Will change after the move
int x;
int y;
};
map <int, net> net_map; //primary data structure for all nets
map <int, net>::iterator net_it;
map <int, node> cell; //primary data structure for all cells
map <int, node>::iterator cell_it;
map <string, int> from_location; //map of (location id, cell no.) pair
/*function for net length calculation*/
int netlength(int &net_no)
{
int xmax,xmin;
int ymax,ymin;
map <int,int>::iterator it1;
map <int,int>::iterator it2;
int length=0;
it1= net_map[net_no].x_coord.begin();
xmin=it1->first;
it1=net_map[net_no].x_coord.end();
it1--;
xmax=it1->first;
it2= net_map[net_no].y_coord.begin();
ymin=it2->first;
it2= net_map[net_no].y_coord.end();
it2--;
ymax= it2->first;
length = ((xmax-xmin)*4)+((ymax-ymin)*16);
return(length);
}
/*function to make or undo the move*/
int check_lengths(bool swap, bool undo)
{
int curr_net;
int old_length;
int new_length;
int old_cost = cost;
int delta_c;
int i;
float temp;
int xmin,xmax;
int ymin,ymax;
map <int,int>::iterator it1;
map <int,int>::iterator it2;
int x2;
int y2;
bool blank1=false;
bool blank2=false;
int x1;
int y1;
new_cost = cost;
if ( duplicate_cell_location[x1_y1] ) //checking if cell1 is not a blank location
{
x1=cell[from_location[x1_y1]].x;
y1=cell[from_location[x1_y1]].y;
}
else
{
vector<string> loc;
loc = split(x1_y1,',');
x1 = toint(loc[0]);
y1 = toint(loc[1]);
blank1 = true;
}
if ( duplicate_cell_location[x2_y2] ) //check if cell2 is not a blank location
{
x2=cell[from_location[x2_y2]].x;
y2=cell[from_location[x2_y2]].y;
}
else
{
vector<string> loc;
loc = split(x2_y2,',');
x2 = toint(loc[0]);
y2 = toint(loc[1]);
blank2 = true;
}
if(!blank1) //if cell1 is not blank, calculate lengths of the all the nets connected to cell1
{
for(i=0;i<cell[from_location[x1_y1]].net_list.size();i++)
{
curr_net=cell[from_location[x1_y1]].net_list[i];
if(!blank2) //cell2 is not blabnk
{
if(net_map[curr_net].cell_list[from_location[x2_y2]]) //cell2 is on the same net
{
continue; // no need to calculate net length
}
}
if(!undo)
{
old_length = net_map[curr_net].length;
it1 = net_map[curr_net].x_coord.begin();
it2 = net_map[curr_net].x_coord.end(); //points to the past the end of the map
it2--; // points to the actual end of the net
xmin = it1->first; //current min
xmax = it2->first; //current max
if(x2<xmin) //is future min less than current min?
{
xmin = x2;
}
else if(x1 == xmin ) //is current cell one of the horizontal extremes?
{
if(net_map[curr_net].x_coord[x1] == 1) //is that the only cell?
{
it1++;
if(x2<=it1->first) //comparing potential xmin
{
xmin=x2;
}
else
{
xmin = it1->first;
}
}
}
if(x2>xmax) //is future max greater than the current max?
{
xmax=x2;
}
else if(x1 == xmax) //is current cell one of the horizontal extremes?
{
if(net_map[curr_net].x_coord[x1] == 1) //is that the only cell?
{
it2--;
if(x2>=it2->first) //comparing potential xmax
{
xmax=x2;
}
else
{
xmax=it2->first;
}
}
}
it1 = net_map[curr_net].y_coord.begin();
it2 = net_map[curr_net].y_coord.end();
it2--;
ymin = it1->first; //current min
ymax = it2->first; //current max
if(y2<ymin) //is future min less than current min?
{
ymin = y2;
}
else if(y1 == ymin) //is current cell one of the vertical extremes?
{
if(net_map[curr_net].y_coord[y1] == 1) //is that the only cell?
{
it1++;
if(y2<=it1->first) //comparing potential ymin
{
ymin=y2;
}
else
{
ymin = it1->first;
}
}
}
if(y2>ymax) //is future max greater than the current max?
{
ymax=y2;
}
if(y1 == ymax ) //is current cell one of the vertical extremes?
{
if(net_map[curr_net].y_coord[y1] == 1)
{ //is that the only cell?
it2--;
if(y2>=it2->first)
{ //comparing potential ymax
ymax=y2;
}
else
{
ymax=it2->first;
}
}
}
new_length=((xmax-xmin)*4)+((ymax-ymin)*16);
new_cost=new_cost-old_length+new_length;
}
if ( swap )
{
if(!blank2)
{
if( net_map[curr_net].cell_list[from_location[x2_y2]] ) //cell2 is blank and is on the same net
{
continue; //no need to calculate length
}
}
net_map[curr_net].x_coord[x1]--; //update x coordinates for the net
if(net_map[curr_net].x_coord[x1] == 0)
{
net_map[curr_net].x_coord.erase(x1);
}
net_map[curr_net].x_coord[x2]++;
net_map[curr_net].y_coord[y1]--; //update y coordinates for the net
if(net_map[curr_net].y_coord[y1] == 0)
{
net_map[curr_net].y_coord.erase(y1);
}
net_map[curr_net].y_coord[y2]++;
new_length=netlength(curr_net); //calculate netlength
old_length = net_map[curr_net].length;
new_cost=new_cost-old_length+new_length;
net_map[curr_net].length = new_length;
cost = new_cost;
}
}
}
if ( !blank2 )
{
for(i=0;i<cell[from_location[x2_y2]].net_list.size();i++)
{
curr_net=cell[from_location[x2_y2]].net_list[i];
if(!blank1)
{
if(net_map[curr_net].cell_list[from_location[x1_y1]])
{
continue;
}
}
if(!undo)
{
old_length = net_map[curr_net].length;
it1 = net_map[curr_net].x_coord.begin();
it2 = net_map[curr_net].x_coord.end(); //points to the past the end of the map
it2--; // points to the actual end of the net
xmin = it1->first; //current min
xmax = it2->first; //current max
if(x1<xmin) //is future min less than current min?
{
xmin = x1;
}
else if(x2 == xmin ) //is current cell one of the horizontal extremes?
{
if(net_map[curr_net].x_coord[x2] == 1) //is that the only cell?
{
it1++;
if(x1<=it1->first) //comparing potential xmin
{
xmin=x1;
}
else
{
xmin = it1->first;
}
}
}
if(x1>xmax) //is future max greater than the current max?
{
xmax=x1;
}
else if(x2 == xmax) //is current cell one of the horizontal extremes?
{
if(net_map[curr_net].x_coord[x2] == 1) //is that the only cell?
{
it2--;
if(x1>=it2->first) //comparing potential xmax
{
xmax=x1;
}
else
{
xmax=it2->first;
}
}
}
it1 = net_map[curr_net].y_coord.begin();
it2 = net_map[curr_net].y_coord.end();
it2--;
ymin = it1->first; //current min
ymax = it2->first; //current max
if(y1<ymin) //is future min less than current min?
{
ymin = y1;
}
else if(y2 == ymin) //is current cell one of the vertical extremes?
{
if(net_map[curr_net].y_coord[y2] == 1) //is that the only cell?
{
it1++;
if(y1<=it1->first) //comparing potential ymin
{
ymin=y1;
}
else
{
ymin = it1->first;
}
}
}
if(y1>ymax) //is future max greater than the current max?
{
ymax=y1;
}
if(y2 == ymax ) //is current cell one of the vertical extremes?
{
if(net_map[curr_net].y_coord[y2] == 1) //is that the only cell?
{
it2--;
if(y1>=it2->first) //comparing potential ymax
{
ymax=y1;
}
else
{
ymax=it2->first;
}
}
}
new_length=((xmax-xmin)*4)+((ymax-ymin)*16);
new_cost=new_cost-old_length+new_length;
}
if(swap)
{
if(!blank1)
{
if( net_map[curr_net].cell_list[from_location[x1_y1]] )
{
continue;
}
}
net_map[curr_net].x_coord[x2]--; //update x coordinates for the net
if(net_map[curr_net].x_coord[x2] == 0)
{
net_map[curr_net].x_coord.erase(x2);
}
net_map[curr_net].x_coord[x1]++;
net_map[curr_net].y_coord[y2]--; //update y coordinates for the net
if(net_map[curr_net].y_coord[y2] == 0)
{
net_map[curr_net].y_coord.erase(y2);
}
net_map[curr_net].y_coord[y1]++;
new_length = netlength(curr_net); //calculate netlength
old_length = net_map[curr_net].length;
new_cost=new_cost-old_length+new_length;
net_map[curr_net].length = new_length;
cost = new_cost;
}
}
}
if (swap)
{
int temp;
if(!blank1 && !blank2) //swap cell1 and cell2
{
temp = cell[from_location[x1_y1]].x;
cell[from_location[x1_y1]].x = cell[from_location[x2_y2]].x;
cell[from_location[x2_y2]].x = temp;
temp = cell[from_location[x1_y1]].y;
cell[from_location[x1_y1]].y = cell[from_location[x2_y2]].y;
cell[from_location[x2_y2]].y = temp;
temp = from_location[x1_y1];
from_location[x1_y1] = from_location[x2_y2];
from_location[x2_y2] = temp;
}
else if(blank1) //swap cell2 and blank
{
cell[from_location[x2_y2]].x = x1;
cell[from_location[x2_y2]].y = y1;
from_location[x1_y1] = from_location[x2_y2];
from_location.erase(x2_y2);
duplicate_cell_location[x1_y1] = true;
duplicate_cell_location[x2_y2] = false;
}
else if(blank2) //swap cell1 and blank
{
cell[from_location[x1_y1]].x = x2;
cell[from_location[x1_y1]].y = y2;
from_location[x2_y2] = from_location[x1_y1];
from_location.erase(x1_y1);
duplicate_cell_location[x2_y2] = true;
duplicate_cell_location[x1_y1] = false;
}
}
delta_c = new_cost-old_cost; //delta_c calculation
return delta_c;
}
/*function bellow returns true if cells to be swapped are in range*/
bool inwindow(int x1, int x2, int y1, int y2)
{
if((abs(x2-x1)<=Wx) && (abs(y2-y1)<=Wy))
{
return true;
}
else
{
return false;
}
}
/*generates random two locations to be swapped*/
int generate()
{
int x1,x2;
int y1,y2;
do
{
x1 = rand()%col;
y1 = 2*(rand()%row)+1;
x1_y1 = toStr(x1,y1);
}while(!duplicate_cell_location[x1_y1]); //cell1 has to be a cell
do
{
x2 = rand()%col;
y2 = 2*(rand()%row)+1;
x2_y2 = toStr(x2,y2);
}while((x1_y1 == x2_y2) && (!inwindow(x1,x2,y1,y2)) ); //cell2 is not cell1 and is in window
}
/*function to decide if the move is acceptable at current temperature*/
bool accept(int &delta_c, float &T)
{
float prob;
float r;
int iprob;
prob = exp((-delta_c)/(float)T);
r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); //generates a random no. between 0-1
float one = 1.0;
if( r < min(prob,one) )
{
results<<cost<<endl;
return true;
}
else
{
results<<","<<cost<<endl;
return false;
}
}
int main (int argc, char* argv[])
{
string line;
vector<string> netline;
vector<string> nodeline;
map <int,bool> duplicate_cell; //holds a true value for every cell no. present in the circuit
int cell_no=0;
int net_no=0;
float AR;
int blanks=0;
int x = 0;
int y = 1;
string x_y;
int delta_c=0;
float T,T1;
int innerLimit;
bool undo = true;
bool swap = true;
bool flag = false;
srand(time(NULL));
time_t start_time;
time_t time1;
if(argc < 2)
{
cout<<"please specify the input file name\n";
return 0;
}
const char* arg = argv[1];
ifstream inputfile(arg);
if(inputfile.is_open())
{
for(int i =0; i<5; i++)
{
getline (inputfile,line); //line#5 gives total no. of cells in the circuit
}
tot = toint(line)+1;
row = round((-4 + sqrt(16 + 32 * tot)) / 16);
if ( tot <500 ) //if circuit consists of less than 500 cells
{
innerLimit = 3000;
}
else //circuit consists of more than 500 cells
{
innerLimit = tot*2.5;
}
col = ceil((float)tot/row);
AR = ((8*round(row))+8)/(float)ceil(col);
blanks =(row*col)-tot;
selectAR(row,col,tot,blanks); //Tune aspect ratio
AR = ((8*round(row))+8)/(float)ceil(col);
blanks =(row*col)-tot;
Wx = col*4;
Wy = (2*row+1)*16;
logfile<<"total cells: "<<tot<<endl;
logfile<<"rows: "<<row<<endl;
logfile<<"columns: "<<col<<endl;
logfile<<"AR: "<<AR<<endl;
logfile<<"blanks: "<<blanks<<endl;
while(getline (inputfile,line))
{
if(line.find("s")!= string::npos)
{
if(net_map[net_no].cell_count>1)
{
net_map[net_no].length = netlength(net_no); //calculating net length
cost = cost+net_map[net_no].length ;
net_no++;
}
if(net_map[net_no].cell_count==1) //ignoring nets with only 1 cell
{
cell[cell_no].net_list.pop_back();
net_map[net_no].cell_list[cell_no] = false;
net_map[net_no].cell_count--;
if(net_map[net_no].cell_count == 0)
{
net_map.erase(net_no);
}
}
}
if(line.find("a") != string::npos)
{
net_map[net_no].cell_count++;
nodeline = split(line, ' ');
nodeline = split(nodeline[0], 'a');
cell_no = toint(nodeline[1]);
cell[cell_no].net_list.push_back(net_no); //adding the net to the netlist of the cell
net_map[net_no].cell_list[cell_no] = true;
if(!duplicate_cell[cell_no]) //if cell is new
{
duplicate_cell[cell_no] = true;
do
{
x = rand()%col; //pick random x location
y = 2*(rand()%row)+1; //pick random y location
x_y = toStr(x,y); //create a unique location id
}while(duplicate_cell_location[x_y]); //initial random placement
cell[cell_no].x=x;
cell[cell_no].y=y;
from_location[x_y]=cell_no;
duplicate_cell_location[x_y] = true;
}
net_map[net_no].x_coord[cell[cell_no].x]++;
net_map[net_no].y_coord[cell[cell_no].y]++;
}
}
inputfile.close();
if(net_map[net_no].cell_count==1) //ignoring if last net has only one cell
{
cell[cell_no].net_list.pop_back();
net_map[net_no].cell_count--;
if(net_map[net_no].cell_count == 0)
{
net_map.erase(net_no);
}
}
else //updating parameters for last net
{
net_map[net_no].length = netlength(net_no);
cost = cost+net_map[net_no].length ;
}
}
logfile<<"initial cost: "<<cost<<endl;
T = 40000; //Initial temperature
/*
ofstream tem ("temperature.csv");
time (&start_time);
tem<<"Iteration#, Temperature, InternalLimit, End cost, Time\n";
tem<<cnt<<","<<T<<","<<innerLimit<<","<<cost<<", "<<ctime(&start_time);
*/
while(T>0.1)
{
int count =0; //initializing internal iteration count for this temp.
int rejected = 0; //rejection count for this temp.
int accepted = 0; //acceptance count for this temp.
while( count < innerLimit )
{
cnt++; //total iteration count
generate(); //generate two locations to swap
if(!flag) //flag is low since rejections < acceptance
{
delta_c = check_lengths(swap,undo); //do the swap and ignore calculation part
if(!accept(delta_c,T))
{
check_lengths(swap,undo); //rejected! swap again and undo
rejected++;
}
else
{
accepted++;
}
}
else //flag is high since rejections > acceptance
{
delta_c = check_lengths(!swap,!undo); //don't swap, check first
if(accept(delta_c,T))
{
check_lengths(swap,undo); //accepted!, do the swap
accepted++;
}
else
{
rejected++;
}
}
count++;
}
/*
time (&time1);
tem<<cnt<<","<<T<<","<<innerLimit<<","<<cost<<","<<difftime(time1,start_time)<<endl;
*/
if(accepted == 0) //exit if nothing gets accepted
{
goto end;
}
else if(accepted < rejected)
{
flag = true;
if(T>4)
{
T1 = 0.95*(float)T; //lower the rate of temp. reduction
}
else
{
T1 = 0.98*(float)T; //temp. reducing at the lowest rate
}
}
else
{
T1 = 0.80*(float)T; //temp. reducing by 20%
}
Wx = Wx*(log(T1)/log(T)); //update window width
Wy = Wy*(log(T1)/log(T)); //update window hight
T= T1;
}
end:logfile<<"final cost: "<<cost<<endl;
logfile.close();
return 0;
}