forked from JackHack96/logic-synthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrp.c
327 lines (267 loc) · 6.94 KB
/
rp.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
/*
* Bill Lin
* University of California, Berkeley
* Comments to [email protected]
*
* Copyright (c) 1989 Bill Lin, UC Berkeley CAD Group
* Permission is granted to do anything with this
* code except sell it or remove this message.
*/
#include "jedi.h"
#include "rp.h"
#include "rp_int.h"
double determine_cost();
void
combinatorial_optimize (
cost_function, generate_function,
accept_state, reject_state,
fin, fout, ferr,
beginning_states, ending_states,
starting_temperature,
maximum_temperature,
problem_size, verbose
)
int (*cost_function)();
int (*generate_function)();
int (*accept_state)();
int (*reject_state)();
FILE *fin;
FILE *fout;
FILE *ferr;
double beginning_states;
double ending_states;
double starting_temperature;
double maximum_temperature;
int problem_size;
int verbose;
{
int max_gen, cur_gen, temp_points;
double c1, c2, c3, c4, start_cost;
double temp, start_temp, stop_temp;
double update_temp();
double find_starting_temp();
long time;
/*
* initialization
*/
Fin = fin;
Fout = fout;
Ferr = ferr;
if (beginning_states == SA_DEFAULT_PARAMETER) {
StatesBegin = 1;
} else {
StatesBegin = beginning_states;
}
if (ending_states == SA_DEFAULT_PARAMETER) {
StatesEnd = 1;
} else {
StatesEnd = ending_states;
}
Alpha = 0.90;
stop_temp = 1;
NewStatesPerUnit = StatesBegin;
RangeSmall = SA_FULL_RANGE;
Verbose = verbose;
OldCost = determine_cost(cost_function);
start_temp = find_starting_temp(starting_temperature, maximum_temperature);
temp = start_temp;
/* start time */
time = util_cpu_time();
c1 = OldCost + 10;
c2 = c1 + 10;
c3 = OldCost;
c4 = c2 + 10;
start_cost = OldCost;
cur_gen = 0;
temp_points = 0;
if (Verbose) {
(void) fprintf(Fout, "starting condition: ");
(void) fprintf(Fout, "starting temperature is %.4g; ", start_temp);
(void) fprintf(Fout, "starting cost is %.4g\n", start_cost);
(void) fprintf(Fout, "\n");
(void) fflush(Fout);
}
while (!stopping_criterion(c1, c2, c3, c4, stop_temp, temp)) {
max_gen = NewStatesPerUnit * problem_size;
cur_gen = 0;
temp_points++;
while (!inner_loop_criterion(max_gen, cur_gen)) {
generate_new_state(RangeSmall,generate_function);
cur_gen++;
if (should_accept(temp,cost_function)) {
accept_new_state(accept_state);
} else {
reject_new_state(reject_state);
}
}
c1 = c2;
c2 = c3;
c3 = c4;
c4 = OldCost;
temp = update_temp(temp, start_temp);
if (Verbose) {
(void) fprintf(Fout, "new temperature is %.2g; ", temp);
(void) fprintf(Fout, "new cost is %.4g; ", OldCost);
(void) fprintf(Fout, "changed after %d states\n", cur_gen);
(void) fflush(Fout);
}
}
/*
* print statistics
*/
if (Verbose) {
(void) fprintf(Fout, "\n");
(void) fprintf(Fout, "starting condition: ");
(void) fprintf(Fout, "starting temperature was %.4g; ", start_temp);
(void) fprintf(Fout, "starting cost was %.4g\n", start_cost);
(void) fprintf(Fout, "final condition: ");
(void) fprintf(Fout, "final temperature was %.4g; ", temp);
(void) fprintf(Fout, "final cost was %.4g\n", OldCost);
(void) fprintf(Fout, "stopped after %d temperature points\n",
temp_points);
(void) fprintf(Fout, "\n");
time = util_cpu_time() - time;
(void) fprintf(Ferr, "total cpu time is %s.\n", util_print_time(time));
}
} /* end of simulated annealing */
should_accept(temp, cost_function)
double temp;
int (*cost_function)();
{
double change_in_cost, R, Y, random_generator();
NewCost = determine_cost(cost_function);
change_in_cost = NewCost - OldCost;
if (change_in_cost < 0) {
return (TRUE);
} else {
Y = exp(-change_in_cost/temp);
R = random_generator(0, 1);
if (R < Y)
return (TRUE);
else
return (FALSE);
}
} /* end of should_accept */
double
update_temp(temp, start_temp)
double temp;
double start_temp;
{
double new_temp, factor;
if (sqrt(start_temp) < temp)
new_temp = temp * Alpha * Alpha;
else
new_temp = temp * Alpha;
if (new_temp >= 1.0) {
factor = log(new_temp)/log(start_temp);
} else {
factor = 0;
RangeSmall = SA_SMALL_RANGE;
}
NewStatesPerUnit = StatesEnd - (StatesEnd - StatesBegin)*factor;
return (new_temp);
} /* end of update_temp */
accept_new_state(accept_state)
int (*accept_state)();
{
int ret_val;
OldCost = NewCost;
ret_val = (*accept_state)();
if (ret_val != SA_OK) {
(void) fprintf(Ferr, "Panic: error in accept new state function\n");
(void) exit(1);
}
} /* end of accept_new_state */
reject_new_state(reject_state)
int (*reject_state)();
{
int ret_val;
ret_val = (*reject_state)();
if (ret_val != SA_OK) {
(void) fprintf(Ferr, "Panic: error in reject new state function\n");
(void) exit(1);
}
} /* end of reject_new_state */
inner_loop_criterion(num_states_per_stage, num_states_generated)
int num_states_per_stage, num_states_generated;
{
if (num_states_per_stage < num_states_generated)
return (TRUE);
else
return (FALSE);
} /* end of inner_loop_criterion */
stopping_criterion(c1, c2, c3, c4, stop_temp, temp)
double c1, c2, c3, c4;
double stop_temp, temp;
{
if (stop_temp < temp)
return (FALSE);
if ((c1 == c2) && (c1 == c3) && (c1 == c4))
return (TRUE);
else
return (FALSE);
} /* end of stopping_criterion */
double
find_starting_temp(starting_temperature, maximum_temperature)
double starting_temperature, maximum_temperature;
{
double delta_c;
double start_temp;
delta_c = 0.04 * OldCost;
start_temp = delta_c/log(1.25);
if (starting_temperature != SA_DEFAULT_PARAMETER) {
start_temp = starting_temperature;
}
if (maximum_temperature != SA_DEFAULT_PARAMETER) {
if (start_temp > maximum_temperature) {
start_temp = maximum_temperature;
}
}
return (start_temp);
} /* end of find_starting_temp */
double determine_cost(cost_function)
int (*cost_function)();
{
int ret_val;
double cost = 0.0;
ret_val = (*cost_function)(&cost);
if (ret_val != SA_OK) {
(void) fprintf(Ferr, "Panic: error in determine cost function\n");
(void) exit(1);
}
return (cost);
} /* end of determine_cost */
generate_new_state(range, generate_function)
int range;
int (*generate_function)();
{
int ret_val;
ret_val = (*generate_function)(range);
if (ret_val != SA_OK) {
(void) fprintf(Ferr, "Panic: error in generate new state function\n");
(void) exit(1);
}
} /* end of generate_new_state */
double
random_generator(left,right)
int left,right;
{
double range, divide, rnum, number;
rnum = (double) random();
divide = 2147483647;
number = rnum/divide;
range = (double) right - left;
number = number*range + left;
return(number);
} /* end of random_generator */
int int_random_generator(x,y)
int x, y;
{
int i;
i = (int) floor(random_generator(x,y+1));
if (i > y) {
return y;
} else {
return i;
}
} /* end of int_random_generator */