-
Notifications
You must be signed in to change notification settings - Fork 46
/
npe.c
357 lines (311 loc) · 8.89 KB
/
npe.c
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
#include <stdio.h>
#ifdef _MSC_VER
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
#include <strings.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "npe.h"
#include "flp.h"
#include "util.h"
void fill_unit_pos(NPE_t *expr)
{
int i, j=0;
for (i=0; i < expr->size; i++)
if (expr->elements[i] >= 0) {
expr->unit_pos[j] = i;
j++;
}
expr->n_units = j;
}
void fill_flip_pos(NPE_t *expr)
{
int i, j=0;
for (i=0; i < expr->size - 1; i++)
if ((expr->elements[i] < 0 && expr->elements[i+1] >= 0) ||
(expr->elements[i] >= 0 && expr->elements[i+1] < 0)) {
expr->flip_pos[j] = i;
j++;
}
expr->n_flips = j;
}
void fill_chain_pos(NPE_t *expr)
{
int i=0, j=0, prev;
while (i < expr->size) {
if (expr->elements[i] < 0) {
expr->chain_pos[j] = i;
j++;
/* skip this chain of successive cuts */
prev = expr->elements[i];
i++;
while(i < expr->size && expr->elements[i] < 0) {
if (expr->elements[i] == prev)
fatal("NPE not normalized\n");
prev = expr->elements[i];
i++;
}
} else
i++;
}
expr->n_chains = j;
}
void fill_ballot_count(NPE_t *expr)
{
int i, ballot_count = 0;
for (i=0; i < expr->size; i++) {
if (expr->elements[i] < 0)
ballot_count++;
expr->ballot_count[i] = ballot_count;
}
}
/* the starting solution for simulated annealing */
NPE_t *NPE_get_initial(flp_desc_t *flp_desc)
{
int i;
NPE_t *expr = (NPE_t *) calloc(1, sizeof(NPE_t));
if (!expr)
fatal("memory allocation error\n");
expr->size = 2 * flp_desc->n_units - 1;
expr->elements = (int *) calloc(expr->size, sizeof(int));
expr->unit_pos = (int *) calloc(flp_desc->n_units, sizeof(int));
expr->flip_pos = (int *) calloc(expr->size, sizeof(int));
expr->chain_pos = (int *) calloc(flp_desc->n_units-1, sizeof(int));
expr->ballot_count = (int *) calloc(expr->size, sizeof(int));
if(!expr->elements || !expr->unit_pos || !expr->flip_pos
|| !expr->chain_pos || !expr->ballot_count)
fatal("memory allocation error\n");
/* starting solution - 0, 1, V, 2, V, ..., n-1, V */
expr->elements[0] = 0;
for (i=1; i < expr->size; i+=2) {
expr->elements[i] = (i+1) / 2;
expr->elements[i+1] = CUT_VERTICAL;
}
fill_unit_pos(expr);
fill_flip_pos(expr);
fill_chain_pos(expr);
fill_ballot_count(expr);
return expr;
}
void free_NPE(NPE_t *expr)
{
free(expr->elements);
free(expr->unit_pos);
free(expr->flip_pos);
free(expr->chain_pos);
free(expr->ballot_count);
free(expr);
}
/* debug print */
void print_NPE(NPE_t *expr, flp_desc_t *flp_desc)
{
int i;
fprintf(stdout, "printing normalized polish expression ");
fprintf(stdout, "of size %d\n", expr->size);
fprintf(stdout, "%s", flp_desc->units[expr->elements[0]].name);
for(i=1; i < expr->size; i++) {
if (expr->elements[i] >= 0)
fprintf(stdout, ", %s", flp_desc->units[expr->elements[i]].name);
else if (expr->elements[i] == CUT_VERTICAL)
fprintf(stdout, ", V");
else if (expr->elements[i] == CUT_HORIZONTAL)
fprintf(stdout, ", H");
else
fprintf(stdout, ", X");
}
fprintf(stdout, "\n");
fprintf(stdout, "unit_pos:\n");
for(i=0; i < expr->n_units; i++)
fprintf(stdout, "%d\t", expr->unit_pos[i]);
fprintf(stdout, "\nflip_pos:\n");
for(i=0; i < expr->n_flips; i++)
fprintf(stdout, "%d\t", expr->flip_pos[i]);
fprintf(stdout, "\nchain_pos:\n");
for(i=0; i < expr->n_chains; i++)
fprintf(stdout, "%d\t", expr->chain_pos[i]);
fprintf(stdout, "\nballot_count:\n");
for(i=0; i < expr->size; i++)
fprintf(stdout, "%d\t", expr->ballot_count[i]);
fprintf(stdout, "\n");
}
/*
* move M1 of the floorplan paper
* swap two units adjacent in the NPE
*/
void NPE_swap_units(NPE_t *expr, int pos)
{
int i, t;
/* find adjacent unit */
for (i=pos+1; i < expr->size; i++)
if (expr->elements[i] >= 0)
break;
if (i >= expr->size)
fatal("unable to find adjacent unit\n");
/* swap */
t = expr->elements[pos];
expr->elements[pos] = expr->elements[i];
expr->elements[i] = t;
}
/* move M2 - invert a chain of cut_types in the NPE */
void NPE_invert_chain(NPE_t *expr, int pos)
{
int i = pos+1, prev = expr->elements[pos];
if (expr->elements[pos] == CUT_VERTICAL)
expr->elements[pos] = CUT_HORIZONTAL;
else if (expr->elements[pos] == CUT_HORIZONTAL)
expr->elements[pos] = CUT_VERTICAL;
else
fatal("invalid NPE in invert_chain\n");
while(i < expr->size && expr->elements[i] < 0) {
if (expr->elements[i] == prev)
fatal("NPE not normalized\n");
prev = expr->elements[i];
if (expr->elements[i] == CUT_VERTICAL)
expr->elements[i] = CUT_HORIZONTAL;
else if (expr->elements[i] == CUT_HORIZONTAL)
expr->elements[i] = CUT_VERTICAL;
else
fatal("unknown cut type\n");
i++;
}
}
/* binary search and increment the unit position by delta */
int update_unit_pos(NPE_t *expr, int pos, int delta,
int start, int end)
{
int mid;
if (start > end)
return FALSE;
mid = (start + end) / 2;
if (expr->unit_pos[mid] == pos) {
expr->unit_pos[mid] += delta;
return TRUE;
} else if (expr->unit_pos[mid] > pos)
return update_unit_pos(expr, pos, delta, start, mid-1);
else
return update_unit_pos(expr, pos, delta, mid+1, end);
}
/*
* move M3 - swap adjacent cut_type and unit in the NPE
* - could result in a non-allowable move. hence returns
* if the move is legal or not
*/
int NPE_swap_cut_unit(NPE_t *expr, int pos)
{
int t;
if (pos <= 0 || pos >= expr->size -1)
fatal("invalid position in NPE_swap_cut_unit\n");
/* unit, cut_type swap */
if (expr->elements[pos] >= 0) {
/* swap leads to consecutive cut_types that are identical? */
if (expr->elements[pos-1] == expr->elements[pos+1])
return FALSE;
/* move should not violate the balloting property */
if (2 * expr->ballot_count[pos+1] >= pos+1)
return FALSE;
/* unit's position is advanced by 1 */
if (!update_unit_pos(expr, pos, 1, 0, expr->n_units-1))
fatal("unit position not found\n");
expr->ballot_count[pos]++;
} else { /* cut_type, unit swap */
/* swap leads to consecutive cut_types that are identical? */
if ((pos < expr->size - 2) && (expr->elements[pos] == expr->elements[pos+2]))
return FALSE;
/* unit's position is reduced by 1 */
if (!update_unit_pos(expr, pos+1, -1, 0, expr->n_units-1))
fatal("unit position not found\n");
expr->ballot_count[pos]--;
}
/* swap O.K */
t = expr->elements[pos];
expr->elements[pos] = expr->elements[pos+1];
expr->elements[pos+1] = t;
/* flip and chain positions altered. recompute them */
fill_flip_pos(expr);
fill_chain_pos(expr);
return TRUE;
}
/* make a random move out of the above */
NPE_t *make_random_move(NPE_t *expr)
{
int i, move, count = 0, done = FALSE, m3_count;
NPE_t *copy = NPE_duplicate(expr);
while (!done && count < MAX_MOVES) {
/* choose one of three moves */
move = rand_upto(3);
switch(move) {
case 0: /* swap adjacent units */
/* leave the unit last in the NPE */
i = rand_upto(expr->n_units-1);
#if VERBOSE > 2
fprintf(stdout, "making M1 at %d\n", expr->unit_pos[i]);
#endif
NPE_swap_units(copy, expr->unit_pos[i]);
done = TRUE;
break;
case 1: /* invert an arbitrary chain */
i = rand_upto(expr->n_chains);
#if VERBOSE > 2
fprintf(stdout, "making M2 at %d\n", expr->chain_pos[i]);
#endif
NPE_invert_chain(copy, expr->chain_pos[i]);
done = TRUE;
break;
case 2: /* swap a unit and an adjacent cut_type */
m3_count = 0;
while (!done && m3_count < MAX_MOVES) {
i = rand_upto(expr->n_flips);
#if VERBOSE > 2
fprintf(stdout, "making M3 at %d\n", expr->flip_pos[i]);
#endif
done = NPE_swap_cut_unit(copy, expr->flip_pos[i]);
m3_count++;
}
break;
default:
fatal("unknown move type\n");
break;
}
count++;
}
if (count == MAX_MOVES) {
char msg[STR_SIZE];
sprintf(msg, "tried %d moves, now giving up\n", MAX_MOVES);
fatal(msg);
}
return copy;
}
/* make a copy of this NPE */
NPE_t *NPE_duplicate(NPE_t *expr)
{
int i;
NPE_t *copy = (NPE_t *) calloc(1, sizeof(NPE_t));
if (!copy)
fatal("memory allocation error\n");
copy->elements = (int *) calloc(expr->size, sizeof(int));
copy->unit_pos = (int *) calloc(expr->n_units, sizeof(int));
copy->flip_pos = (int *) calloc(expr->size, sizeof(int));
copy->chain_pos = (int *) calloc(expr->n_units-1, sizeof(int));
copy->ballot_count = (int *) calloc(expr->size, sizeof(int));
if(!copy->elements || !copy->unit_pos || !copy->flip_pos
|| !copy->chain_pos || !copy->ballot_count)
fatal("memory allocation error\n");
copy->size = expr->size;
for (i=0; i < expr->size; i++)
copy->elements[i] = expr->elements[i];
copy->n_units = expr->n_units;
for (i=0; i < expr->n_units; i++)
copy->unit_pos[i] = expr->unit_pos[i];
copy->n_flips = expr->n_flips;
for (i=0; i < expr->n_flips; i++)
copy->flip_pos[i] = expr->flip_pos[i];
copy->n_chains = expr->n_chains;
for (i=0; i < expr->n_chains; i++)
copy->chain_pos[i] = expr->chain_pos[i];
for (i=0; i < expr->size; i++)
copy->ballot_count[i] = expr->ballot_count[i];
return copy;
}