-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchromosome.cpp
153 lines (131 loc) · 3.17 KB
/
chromosome.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "chromosome.h"
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
Chromosome::Chromosome()
{
//ctor
}
Chromosome::Chromosome(const Chromosome& other)
{
//copy ctor
this->TSList = other.getSequence();
this->conditions = other.getConditionHash();
this->TestCaseCount = other.getCount();
this->TotalWeight = other.getWeight();
this->fitness = other.getFitness();
this->mcount = other.getMessageCount();
this->size = other.getSize();
}
bool Chromosome::operator < (const Chromosome& another)const
{
if(this->TestCaseCount < another.getCount())
return true;
else if(this->TestCaseCount > another.getCount())
return false;
else
return((this->fitness)< another.getFitness());
}
void Chromosome::setSequence(vector<TestSequence> TS)
{
this->TSList = TS;
}
vector<TestSequence> Chromosome::getSequence() const
{
return this->TSList;
}
void Chromosome::generate(vector<TestSequence> TS, int size)
{
random_shuffle(TS.begin(),TS.end(), p_myrandom);
random_shuffle(TS.begin(),TS.end(), p_myrandom);
vector<TestSequence> list;
this->TotalWeight = 0;
this->mcount = 0;
for(int i =0; i<size; i++)
{
list.push_back(TS[i]);
(this->TotalWeight)+= TS[i].getPriority();
(this->mcount)+= TS[i].getLength();
}
this->setSequence(list);
this->setSize(size);
this->evalConditions();
double fit = (double)(this->TotalWeight)/(this->mcount);
this->setFitness(fit);
}
void Chromosome::create(vector<TestSequence> TS)
{
int size = TS.size();
this->TotalWeight = 0;
this->mcount = 0;
for(int i =0; i<size; i++)
{
(this->TotalWeight)+= TS[i].getPriority();
(this->mcount)+= TS[i].getLength();
}
this->setSequence(TS);
this->setSize(size);
this->evalConditions();
double fit = (double)(this->TotalWeight)/(this->mcount);
this->setFitness(fit);
}
void Chromosome::setFitness(double fit)
{
this->fitness = fit;
}
void Chromosome::setWeight(int weight)
{
this->TotalWeight = weight;
}
void Chromosome::setCount(int count)
{
this->TestCaseCount = count;
}
void Chromosome::setSize(int s)
{
this->size = s;
}
int Chromosome::getCount() const
{
return this->TestCaseCount;
}
double Chromosome::getFitness() const
{
return this->fitness;
}
int Chromosome::getWeight() const
{
return this->TotalWeight;
}
int Chromosome::getSize() const
{
return this->size;
}
void Chromosome::evalConditions()
{
for(unsigned int i =0; i<SeqD_conditions.size(); i++)
{
for(unsigned int j=0; j<TSList.size(); j++)
{
map<string,int> temp = TSList[j].getConditions();
if(temp[SeqD_conditions[i]])
{
this->conditions[SeqD_conditions[i]] = 1;
break;
}
}
}
this->setCount((this->conditions).size());
}
map<string,int> Chromosome::getConditionHash()const
{
return this->conditions;
}
void Chromosome::setMessageCount(int mcount)
{
this->mcount = mcount;
}
int Chromosome::getMessageCount() const
{
return this->mcount;
}