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 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; }