#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include "Population.h"
Population::Population (int iInstance0,
int iNbGroup,
int iNbIndividual,
TSP * pTSP,
TspLog * pTspLog0,
char *sLogDir,
char *sLogBasename)
{
int i;
char sGroupFilename[255];
iInstance = iInstance0;
pTspLog = pTspLog0;
pTspLog->StartFunction ("Population::Population", iInstance, LEVEL_DEBUG, 0);
iPopulationSize = iNbGroup;
List = new Group*[iPopulationSize];
for (i = 0; i < iPopulationSize; i++)
{
sprintf (sGroupFilename, "%s/%s%03d.csv", sLogDir, sLogBasename, i);
List[i] = new Group (i, iNbIndividual, pTSP, pTspLog, sGroupFilename);
}
pTspLog->EndFunction ("Population::Population", iInstance);
}
Population::~Population ()
{
int i;
pTspLog->StartFunction ("Population::~Population", iInstance, LEVEL_DEBUG, 0);
for (i = 0; i < iPopulationSize; i++)
{
delete List[i];
}
delete[] List;
pTspLog->EndFunction ("Population::~Population", iInstance);
}
void Population::Init (char *sInitMacro)
{
int i, iInstruction;
char sInstruction[255];
char sMessage[255];
int iParam1, iParam2, iParam3, iParam4;
bool bOk;
pTspLog->StartFunction ("Population::Init", iInstance, LEVEL_DEBUG, 0);
iInstruction = 0;
while (DecodeMacro(sInitMacro, iInstruction, sInstruction, &iParam1, &iParam2, &iParam3, &iParam4))
{
bOk = false;
if (strcmp(sInstruction, "Reprise") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->RandomInit ();
(List[i])->ReadFromCSV ();
}
bOk = true;
}
if (strcmp(sInstruction, "NearestNeighbourInit") == 0)
{
(List[0])->NearestNeighbourInit ();
(List[0])->WriteToCSV ();
for (i = 1; i < iPopulationSize; i++)
{
(List[i])->Duplicate (List[0]);
}
bOk = true;
}
if (strcmp(sInstruction, "RandomInit") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->RandomInit ();
}
bOk = true;
}
if (strcmp(sInstruction, "OptInit") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->OptInit ();
}
bOk = true;
}
if (strcmp(sInstruction, "RandomEvol") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->RandomEvol (iParam1);
}
bOk = true;
}
if (strcmp(sInstruction, "MonteCarloEvol") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->MonteCarloEvol (iParam1, iParam2);
}
bOk = true;
}
if (strcmp(sInstruction, "MonteCarloDistinctEvol") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->MonteCarloDistinctEvol (iParam1, iParam2);
}
bOk = true;
}
if (strcmp(sInstruction, "DarwinEvol") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->DarwinEvol (iParam1, iParam2, iParam3, iParam4);
}
bOk = true;
}
if (strcmp(sInstruction, "MakePheromoneMatrix") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->MakePheromoneMatrix ();
}
bOk = true;
}
if (strcmp(sInstruction, "SortGroup") == 0)
{
Sort();
bOk = true;
}
if (bOk)
{
WriteToCSV();
}
else
{
sprintf(sMessage, "Instruction inconnue : %s", sInstruction);
pTspLog->ErrorMsg(sMessage);
}
iInstruction++;
}
pTspLog->EndFunction ("Population::Init", iInstance);
}
void Population::Evolve (char *sEvolMacro,
int iNbCycles,
bool bGroupRepartition)
{
int i, j, iNbIter;
char sMessage[255];
int iInstruction;
char sInstruction[255];
int iParam1, iParam2, iParam3, iParam4;
bool bOk;
pTspLog->StartFunction ("Population::Evolve", iInstance, LEVEL_DEBUG, iNbCycles);
for (j = 0; j < iNbCycles; j++)
{
sprintf(sMessage, "===== Debut du loop %d / %d =====", j+1, iNbCycles);
pTspLog->DebugMsg (sMessage);
iInstruction = 0;
while (DecodeMacro(sEvolMacro, iInstruction, sInstruction, &iParam1, &iParam2, &iParam3, &iParam4))
{
bOk = false;
if (strcmp(sInstruction, "RandomEvol") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->RandomEvol (iParam1);
}
bOk = true;
}
if (strcmp(sInstruction, "MonteCarloEvol") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
if (bGroupRepartition)
{
iNbIter = (int) (iParam1 * (iPopulationSize - i) * (iPopulationSize - i) * (iPopulationSize - i))
/ (iPopulationSize * iPopulationSize * iPopulationSize * iPopulationSize);
}
else
{
iNbIter = iParam1;
}
(List[i])->MonteCarloEvol (iNbIter, iParam2);
}
bOk = true;
}
if (strcmp(sInstruction, "DarwinEvol") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
if (bGroupRepartition)
{
iNbIter = (int) (iParam1 * (iPopulationSize - i) * (iPopulationSize - i) * (iPopulationSize - i))
/ (iPopulationSize * iPopulationSize * iPopulationSize * iPopulationSize);
}
else
{
iNbIter = iParam1;
}
(List[i])->DarwinEvol (iNbIter, iParam2, iParam3, iParam4);
}
bOk = true;
}
if (strcmp(sInstruction, "MonteCarloDistinctEvol") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->MonteCarloDistinctEvol (iParam1, iParam2);
}
bOk = true;
}
if (strcmp(sInstruction, "MakePheromoneMatrix") == 0)
{
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->MakePheromoneMatrix ();
}
bOk = true;
}
if (strcmp(sInstruction, "SortGroup") == 0)
{
Sort();
bOk = true;
}
if (bOk)
{
WriteToCSV();
}
else
{
sprintf(sMessage, "Instruction inconnue : %s", sInstruction);
pTspLog->ErrorMsg(sMessage);
}
iInstruction++;
}
pTspLog->DebugMsg ("Fin du loop");
}
pTspLog->EndFunction ("Population::Evolve", iInstance);
}
void Population::Sort ()
{
int i, j, groupe_i, groupe_j, elem_i, elem_j, iGroupSize;
iGroupSize = (List[0]->iGroupSize);
pTspLog->StartFunction ("Population::Sort", iInstance, LEVEL_DEBUG, (iPopulationSize * iGroupSize));
for (i = 0; i < (iPopulationSize * iGroupSize); i++)
{
for (j = i + 1; j < (iPopulationSize * iGroupSize); j++)
{
groupe_i = ((int) i) / ((int) iGroupSize);
groupe_j = ((int) j) / ((int) iGroupSize);
elem_i = i % iGroupSize;
elem_j = j % iGroupSize;
if (List[groupe_i]->List[elem_i]->lfFitness < List[groupe_j]->List[elem_j]->lfFitness)
{
if (groupe_i == groupe_j)
{
List[groupe_i]->Exchange (elem_i, elem_j);
}
else
{
if ((!(List[groupe_i]->Belongs (*(List[groupe_j]->List[elem_j]))))
&& (!(List[groupe_j]-> Belongs (*(List[groupe_i]->List[elem_i])))))
{
(List[groupe_i]->pI)->Duplicate (*(List[groupe_i]->List[elem_i]));
(List[groupe_i]->List[elem_i])->Duplicate (*(List[groupe_j]->List[elem_j]));
(List[groupe_j]->List[elem_j])->Duplicate (*(List[groupe_i]->pI));
}
}
}
}
}
pTspLog->EndFunction ("Population::Sort", iInstance);
}
void Population::WriteToCSV()
{
int i;
for (i = 0; i < iPopulationSize; i++)
{
(List[i])->WriteToCSV ();
}
}
bool DecodeMacro(char *sMacro0,
int iMacroNum,
char *sFunctionName,
int *iParam1,
int *iParam2,
int *iParam3,
int *iParam4)
{
char sMacro[255];
char *sParam;
char *sParam2;
bool bOk;
int i;
bOk = false;
strcpy(sFunctionName, "<undefined>");
*iParam1 = -1;
*iParam2 = -1;
*iParam3 = -1;
*iParam4 = -1;
strcpy(sMacro, sMacro0);
sParam=strtok(sMacro, ";");
bOk = (sParam!=NULL);
if (bOk)
{
if (iMacroNum > 0)
{
i = 0;
while ( (i<iMacroNum) && (sParam!=NULL) )
{
sParam=strtok(NULL, ";\n");
i++;
}
bOk = ((i == iMacroNum) && (sParam != NULL) );
}
}
if (bOk)
{
sParam2=strtok(sParam, " (,)\n");
if (sParam2 != NULL)
{
strcpy(sFunctionName, sParam2);
}
else
{
bOk = false;
}
if (sParam2 != NULL)
{
sParam2=strtok(NULL, " (,)\n");
*iParam1 = GetValue(sParam2);
}
if (sParam2 != NULL)
{
sParam2=strtok(NULL, " (,)\n");
*iParam2 = GetValue(sParam2);
}
if (sParam2 != NULL)
{
sParam2=strtok(NULL, " (,)\n");
*iParam3 = GetValue(sParam2);
}
if (sParam2 != NULL)
{
sParam2=strtok(NULL, " (,)\n");
*iParam4 = GetValue(sParam2);
}
}
return bOk;
}
int GetValue(char *sValue)
{
int iResult;
char sVariable[255];
iResult = -1;
if (sValue != NULL)
{
if ( (strlen(sValue)>1) && (sValue[0] == '\%') )
{
strcpy(sVariable, sValue+1);
if (strcmp(sVariable, "MUT_2_CHANGE") == 0)
{
iResult = MUT_2_CHANGE;
}
if (strcmp(sVariable, "MUT_NEAR_2_CHANGE") == 0)
{
iResult = MUT_NEAR_2_CHANGE;
}
if (strcmp(sVariable, "MUT_2_SWAP") == 0)
{
iResult = MUT_2_SWAP;
}
if (strcmp(sVariable, "MUT_NEAR_2_SWAP") == 0)
{
iResult = MUT_NEAR_2_SWAP;
}
if (strcmp(sVariable, "MUT_NEAR_2_KFP") == 0)
{
iResult = MUT_NEAR_2_KFP;
}
if (strcmp(sVariable, "MUT_NEAR_SCRAMBLE_SWAP") == 0)
{
iResult = MUT_NEAR_SCRAMBLE_SWAP;
}
if (strcmp(sVariable, "MUT_NEAR_SCRAMBLE_CHANGE") == 0)
{
iResult = MUT_NEAR_SCRAMBLE_CHANGE;
}
if (strcmp(sVariable, "MUT_4_CHANGE") == 0)
{
iResult = MUT_4_CHANGE;
}
if (strcmp(sVariable, "MUT_DEPL_1") == 0)
{
iResult = MUT_DEPL_1;
}
if (strcmp(sVariable, "MUT_DEPL_1") == 0)
{
iResult = MUT_DEPL_1;
}
if (strcmp(sVariable, "MUT_DEPL_2") == 0)
{
iResult = MUT_DEPL_2;
}
if (strcmp(sVariable, "MUT_DEPL_4") == 0)
{
iResult = MUT_DEPL_4;
}
if (strcmp(sVariable, "MUT_PHEROM_1") == 0)
{
iResult = MUT_PHEROM_1;
}
if (strcmp(sVariable, "MUT_PHEROM_2") == 0)
{
iResult = MUT_PHEROM_2;
}
if (strcmp(sVariable, "MUT_REPLI") == 0)
{
iResult = MUT_REPLI;
}
}
else
{
iResult = atoi(sValue);
}
}
return iResult;
}