TreeNode* string2tree(const string& s){ stringstream ss(s); string val; list<string> L; while(ss>>val)L.push_back(val); return list2tree(L); }
void lisp2tree(tree<int> &A,const string &s) { list<int> L; int EP = INT_MAX, BP=INT_MAX-1; const char *p = s.c_str(); char num[100]; while (true) { if (*p == '\0') break; else if (*p == '(') { L.push_back(BP); p++; } else if (*p == ')') { L.push_back(EP); p++; } else if (*p == ' ') p++; else if (*p>='0' && *p<='9') { char *pn=num; *pn=*p; while (*p>='0' && *p<='9') { *pn++ = *p++; } *++pn='\0'; int n = atoi(num); L.push_back(n); } else p++; } // list<int>::iterator q = L.begin(); // while (q!=L.end()) cout << *q++ << " "; // cout << endl; A.clear(); list2tree(A,L,BP,EP); }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*>---:---<*>---: iterator_t list2tree(tree &T, iterator_t n, const list<elem_t> &L, list<elem_t>::const_iterator &p, elem_t BP,elem_t EP) { if (*p != BP) n = T.insert(n,*p); else { list<elem_t>::const_iterator q = p; q++; assert(*q != BP && *q != EP); n = T.insert(n,*q++); iterator_t r = n.lchild(); while (*q != EP) { r = list2tree(T,r,L,q,BP,EP); r = r.right(); } p = q; } p++; return n; }
//---:---<*>---:---<*>---:---<*>---:---<*>---:---<*>---:---<*>---: iterator_t list2tree(tree &A,const list<elem_t> &L,elem_t BP,elem_t EP) { list<elem_t>::const_iterator p = L.begin(); return list2tree(A,A.begin(),L,p,BP,EP); }
void anneal(NET * net_arr, MODULE * module_arr ){ int i1, i2; /* pair of items to swap */ int i,j; /* counters */ double temperature; /* the current system temp */ long current_value; /* value of current state */ long start_value; /* value at start of loop */ int delta; /* value after swap */ double merit, flip; /* hold swap accept conditions*/ double exponent; /* exponent for energy funct*/ FPTREE * tempnodes[3 * OPERATOR_LIMIT]; // double random_float(); //double solution_cost(); //double transition(); temperature = INITIAL_TEMPERATURE; current_value = solution_cost(solution, net_arr, module_arr); for (i=1; i<=COOLING_STEPS; i++) { temperature *= COOLING_FRACTION; start_value = current_value; for (j=1; j<=STEPS_PER_TEMP; j++) { current_value = solution_cost(solution, net_arr, module_arr); fprintf(stderr, "current value: %ld\n", current_value); int randint1 = random_int(0, 196); int type = (nodes[randint1]->operator == 0 ? 1 : 0); delta = transition( net_arr, module_arr, randint1 ); // fprintf(stderr, "delta: %d\n", delta); flip = rand()/(RAND_MAX - 1.0);//random_float(0.0,1.0); exponent = (-delta)/(K * temperature); merit = pow(E,exponent); /*printf("merit = %f flip=%f exponent=%f\n",merit,flip,exponent); */ /*if (merit >= 1.0) merit = 0.0;*/ /* don't do unchanging swaps*/ if(delta < 0) { /*ACCEPT-WIN choose a better solution*/ //current_value = current_value + delta; if (TRACE_OUTPUT) { fprintf(stderr, "swap WIN %d value %ld temp=%f \n", delta, solution_cost(solution,net_arr, module_arr),temperature); fprintf(stderr, "\n\n"); } }else{if(merit >= flip){ /*ACCEPT-LOSS choose a worse solution*/ //current_value = current_value+delta; if (TRACE_OUTPUT) { fprintf(stderr, "swap LOSS %d value %ld merit=%f flip=%f\n", delta,solution_cost(solution,net_arr, module_arr) , merit, flip) ; fprintf(stderr, "\n\n") ; } }else{ /* REJECT */ if(type == 0){ int i ; for(i = randint1; ; i--){ if(nodes[i] == NULL || nodes[i]->operator == 0){ break; }else{ if(nodes[i]->operator == 'H'){ nodes[i]->operator = 'V'; }else{if(nodes[i]->operator == 'V'){ nodes[i]->operator = 'H'; }} } } for(i = randint1; ; i++){ if(nodes[i] == NULL || nodes[i]->operator == 0){ break; }else{ if(nodes[i]->operator == 'H'){ nodes[i]->operator = 'V'; }else{if(nodes[i]->operator == 'V'){ nodes[i]->operator = 'H'; }} } } }else{if(type == 1){ FPTREE * temp ; temp = nodes[randint1] ; nodes[randint1] = nodes[randint1 + 1]; nodes[randint1 + 1] = temp ; }} solution = list2tree(nodes); iter_update_tree(solution); iter_update_module(0, 0, solution, module_arr); }} solution_count_update(solution, net_arr, module_arr) ; } if((current_value-start_value) > 0.0){ // rerun at this temp temperature /= COOLING_FRACTION; if (TRACE_OUTPUT) {printf("rerun at temperature %f\n",temperature);} } } }