summaryrefslogtreecommitdiff
path: root/base/anneal.c
blob: c24136fedeb4245b8270bddeae6aa8f9baffe475 (plain)
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
/*
     NETGEN  --  Copyright 1989, Massimo A. Sivilotti, Caltech
*/

#include "config.h"

#include <stdio.h>
#include <math.h>

#include "hash.h"
#include "objlist.h"
#include "embed.h"
#include "timing.h"
#include "print.h"
#include "dbug.h"

int MaxCPUTime = 100;  /* max number of seconds to burn */

#define MAX_SIM_ANNEAL_ITER 10
#define MAX_CHANGES_PER_ITER 2

#if 0
float RandomUniform(void)
{
  /* DANGER! DANGER! Non-portable code below!! */
  return ((float)(rand()) / ((1<<15) - 1.0));
}
#endif

int GenerateAnnealPartition(int left, int right, int level)
/* tries to find a balanced partition, as far as leaf cell usage */
{
  int i;
  int IncludedElements, partition;
  int ChangesMade, Iterations;
  int leftfanout, rightfanout;
  float T;

  level++;  /* just to keep to compiler from whining about unused parameter */

  IncludedElements = (right - left) / 2;
  partition = left + IncludedElements - 1;

  /* don't actually use {left,right}fanout, but need to 
     initialize leftnodes and rightnodes arrays */

  leftfanout = PartitionFanout(left, partition, LEFT);
  rightfanout = PartitionFanout(partition + 1, right, RIGHT);

#if 0
  /* someday, need an escape clause here */
  if (leftfanout <= TreeFanout[level] && rightfanout <= TreeFanout[level])
	  return(partition);
#endif

Printf("called generateannealpartition with left = %d, right = %d\n",left,right);
  /* actually do the annealing now */
  T = 3.0;
  do {
    int el1, el2;
    int delta;

    Iterations = 0;
    ChangesMade = 0;
    do {
      el1 = Random(partition - left + 1) + left;
      el2 = Random(right - partition) + partition + 1;
      Iterations++;

      delta = 0;
      for (i = 1; i <= Nodes; i++) {
	/* could do the following test with XOR */
	if (CSTAR[permutation[el1]][i] == 0 && CSTAR[permutation[el2]][i] == 0)
	  continue;
	if (CSTAR[permutation[el1]][i] != 0 && CSTAR[permutation[el2]][i] != 0)
	  continue;

	/* only one of these is non-zero */
	if (CSTAR[permutation[el1]][i] != 0) {
	  if (rightnodes[i] == 0) {
	    /* things are not good unless all the fanout is captured in el1 */
	    if (CSTAR[permutation[el1]][i] != leftnodes[i]) delta++;
	  }
	  else {
	    /* things are good if all fanout is captured in el1 */
	    if (CSTAR[permutation[el1]][i] == leftnodes[i]) delta--;
	  }
	}
	else {
	  if (leftnodes[i] == 0) {
	    /* things are not good unless all the fanout is captured in el2 */
	    if (CSTAR[permutation[el2]][i] != rightnodes[i]) delta++;
	  }
	  else {
	    /* things are good if all fanout is captured in el2 */
	    if (CSTAR[permutation[el2]][i] == rightnodes[i]) delta--;
	  }
	}
      }
DBUG_EXECUTE("place",
Printf("\n");
Printf("considering swapping %d and %d\n",permutation[el1],permutation[el2]);
Printf("E1: "); 
for (i = 1; i <= Nodes; i++) Printf("%2d ",CSTAR[permutation[el1]][i]);
Printf("\nL:  ");
for (i = 1; i <= Nodes; i++) Printf("%2d ",leftnodes[i]);
Printf("\nE2: ");
for (i = 1; i <= Nodes; i++) Printf("%2d ",CSTAR[permutation[el2]][i]);
Printf("\nR:  ");
for (i = 1; i <= Nodes; i++) Printf("%2d ",rightnodes[i]);
Printf("\nC0: ");
for (i = 1; i <= Nodes; i++) Printf("%2d ",CSTAR[0][i]);
Printf("\ndelta = %d\n", delta);
);

      if (delta < 0 || exp(-delta / T) > RandomUniform()) {
	int tmp;

	if (delta < 0) ChangesMade++;
	/* update the {left, right}nodes arrays */
	for (i = 1; i <= Nodes; i++) {
	   leftnodes[i] +=
		 CSTAR[permutation[el2]][i] - CSTAR[permutation[el1]][i];
	   rightnodes[i] -=
		 CSTAR[permutation[el2]][i] - CSTAR[permutation[el1]][i];
        }
	/* now swap the elements */
DBUG_EXECUTE("place",
        Printf("swapping elements %d and %d\n",
	       permutation[el1],permutation[el2]);
	);
	tmp = permutation[el1];
	permutation[el1] = permutation[el2];
	permutation[el2] = tmp;
      }
      
    } while (ChangesMade <= MAX_CHANGES_PER_ITER && 
	    Iterations < MAX_SIM_ANNEAL_ITER);
    T = 0.90 * T;
Printf("decreasing T to %.2f after %d iterations.\n",T,Iterations);
  } while (ChangesMade > 0);

  return (partition);
}

