-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrait.cpp
110 lines (83 loc) · 2.58 KB
/
trait.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
#include "trait.h"
#include <iostream>
#include <sstream>
using namespace NEAT;
Trait::Trait () {
for (int count=0;count<NEAT::num_trait_params;count++)
params[count]=0;
trait_id=0;
}
Trait::Trait(int id,double p1,double p2,double p3,double p4,double p5,double p6,double p7,double p8,double p9) {
trait_id=id;
params[0]=p1;
params[1]=p2;
params[2]=p3;
params[3]=p4;
params[4]=p5;
params[5]=p6;
params[6]=p7;
params[7]=0;
}
Trait::Trait(const Trait& t) {
for(int count=0; count < NEAT::num_trait_params; count++)
params[count]=(t.params)[count];
trait_id = t.trait_id;
}
Trait::Trait(Trait *t) {
for(int count=0;count<NEAT::num_trait_params;count++)
params[count]=(t->params)[count];
trait_id=t->trait_id;
}
Trait::Trait(const char *argline) {
std::stringstream ss(argline);
//Read in trait id
// std::string curword;
//char delimiters[] = " \n";
//int curwordnum = 0;
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
// trait_id = atoi(curword);
ss >> trait_id;
//std::cout << ss.str() << " trait_id: " << trait_id << std::endl;
//IS THE STOPPING CONDITION CORRECT? ALERT
for(int count=0;count<NEAT::num_trait_params;count++) {
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
//params[count] = atof(curword);
ss >> params[count];
//iFile>>params[count];
}
}
Trait::Trait(Trait *t1,Trait *t2) {
for(int count=0;count<NEAT::num_trait_params;count++)
params[count]=(((t1->params)[count])+((t2->params)[count]))/2.0;
trait_id=t1->trait_id;
}
void Trait::print_to_file(std::ofstream &outFile) {
outFile<<"trait "<<trait_id<<" ";
for(int count=0;count<NEAT::num_trait_params;count++)
outFile<<params[count]<<" ";
outFile<<std::endl;
}
void Trait::print_to_file(std::ostream &outFile) {
//outFile<<"trait "<<trait_id<<" ";
//for(int count=0;count<NEAT::num_trait_params;count++)
// outFile<<params[count]<<" ";
//outFile<<endl;
char tempbuf[128];
sprintf(tempbuf, "trait %d ", trait_id);
outFile << tempbuf;
for (int count = 0; count<NEAT::num_trait_params;count++) {
char tempbuf2[128];
sprintf(tempbuf2, "%f ", params[count]);
outFile << tempbuf2;
}
outFile << std::endl;
}
void Trait::mutate() {
for(int count=0;count<NEAT::num_trait_params;count++) {
if (randfloat()>NEAT::trait_param_mut_prob) {
params[count]+=(randposneg()*randfloat())*NEAT::trait_mutation_power;
if (params[count]<0) params[count]=0;
if (params[count]>1.0) params[count]=1.0;
}
}
}