Skip to content

Commit

Permalink
Refactor elkai.c
Browse files Browse the repository at this point in the history
  • Loading branch information
fikisipi committed May 14, 2019
1 parent a82b6dc commit 51ece58
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions elkai/_elkai.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// - Make sure camel case is used everywhere
// - Use git submodule instead of a LKH copy

static int m_calculate(int *matrixBuff, int matrixLen, int runCount, int *tourBuff, int *tourN);
static int elk_invoke_solver(int *matrixBuff, int matrixLen, int runCount, int *tourBuff, int *tourN);

static PyObject *elk_solve(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -62,7 +62,7 @@ static PyObject *elk_solve(PyObject *self, PyObject *args)
// TODO: don't lose precision
matrixBuff[i] = justNumber_i;
}
int norm_result = m_calculate(matrixBuff, pyLen, runCount, tourBuff, &tourN);
int norm_result = elk_invoke_solver(matrixBuff, pyLen, runCount, tourBuff, &tourN);
free(matrixBuff);
PyObject *list = PyList_New(tourN);
for (i = 0; i < tourN; i++)
Expand All @@ -74,6 +74,7 @@ static PyObject *elk_solve(PyObject *self, PyObject *args)
free(tourBuff);
return list;
}

static char elk_docs[] =
"solve(x): Solve a TSP problem.\n";

Expand All @@ -95,6 +96,7 @@ PyMODINIT_FUNC PyInit__elkai(void)
}

static void LoadWeightMatrix(int *_wArr);

void ParseTour(int *outN, int *outBuff, int *Tour)
{
int i, j, n, Forwards;
Expand Down Expand Up @@ -122,6 +124,7 @@ void ParseTour(int *outN, int *outBuff, int *Tour)
}
*outN = bufIdx;
}

static void CreateNodes()
{
Node *Prev = 0, *N = 0;
Expand Down Expand Up @@ -170,11 +173,8 @@ static int FixEdge(Node *Na, Node *Nb)
return 1;
}

void ReadParameters(int runCount)
static void SetParameters(int runCount)
{
char *Line, *Keyword, *Token, *Name;
unsigned int i;

ProblemFileName = PiFileName = InputTourFileName =
OutputTourFileName = TourFileName = 0;
CandidateFiles = MergeTourFiles = 0;
Expand Down Expand Up @@ -243,7 +243,7 @@ void ReadParameters(int runCount)
// Set up actual values
Runs = runCount;
Precision = 50;
TourFileName = "aaa2.txt";
TourFileName = "";
}

void _elk_ReadProblem(int *myMatrix, int matrixLen)
Expand All @@ -265,6 +265,7 @@ void _elk_ReadProblem(int *myMatrix, int matrixLen)
// At the moment, this is hardcoded.
// Future elkai may allow different problem types, or
// different weight formats.

ProblemType = ATSP;
Dimension = (int)sqrt(matrixLen);
DimensionSaved = Dimension;
Expand Down Expand Up @@ -380,9 +381,7 @@ void _elk_ReadProblem(int *myMatrix, int matrixLen)
}
if (ProblemType == HCP || ProblemType == HPP)
MaxCandidates = 0;
if (TraceLevel >= 1)
{
}
/* These printfs shouldn't happen, maybe delete them or assert. */
if (InitialTourFileName)
printf("initial tour name\n");
if (InputTourFileName)
Expand Down Expand Up @@ -482,9 +481,9 @@ void _Reset6();
void _Reset7();
void _Reset8();

// m_calculate reads the matrix buffer and outputs it into tourBuff and updates
// elk_invoke_solver reads the matrix buffer and outputs it into tourBuff and updates
// tourN which is the tour length.
static int m_calculate(int *matrixBuff, int matrixLen, int runCount, int *tourBuff, int *tourN)
static int elk_invoke_solver(int *matrixBuff, int matrixLen, int runCount, int *tourBuff, int *tourN)
{
_Reset1();
_Reset2();
Expand All @@ -497,7 +496,7 @@ static int m_calculate(int *matrixBuff, int matrixLen, int runCount, int *tourBu

GainType Cost, OldOptimum;
double Time, LastTime = GetTime();
ReadParameters(runCount);
SetParameters(runCount);
MaxMatrixDimension = 20000;
MergeWithTour = Recombination == IPT ? MergeWithTourIPT : MergeWithTourGPX2;
_elk_ReadProblem(matrixBuff, matrixLen);
Expand Down

0 comments on commit 51ece58

Please sign in to comment.