int AnnealPartition(int left, int right, int level)
/* return index of new element, if successful partition has been found */
{
  int partition;
  int iterations;
  int found;
  int OriginalNewN;
  int leftelement, rightelement;

#define MAX_PARTITION_ITERATIONS 10

  DBUG_ENTER("AnnealPartition");
  OriginalNewN = NewN;
  if (level < LEVEL(permutation[left])) {
    Fprintf(stdout,"Failed at level %d; subtree too deep\n",level);
    DBUG_RETURN(0);
  }

  if (left == right) DBUG_RETURN(permutation[left]);

  if (right - left == 1) {
	  /* don't bother annealing, just add it to the list */
	  AddNewElement(permutation[left], permutation[right]);
	  DBUG_RETURN(NewN);    
  }
  /* use simulated annealing to gather about 1/2 of the elements,
     Then check to see if it is valid.
     */

  DBUG_PRINT("place",("trying to partition %d, %d",left,right)); 
  iterations = 0;
  do {
    int i;
    int leftfanout, rightfanout;

    iterations++;
    partition = GenerateAnnealPartition(left, right, level);
    if (partition == 0) DBUG_RETURN(0); /* no valid partition */

    found = 0;
    leftfanout = PartitionFanout(left,partition,LEFT);
    rightfanout = PartitionFanout(partition+1, right, RIGHT);
    if (leftfanout <= TreeFanout[level] && rightfanout <= TreeFanout[level])
	    found = 1;

    if (!found || level > TopDownStartLevel - 2) {
      for (i = MAX_TREE_DEPTH; i > level; i--) Fprintf(stdout, "   ");
      Fprintf(stdout,
    "Level: %d; L (%d leaves) fanout %d; R (%d leaves) fanout %d (<= %d) %s\n",
	      level, (partition - left + 1), leftfanout, 
	      (right - partition), rightfanout, TreeFanout[level],
	      found ? "SUCCESSFUL" : "UNSUCCESSFUL");
    }

#if 0
    if (!found) { 
      int IterationLimit;

      for (IterationLimit = 0; IterationLimit < 20 &&
	   GradientDescent(left, right, partition); IterationLimit++);

      found = 0; 
      leftfanout =  PartitionFanout(left,partition,LEFT); 
      rightfanout = PartitionFanout(partition+1, right, RIGHT); 
      if (leftfanout <= TreeFanout[level] && rightfanout <= TreeFanout[level])
	      found = 1;

      for (i = MAX_TREE_DEPTH; i > level; i--) Fprintf(stdout, "   ");
      Fprintf(stdout,
	      "       Iteration %2d: L fanout %d; R fanout %d (<= %d) %s\n",
	      iterations, leftfanout, rightfanout, TreeFanout[level],
	      found ? "SUCCESSFUL" : "UNSUCCESSFUL");
    }
#endif

    DBUG_EXECUTE("place", 
		 Fprintf(DBUG_FILE,"Level %d: ",level);
		 Dbug_print_cells(left,partition);
		 Dbug_print_cells(partition+1,right);
		 Fprintf(DBUG_FILE,"\n");
		 {
		   int i;
		   Fprintf(DBUG_FILE,"L ");
		   for (i = 1; i <= Nodes; i++) 
		     Fprintf(DBUG_FILE,"%2d ",leftnodes[i]);
		   Fprintf(DBUG_FILE,"\nR ");
		   for (i = 1; i <= Nodes; i++) 
		     Fprintf(DBUG_FILE,"%2d ",rightnodes[i]);
		   Fprintf(DBUG_FILE,"\n");
		 }
		 Fprintf(DBUG_FILE, "%s\n", found?"SUCCESSFUL":"UNSUCCESSFUL");
		 );
  } while (iterations < MAX_PARTITION_ITERATIONS && !found);
  if (!found) {
    Fprintf(stdout,"Failed embedding at level %d; no partition\n",level);
    goto fail;
  }
  leftelement = AnnealPartition(left, partition, level-1);
  if (leftelement == 0) goto fail;
  rightelement = AnnealPartition(partition+1, right, level-1);
  if (rightelement == 0) goto fail;

  /* add it to the list */
  AddNewElement(leftelement, rightelement);
  DBUG_RETURN(NewN);    

 fail:
  NewN = OriginalNewN;
  DBUG_RETURN(0);
}