int main() { srand(time(NULL)); getInputs(); loadFile(); if(algorithmChoice == 0) { for(int j = 0; j < 5; j++) { vector<TurnPointChrom*>* chromPool = new vector<TurnPointChrom*>(); for(int i = 0; i < poolSize; i++) chromPool->push_back(new TurnPointChrom(mapSize, collisionMap)); vector<TurnPointChrom*>* endChroms = mainGALoop(chromPool); printResults(endChroms); for(int i = 0; i < poolSize; i++) delete endChroms->at(i); delete endChroms; } } else { TurnPointChrom* result = annealing(); printResults(result); delete result; } for(int i = 0; i < mapSize; i++) delete collisionMap[i]; delete collisionMap; }
int main(int argc, const char * argv[]) { Data data("/Users/ivanov/Dropbox/projects/annealing/annealing/in/inst1.txt"); if(argc > 1) { std::cout<<"first param must be name of file" <<std::endl; Data data(argv[1]); //return 0; } //data.dataOut(); Annealing annealing(data); //Location init(&data); //double length[] = {0.5, 1, 2, 4, 8, 16}; Move move = Move::Move(data); //std::cout<<"\a"; /*for(int i = 0; i < 6; i++) { //std::cout<<"Length = "<<length[i]<<std::endl; for(int j = 0; j < 1; j++) { clock_t clockStart = clock(); Location result = annealing.find(1000, 0.97, move, 10, 2, Location(data)); clock_t clockEnd = clock(); std::cout<<result.calc()<<std::endl; std::cout<<(clockEnd - clockStart) / CLOCKS_PER_SEC<<std::endl; std::cout<<"\a"; } }*/ //Location result = Location(data); Location result = annealing.find(1000, 0.97, move, 10, 2 , Location(data)); std::cout<<result.calc()<<std::endl; /*if(result.allow()) { std::cout<<"allow"<<std::endl; } else { std::cout<<"NOTallow"<<std::endl; }*/ return 0; }
int find_tsp_solution(int num, DTYPE *cost, int *ids, int start, int end, DTYPE *total_len, char *err_msg) { int i, j; int istart = 0; int jstart = 0; int iend = -1; int jend = -1; int rev = 0; TSP tsp; int64_t seed = -314159L; DTYPE blength; PGR_DBG("sizeof(int64_t)=%d", (int)sizeof(int64_t)); initRand((int) seed); #ifdef DEBUG char bufff[2048]; int nnn; PGR_DBG("---------- Matrix[%d][%d] ---------------------\n", num, num); for (i = 0; i < num; i++) { sprintf(bufff, "%d:", i); nnn = 0; for (j = 0; j < num; j++) { nnn += sprintf(bufff + nnn, "\t%.4f", cost[i * num + j]); } PGR_DBG("%s", bufff); } #endif /* initialize tsp struct */ tsp.n = num; tsp.dist = NULL; tsp.iorder = NULL; tsp.jorder = NULL; tsp.border = NULL; if (!(tsp.iorder =(int*) palloc((size_t) tsp.n * sizeof(int))) || !(tsp.jorder =(int*) palloc((size_t) tsp.n * sizeof(int))) || !(tsp.border =(int*) palloc((size_t) tsp.n * sizeof(int))) ) { elog(FATAL, "Memory allocation failed!"); return -1; } tsp.dist = cost; tsp.maxd = 0; for (i=0; i < tsp.n * tsp.n; i++) { tsp.maxd = MAX(tsp.maxd, cost[i]); } /* identity permutation */ for (i = 0; i < tsp.n; i++) tsp.iorder[i] = i; tsp.bestlen = pathLength(&tsp); for (i = 0; i < tsp.n; i++) tsp.border[i] = tsp.iorder[i]; PGR_DBG("Initial Path Length: %.4f", tsp.bestlen); /* * Set up first eulerian path iorder to be improved by * simulated annealing. */ if (findEulerianPath(&tsp)) return -1; blength = pathLength(&tsp); if (blength < tsp.bestlen) { tsp.bestlen = blength; for (i = 0; i < tsp.n; i++) tsp.border[i] = tsp.iorder[i]; } PGR_DBG("Approximated Path Length: %.4f", blength); annealing(&tsp); *total_len = pathLength(&tsp); PGR_DBG("Final Path Length: %.4f", *total_len); *total_len = tsp.bestlen; for (i = 0; i < tsp.n; i++) tsp.iorder[i] = tsp.border[i]; PGR_DBG("Best Path Length: %.4f", *total_len); // reorder ids[] with start as first #ifdef DEBUG for (i = 0; i < tsp.n; i++) { PGR_DBG("i: %d, ids[i]: %d, io[i]: %d, jo[i]: %d, jo[io[i]]: %d", i, ids[i], tsp.iorder[i], tsp.jorder[i], tsp.jorder[tsp.iorder[i]]); } #endif // get index of start node in ids for (i=0; i < tsp.n; i++) { if (ids[i] == start) istart = i; if (ids[i] == end) iend = i; } PGR_DBG("istart: %d, iend: %d", istart, iend); // get the idex of start in iorder for (i=0; i < tsp.n; i++) { if (tsp.iorder[i] == istart) jstart = i; if (tsp.iorder[i] == iend) jend = i; } PGR_DBG("jstart: %d, jend: %d", jstart, jend); /* * If the end is specified and the end point and it follow start * then we swap start and end and extract the list backwards * and later we reverse the list for the desired order. */ if ((jend > 0 && jend == jstart+1) ||(jend == 0 && jstart == tsp.n-1)) { int tmp = jend; jend = jstart; jstart = tmp; rev = 1; PGR_DBG("reversed start and end: jstart: %d, jend: %d", jstart, jend); } // copy ids to tsp.jorder so we can rewrite ids memcpy(tsp.jorder, ids, (size_t) tsp.n * sizeof(int)); // write reordered ids into ids[] // remember at this point jorder is our list if ids for (i=jstart, j = 0; i < tsp.n; i++, j++) ids[j] = tsp.jorder[tsp.iorder[i]]; for (i=0; i < jstart; i++, j++) ids[j] = tsp.jorder[tsp.iorder[i]]; // if we reversed the order above, now put it correct. if (rev) { int tmp = jend; jend = jstart; jstart = tmp; reverse(tsp.n, ids); } #ifdef DEBUG PGR_DBG("ids getting returned!"); for (i=0; i < tsp.n; i++) { PGR_DBG("i: %d, ids[i]: %d, io[i]: %d, jo[i]: %d", i, ids[i], tsp.iorder[i], tsp.jorder[i]); } #endif PGR_DBG("tsplib: jstart=%d, jend=%d, n=%d, j=%d", jstart, jend, tsp.n, j); return 0; }