-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgene.cpp
164 lines (134 loc) · 4.58 KB
/
gene.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
154
155
156
157
158
159
160
161
162
163
164
#include "gene.h"
#include <iostream>
#include <sstream>
using namespace NEAT;
Gene::Gene(double w, NNode *inode, NNode *onode, bool recur, double innov, double mnum) {
lnk = new Link(w, inode, onode, recur);
innovation_num = innov;
mutation_num = mnum;
enable = true;
frozen = false;
}
Gene::Gene(Trait *tp,double w,NNode *inode,NNode *onode,bool recur,double innov,double mnum) {
lnk=new Link(tp,w,inode,onode,recur);
innovation_num=innov;
mutation_num=mnum;
enable=true;
frozen=false;
}
Gene::Gene(Gene *g,Trait *tp,NNode *inode,NNode *onode) {
//cout<<"Trying to attach nodes: "<<inode<<" "<<onode<<endl;
lnk=new Link(tp,(g->lnk)->weight,inode,onode,(g->lnk)->is_recurrent);
innovation_num=g->innovation_num;
mutation_num=g->mutation_num;
enable=g->enable;
frozen=g->frozen;
}
Gene::Gene(const char *argline, std::vector<Trait*> &traits, std::vector<NNode*> &nodes) {
//Gene parameter holders
int traitnum;
int inodenum;
int onodenum;
NNode *inode;
NNode *onode;
double weight;
int recur;
Trait *traitptr;
std::vector<Trait*>::iterator curtrait;
std::vector<NNode*>::iterator curnode;
//Get the gene parameters
std::stringstream ss(argline);
//char curword[128];
//char delimiters[] = " \n";
//int curwordnum = 0;
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
//traitnum = atoi(curword);
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
//inodenum = atoi(curword);
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
//onodenum = atoi(curword);
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
//weight = atof(curword);
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
//recur = atoi(curword);
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
//innovation_num = atof(curword);
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
//mutation_num = atof(curword);
//strcpy(curword, NEAT::getUnit(argline, curwordnum++, delimiters));
//enable = (bool)(atoi(curword));
ss >> traitnum >> inodenum >> onodenum >> weight >> recur >> innovation_num >> mutation_num >> enable;
//std::cout << traitnum << " " << inodenum << " " << onodenum << " ";
//std::cout << weight << " " << recur << " " << innovation_num << " ";
//std::cout << mutation_num << " " << enable << std::endl;
frozen=false; //TODO: MAYBE CHANGE
//Get a pointer to the linktrait
if (traitnum==0) traitptr=0;
else {
curtrait=traits.begin();
while(((*curtrait)->trait_id)!=traitnum)
++curtrait;
traitptr=(*curtrait);
}
//Get a pointer to the input node
curnode=nodes.begin();
while(((*curnode)->node_id)!=inodenum)
++curnode;
inode=(*curnode);
//Get a pointer to the output node
curnode=nodes.begin();
while(((*curnode)->node_id)!=onodenum)
++curnode;
onode=(*curnode);
lnk=new Link(traitptr,weight,inode,onode,recur);
}
Gene::Gene(const Gene& gene)
{
innovation_num = gene.innovation_num;
mutation_num = gene.mutation_num;
enable = gene.enable;
frozen = gene.frozen;
lnk = new Link(*gene.lnk);
}
Gene::~Gene() {
delete lnk;
}
void Gene::print_to_file(std::ofstream &outFile) {
outFile<<"gene ";
//Start off with the trait number for this gene
if ((lnk->linktrait)==0) outFile<<"0 ";
else outFile<<((lnk->linktrait)->trait_id)<<" ";
outFile<<(lnk->in_node)->node_id<<" ";
outFile<<(lnk->out_node)->node_id<<" ";
outFile<<(lnk->weight)<<" ";
outFile<<(lnk->is_recurrent)<<" ";
outFile<<innovation_num<<" ";
outFile<<mutation_num<<" ";
outFile<<enable<<std::endl;
}
void Gene::print_to_file(std::ostream &outFile) {
outFile<<"gene ";
//outFile.write(5, "gene ");
//Start off with the trait number for this gene
if ((lnk->linktrait)==0) {
outFile<<"0 ";
//outFile.write(2, "0 ");
}
else {
outFile<<((lnk->linktrait)->trait_id)<<" ";
//char tempbuf2[128];
//sprintf(tempbuf2, sizeof(tempbuf2),"%d ", (lnk->linktrait)->trait_id);
//outFile.write(strlen(tempbuf2),tempbuf2);
}
//char tempbuf[1024];
//sprintf(tempbuf,sizeof(tempbuf),"%d %d %f %d %f %f %d\n", (lnk->in_node)->node_id,
// (lnk->out_node)->node_id, lnk->weight, lnk->is_recurrent, innovation_num, mutation_num, enable);
//outFile.write(strlen(tempbuf),tempbuf);
outFile<<(lnk->in_node)->node_id<<" ";
outFile<<(lnk->out_node)->node_id<<" ";
outFile<<(lnk->weight)<<" ";
outFile<<(lnk->is_recurrent)<<" ";
outFile<<innovation_num<<" ";
outFile<<mutation_num<<" ";
outFile<<enable<<std::endl;
}