This repository has been archived by the owner on Aug 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CChromosome.cc
137 lines (76 loc) · 2.07 KB
/
CChromosome.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "CChromosome.h"
#include <iostream>
using namespace std;
int static u=5000;
//constructeur
CChromosome::CChromosome(){
mlength=0;
mPath=NULL;
}
CChromosome::CChromosome(int pNbTowns){ // constructeur le plus utilisé
// ce constructeur prend en parametre juste un nombre de ville
// qui correspond au nombre de villes utilisées pour le test
// et un chemin est calculé aléatoirement
mlength=pNbTowns;
mPath = new int[pNbTowns];
for (int i=0;i<pNbTowns;i++)
mPath[i]=i;
int lx,ltmp,lrnd;
lx = rand()%pNbTowns;
lrnd=lx;
srand(time(NULL)*u);
for (int i=0;i<pNbTowns;i++)
{
while(lx==lrnd)
lrnd=rand()%pNbTowns;
lx=lrnd;
ltmp=mPath[lx];
mPath[lx]=mPath[i];
mPath[i]=ltmp;
}
u++;
}
CChromosome::CChromosome(const CChromosome &pCChromosome){ // recopie
mlength=pCChromosome.mlength;
mPath = new int[mlength];
for(int i=0;i<mlength;i++)
mPath[i]=pCChromosome.mPath[i];
}
//destructeur
CChromosome::~CChromosome(){
delete [] mPath;
}
//methodes
void CChromosome::print(){ // afficheur
for(int i=0;i<mlength;i++)
cout<<mPath[i]<<" ";
cout<<endl;
}
CChromosome& CChromosome::operator=(const CChromosome &pPath){ // surchargeur
if(this!=&pPath){
delete[] mPath;
mlength=pPath.mlength;
mPath=new int[mlength];
for(int i=0;i<mlength;i++)
mPath[i]=pPath.mPath[i];
}
return *this;
}
int CChromosome::getLength(){ // accesseur
return mlength;
}
int CChromosome::getPath(int i){ // accesseur
return mPath[i];
}
int* CChromosome::getmPath(){ // accesseur
return mPath;
}
void CChromosome::setPath(int pPos,int pObj){ // accesseur
mPath[pPos]=pObj;
}
void CChromosome::setNewChromosome(){ // accesseur
// cet accesseur permet de changer au complet un chromosome
CChromosome lp(mlength);
for (int i=0;i<mlength;i++)
mPath[i]=lp.mPath[i];
}