void ReadProblem() { FreeStructures(); FirstNode = 0; C = c = 0; /***************************** my code start**************************/ Name = (char *) malloc(strlen("problem.atsp") + 1); Type = (char *) malloc(strlen("ATSP") + 1); ProblemType = ATSP; DimensionSaved = Dimension = Rp0->VVnum; Read_EDGE_WEIGHT_SECTION(); WeightType = -1; Swaps = 0; /* Adjust parameters */ MaxSwaps = Dimension; if (MaxCandidates > Dimension - 1) MaxCandidates = Dimension - 1; if (AscentCandidates > Dimension - 1) AscentCandidates = Dimension - 1; InitialPeriod = Dimension / 2; if (InitialPeriod < 100) InitialPeriod = 100; Excess = 1.0 / Dimension; MaxTrials = Dimension; MakeHeap(Dimension); C = C_FUNCTION ; D = D_FUNCTION; MoveFunction Movefun[5] = {Best2OptMove,Best2OptMove,Best3OptMove,Best4OptMove,Best5OptMove}; BestMove = BestSubsequentMove = Movefun[MoveType-1]; }
void init(){ FreeStructures(); FirstNode = 0; WeightType = EXPLICIT; WeightFormat = FULL_MATRIX; ProblemType = ATSP; CoordType = NO_COORDS; // Name = "Unnamed"; Type = EdgeWeightType = EdgeWeightFormat = 0; EdgeDataFormat = NodeCoordType = DisplayDataType = 0; Distance = 0; C = 0; c = 0; Distance = Distance_EXPLICIT; DimensionSaved = Dimension = TSP_N; init_graph(); Swaps = 0; /* Adjust parameters */ if (Seed == 0) Seed = (unsigned)time(0); if (Precision == 0) Precision = 100; if (InitialStepSize == 0) InitialStepSize = 1; if (MaxSwaps < 0) MaxSwaps = Dimension; if (KickType > Dimension / 2) KickType = Dimension / 2; if (Runs == 0) Runs = 10; if (MaxCandidates > Dimension - 1) MaxCandidates = Dimension - 1; if (ExtraCandidates > Dimension - 1) ExtraCandidates = Dimension - 1; if (SubproblemSize >= Dimension) SubproblemSize = Dimension; else if (SubproblemSize == 0) { if (AscentCandidates > Dimension - 1) AscentCandidates = Dimension - 1; if (InitialPeriod < 0) { InitialPeriod = Dimension / 2; if (InitialPeriod < 100) InitialPeriod = 100; } if (Excess < 0) Excess = 1.0 / Dimension; if (MaxTrials == -1) MaxTrials = Dimension; MakeHeap(Dimension); } if (CostMatrix == 0 && Dimension <= MaxMatrixDimension && Distance != 0 && Distance != Distance_1 && Distance != Distance_ATSP) { Node *Ni, *Nj; assert(CostMatrix = (int *)calloc((size_t)Dimension * (Dimension - 1) / 2, sizeof(int))); Ni = FirstNode->Suc; do { Ni->C = &CostMatrix[(size_t)(Ni->Id - 1) * (Ni->Id - 2) / 2] - 1; if (ProblemType != HPP || Ni->Id < Dimension) for (Nj = FirstNode; Nj != Ni; Nj = Nj->Suc) Ni->C[Nj->Id] = Fixed(Ni, Nj) ? 0 : Distance(Ni, Nj); else for (Nj = FirstNode; Nj != Ni; Nj = Nj->Suc) Ni->C[Nj->Id] = 0; } while ((Ni = Ni->Suc) != FirstNode); WeightType = EXPLICIT; c = 0; } if (Precision > 1 && (WeightType == EXPLICIT || ProblemType == ATSP)) { int j, n = ProblemType == ATSP ? Dimension / 2 : Dimension; for (int i = 2; i <= n; i++) { Node *N = &NodeSet[i]; for (j = 1; j < i; j++) if (N->C[j] * Precision / Precision != N->C[j]) { printf("PRECISION (= %d) is too large", Precision); system("pause"); } } } C = WeightType == EXPLICIT ? C_EXPLICIT : C_FUNCTION; D = WeightType == EXPLICIT ? D_EXPLICIT : D_FUNCTION; if (SubsequentMoveType == 0) SubsequentMoveType = MoveType; int K = MoveType >= SubsequentMoveType || !SubsequentPatching ? MoveType : SubsequentMoveType; if (PatchingC > K) PatchingC = K; if (PatchingA > 1 && PatchingA >= PatchingC) PatchingA = PatchingC > 2 ? PatchingC - 1 : 1; if (NonsequentialMoveType == -1 || NonsequentialMoveType > K + PatchingC + PatchingA - 1) NonsequentialMoveType = K + PatchingC + PatchingA - 1; if (PatchingC >= 1 && NonsequentialMoveType >= 4) { BestMove = BestSubsequentMove = BestKOptMove; if (!SubsequentPatching && SubsequentMoveType <= 5) { MoveFunction BestOptMove[] = { 0, 0, Best2OptMove, Best3OptMove, Best4OptMove, Best5OptMove }; BestSubsequentMove = BestOptMove[SubsequentMoveType]; } } else { MoveFunction BestOptMove[] = { 0, 0, Best2OptMove, Best3OptMove, Best4OptMove, Best5OptMove }; BestMove = MoveType <= 5 ? BestOptMove[MoveType] : BestKOptMove; BestSubsequentMove = SubsequentMoveType <= 5 ? BestOptMove[SubsequentMoveType] : BestKOptMove; } if (ProblemType == HCP || ProblemType == HPP) MaxCandidates = 0; //if (TraceLevel >= 1) { // printff("done\n"); // PrintParameters(); //} }
ReturnFlag LKH::LKHAlg::run_() { GainType Cost, OldOptimum; double Time, LastTime = GetTime(); if (SubproblemSize > 0) { if (DelaunayPartitioning) SolveDelaunaySubproblems(); else if (KarpPartitioning) SolveKarpSubproblems(); else if (KCenterPartitioning) SolveKCenterSubproblems(); else if (KMeansPartitioning) SolveKMeansSubproblems(); else if (RohePartitioning) SolveRoheSubproblems(); else if (MoorePartitioning || SierpinskiPartitioning) SolveSFCSubproblems(); else SolveTourSegmentSubproblems(); } AllocateStructures(); CreateCandidateSet(); InitializeStatistics(); if (Norm != 0) BestCost = PLUS_INFINITY; else { /* The ascent has solved the problem! */ Optimum = BestCost = (GainType) LowerBound; UpdateStatistics(Optimum, GetTime() - LastTime); RecordBetterTour(); RecordBestTour(); WriteTour(OutputTourFileName, BestTour, BestCost); WriteTour(TourFileName, BestTour, BestCost); Runs = 0; } /* Find a specified number (Runs) of local optima */ for (Run = 1; Run <= Runs; Run++) { LastTime = GetTime(); Cost = FindTour(); /* using the Lin-Kernighan heuristic */ if (*MaxPopulationSize > 1) { /* Genetic algorithm */ int i; for (i = 0; i < *PopulationSize; i++) { GainType OldCost = Cost; Cost = MergeTourWithIndividual(i,this); if (TraceLevel >= 1 && Cost < OldCost) { printff(" Merged with %d: Cost = " GainFormat, i + 1, Cost); if (Optimum != MINUS_INFINITY && Optimum != 0) printff(", Gap = %0.4f%%", 100.0 * (Cost - Optimum) / Optimum); printff("\n"); } } if (!HasFitness(Cost)) { if (*PopulationSize < *MaxPopulationSize) { AddToPopulation(Cost,this); if (TraceLevel >= 1) PrintPopulation(this); } else if (Cost < Fitness.get()[*PopulationSize - 1]) { i = ReplacementIndividual(Cost,this); ReplaceIndividualWithTour(i, Cost,this); if (TraceLevel >= 1) PrintPopulation(this); } } } else if (Run > 1) Cost = MergeBetterTourWithBestTour(); if (Cost < BestCost) { BestCost = Cost; RecordBetterTour(); RecordBestTour(); WriteTour(OutputTourFileName, BestTour, BestCost); WriteTour(TourFileName, BestTour, BestCost); } OldOptimum = Optimum; if (Cost < Optimum) { if (FirstNode->InputSuc) { Node *N = FirstNode; while ((N = N->InputSuc = N->Suc) != FirstNode); } Optimum = Cost; printff("*** New optimum = " GainFormat " ***\n\n", Optimum); } Time = fabs(GetTime() - LastTime); UpdateStatistics(Cost, Time); if (TraceLevel >= 1 && Cost != PLUS_INFINITY) { printff("Run %d: Cost = " GainFormat, Global::msp_global->m_runId, Cost); if (Optimum != MINUS_INFINITY && Optimum != 0) printff(", Gap = %0.4f%%", 100.0 * (Cost - Optimum) / Optimum); // printff(", Time = %0.2f sec. %s\n\n", Time, // Cost < Optimum ? "<" : Cost == Optimum ? "=" : ""); printff(", Time = %0.2f sec. \n", Time); } if (StopAtOptimum && Cost == OldOptimum && *MaxPopulationSize >= 1) { Runs = Run; break; } if (*PopulationSize >= 2 && (*PopulationSize == *MaxPopulationSize || Run >= 2 * *MaxPopulationSize) && Run < Runs) { Node *N; int Parent1, Parent2; Parent1 = LinearSelection(*PopulationSize, 1.25,this); do Parent2 = LinearSelection(*PopulationSize, 1.25,this); while (Parent2 == Parent1); ApplyCrossover(Parent1, Parent2,this); N = FirstNode; do { int d = (this->*C)(N, N->Suc); AddCandidate(N, N->Suc, d, INT_MAX); AddCandidate(N->Suc, N, d, INT_MAX); N = N->InitialSuc = N->Suc; } while (N != FirstNode); } mv_cost[Global::msp_global->m_runId]=Cost; SRandom(++Seed); } // PrintStatistics(); freeAll(); FreeStructures(); return Return_Terminate; }
void ReadProblem() { int i, K; char *Line, *Keyword; if (!(ProblemFile = fopen(ProblemFileName, "r"))) eprintf("Cannot open PROBLEM_FILE: \"%s\"", ProblemFileName); if (TraceLevel >= 1) printff("Reading PROBLEM_FILE: \"%s\" ... ", ProblemFileName); FreeStructures(); FirstNode = 0; WeightType = WeightFormat = ProblemType = -1; CoordType = NO_COORDS; Name = Copy("Unnamed"); Type = EdgeWeightType = EdgeWeightFormat = 0; EdgeDataFormat = NodeCoordType = DisplayDataType = 0; Distance = 0; C = 0; c = 0; while ((Line = ReadLine(ProblemFile))) { if (!(Keyword = strtok(Line, Delimiters))) continue; for (i = 0; i < (int) strlen(Keyword); i++) Keyword[i] = (char) toupper(Keyword[i]); if (!strcmp(Keyword, "COMMENT")); else if (!strcmp(Keyword, "DEMAND_SECTION")) eprintf("Not implemented: %s", Keyword); else if (!strcmp(Keyword, "DEPOT_SECTION")) eprintf("Not implemented: %s", Keyword); else if (!strcmp(Keyword, "DIMENSION")) Read_DIMENSION(); else if (!strcmp(Keyword, "DISPLAY_DATA_SECTION")) Read_DISPLAY_DATA_SECTION(); else if (!strcmp(Keyword, "DISPLAY_DATA_TYPE")) Read_DISPLAY_DATA_TYPE(); else if (!strcmp(Keyword, "EDGE_DATA_FORMAT")) Read_EDGE_DATA_FORMAT(); else if (!strcmp(Keyword, "EDGE_DATA_SECTION")) Read_EDGE_DATA_SECTION(); else if (!strcmp(Keyword, "EDGE_WEIGHT_FORMAT")) Read_EDGE_WEIGHT_FORMAT(); else if (!strcmp(Keyword, "EDGE_WEIGHT_SECTION")) Read_EDGE_WEIGHT_SECTION(); else if (!strcmp(Keyword, "EDGE_WEIGHT_TYPE")) Read_EDGE_WEIGHT_TYPE(); else if (!strcmp(Keyword, "EOF")) break; else if (!strcmp(Keyword, "FIXED_EDGES_SECTION")) Read_FIXED_EDGES_SECTION(); else if (!strcmp(Keyword, "NAME")) Read_NAME(); else if (!strcmp(Keyword, "NODE_COORD_SECTION")) Read_NODE_COORD_SECTION(); else if (!strcmp(Keyword, "NODE_COORD_TYPE")) Read_NODE_COORD_TYPE(); else if (!strcmp(Keyword, "TOUR_SECTION")) Read_TOUR_SECTION(&ProblemFile); else if (!strcmp(Keyword, "TYPE")) Read_TYPE(); else eprintf("Unknown keyword: %s", Keyword); } Swaps = 0; /* Adjust parameters */ if (Seed == 0) Seed = (unsigned) time(0); if (Precision == 0) Precision = 100; if (InitialStepSize == 0) InitialStepSize = 1; if (MaxSwaps < 0) MaxSwaps = Dimension; if (KickType > Dimension / 2) KickType = Dimension / 2; if (Runs == 0) Runs = 10; if (MaxCandidates > Dimension - 1) MaxCandidates = Dimension - 1; if (ExtraCandidates > Dimension - 1) ExtraCandidates = Dimension - 1; if (SubproblemSize >= Dimension) SubproblemSize = Dimension; else if (SubproblemSize == 0) { if (AscentCandidates > Dimension - 1) AscentCandidates = Dimension - 1; if (InitialPeriod < 0) { InitialPeriod = Dimension / 2; if (InitialPeriod < 100) InitialPeriod = 100; } if (Excess < 0) Excess = 1.0 / Dimension; if (MaxTrials == -1) MaxTrials = Dimension; MakeHeap(Dimension); } if (CostMatrix == 0 && Dimension <= MaxMatrixDimension && Distance != 0 && Distance != Distance_1 && Distance != Distance_ATSP && Distance != Distance_SPECIAL) { Node *Ni, *Nj; assert(CostMatrix = (int *) calloc((size_t) Dimension * (Dimension - 1) / 2, sizeof(int))); Ni = FirstNode->Suc; do { Ni->C = &CostMatrix[(size_t) (Ni->Id - 1) * (Ni->Id - 2) / 2] - 1; if (ProblemType != HPP || Ni->Id < Dimension) for (Nj = FirstNode; Nj != Ni; Nj = Nj->Suc) Ni->C[Nj->Id] = Fixed(Ni, Nj) ? 0 : Distance(Ni, Nj); else for (Nj = FirstNode; Nj != Ni; Nj = Nj->Suc) Ni->C[Nj->Id] = 0; } while ((Ni = Ni->Suc) != FirstNode); WeightType = EXPLICIT; c = 0; } if (Precision > 1 && (WeightType == EXPLICIT || ProblemType == ATSP)) { int j, n = ProblemType == ATSP ? Dimension / 2 : Dimension; for (i = 2; i <= n; i++) { Node *N = &NodeSet[i]; for (j = 1; j < i; j++) if (N->C[j] * Precision / Precision != N->C[j]) eprintf("PRECISION (= %d) is too large", Precision); } } C = WeightType == EXPLICIT ? C_EXPLICIT : C_FUNCTION; D = WeightType == EXPLICIT ? D_EXPLICIT : D_FUNCTION; if (SubsequentMoveType == 0) SubsequentMoveType = MoveType; K = MoveType >= SubsequentMoveType || !SubsequentPatching ? MoveType : SubsequentMoveType; if (PatchingC > K) PatchingC = K; if (PatchingA > 1 && PatchingA >= PatchingC) PatchingA = PatchingC > 2 ? PatchingC - 1 : 1; if (NonsequentialMoveType == -1 || NonsequentialMoveType > K + PatchingC + PatchingA - 1) NonsequentialMoveType = K + PatchingC + PatchingA - 1; if (PatchingC >= 1 && NonsequentialMoveType >= 4) { BestMove = BestSubsequentMove = BestKOptMove; if (!SubsequentPatching && SubsequentMoveType <= 5) { MoveFunction BestOptMove[] = { 0, 0, Best2OptMove, Best3OptMove, Best4OptMove, Best5OptMove }; BestSubsequentMove = BestOptMove[SubsequentMoveType]; } } else { MoveFunction BestOptMove[] = { 0, 0, Best2OptMove, Best3OptMove, Best4OptMove, Best5OptMove }; BestMove = MoveType <= 5 ? BestOptMove[MoveType] : BestKOptMove; BestSubsequentMove = SubsequentMoveType <= 5 ? BestOptMove[SubsequentMoveType] : BestKOptMove; } if (ProblemType == HCP || ProblemType == HPP) MaxCandidates = 0; if (TraceLevel >= 1) { printff("done\n"); PrintParameters(); } else printff("PROBLEM_FILE = %s\n", ProblemFileName ? ProblemFileName : ""); fclose(ProblemFile); if (InitialTourFileName) ReadTour(InitialTourFileName, &InitialTourFile); if (InputTourFileName) ReadTour(InputTourFileName, &InputTourFile); if (SubproblemTourFileName && SubproblemSize > 0) ReadTour(SubproblemTourFileName, &SubproblemTourFile); if (MergeTourFiles >= 1) { free(MergeTourFile); assert(MergeTourFile = (FILE **) malloc(MergeTourFiles * sizeof(FILE *))); for (i = 0; i < MergeTourFiles; i++) ReadTour(MergeTourFileName[i], &MergeTourFile[i]); } free(LastLine); LastLine = 0; }