int main( int argc, char **argv ) { struct state *s = parse( argv[1] ); #ifdef DEBUG state_print( s ); #endif state_free( s ); return 0; }
/* * eq: equlibirium factor * ti: temperature initial * te: temperature end * cf: cooling factor (0 < cf < 1) */ uint32_t simulated_annealing(sat_t *sat, double te, double steps) { state_t *state = state_init(sat->vars_cnt, STATE_RANDOMIZE); state_t *state_next = state_init(sat->vars_cnt, STATE_ALL_0); uint32_t ret = 0; uint32_t real_cost = 0; uint32_t eq; /* equlibrium */ double cf; /* cooling factor */ double ti; /* temperature initial */ ti = sat->vars_cnt * sat->weight_max * 10; cf = pow(te / ti, 1 / (steps - 1)); // cf = 1 - ((ti - te) / steps); static bool print_once = false; //true; uint32_t it = 0; // printf("ti=%lf te=%lf cf=%lf, eq=%d\n", ti, te, cf, eq); // for (double t = ti; te < t; t *= cf) { // for (int i = 0; i < eq; i++) { // state_gen_next(state, state_next); // double d = ((double) cost(sat, state)) // - ((double) cost(sat, state_next)); // // if (d < 0 || randd() < pow(M_E, -d / t)) { // state_swap(&state, &state_next); // continue; // } // // //printf("it= %4u best= %4u bits= ", it++, cost(sat, state)); // // state_print(state); // } // } //http://www.cs.ubc.ca/~hoos/SATLIB/benchm.html eq = (uint32_t) (sat->vars_cnt / (double) 2.0); uint32_t *p = calloc(sat->vars_cnt, sizeof(uint32_t)); for (double t = ti; te < t; t *= cf) { permutation(p, sat->vars_cnt); it++; // printf("t=%lf\n", t); for (uint32_t i = 0; i < eq; i++) { double c = cost(sat, state); uint32_t ind = p[i]; state->ch[ind] = (state->ch[ind] + 1) % 2; double d = (c - (double) cost(sat, state)); if (d < 0 || randd() < pow(M_E, -d / t)) { continue; } state->ch[ind] = (state->ch[ind] + 1) % 2; } real_cost = cost_main(sat, state); if (g_print_progress) { printf("%u %u %lf\n", it, real_cost, cost(sat, state)); } ret = max(ret, real_cost); } if (print_once) { fprintf(stderr, "ti=%lf cf=%lf it=%u eq=%u\n", ti, cf, it, eq); assert(0 < cf && cf < 1); print_once = false; state_print(state); } free(p); state_free(state); state_free(state_next); return (ret); }
//Function to simulate the twin lifts. //VARIABLE EXPLANATION:- // qf: array of array of queues // mq: main queue which contains people int their order of ariival. // num_floors: total number of floors. void Simulate(pqueue ** qf, pqueue * mq, int num_floors) { State * lift1 = State_new(0, num_floors, 0, 1, 1); State * lift2 = State_new(num_floors, num_floors, 0, -1, 2); person * p; for(p=mq->head;p!=NULL;p=p->mainnext) { if(p->time!=-1) continue; int reserved = CalculateNumPersons(qf[p->floor_arrival]); int a1 = lift1->floor-p->floor_arrival; int a2 = lift2->floor-p->floor_arrival; if(a1<0)a1*=-1; if(a2<0)a2*=-1; a1*=FLOOR_TIME; a2*=FLOOR_TIME; if(a1+lift1->time<=a2+lift2->time) { GoTo(lift1->floor, p->floor_arrival, lift1, qf, num_floors, reserved); Pickup(p->floor_arrival, lift1, qf); Drop(lift1->floor, p->floor_dest, lift1, qf, num_floors); } else { GoTo(lift2->floor, p->floor_arrival, lift2, qf, num_floors, reserved); Pickup(p->floor_arrival, lift2, qf); Drop(lift2->floor, p->floor_dest, lift2, qf, num_floors); } printf("Lift 1: %d %d\n",lift1->time,lift1->floor); printf("Lift 2: %d %d\n",lift2->time,lift2->floor); } lift1->num_people-=lift1->dst[lift1->floor]; lift2->num_people-=lift2->dst[lift2->floor]; if(lift1->num_people==0 && lift2->num_people==0) printf("\nSuccessfuly simulated!!\n\n"); int j; lift1->place_time[lift1->time+1] = lift1->floor; lift2->place_time[lift2->time+1] = lift2->floor; findfloors(lift1->place_time,lift1->time+1); findfloors(lift2->place_time,lift2->time+1); /*printf("\nLift1 : \n"); for(j=0;j<=lift1->time;++j) { printf("%d %.1f\n",j,lift1->place_time[j]); } printf("\nLift2 : \n"); for(j=0;j<=lift2->time;++j) { printf("%d %.1f\n",j,lift2->place_time[j]); }*/ state_print(qf, num_floors, lift1, lift2, lift1->time>lift2->time?lift1->time:lift2->time); }