File: gene.cc

package info (click to toggle)
achilles 2-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 684 kB
  • sloc: sh: 2,600; cpp: 2,069; xml: 25; makefile: 7
file content (151 lines) | stat: -rw-r--r-- 3,576 bytes parent folder | download | duplicates (6)
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
/* 

Copyright (C) 2000 Matthew Danish

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
using namespace std;
#include"gene.h"
#include"vector.h"
#include<vector>
#include"braininfo.h"
#include"defines.h"
#include<stdio.h>
#include<cstdlib>

GeneClass::GeneClass() { // randomly generate one
  int i;
  for(i=0;i<NUM_GENES;i++)
    DNA.push_back(long(rand()));
  MakeViable();
}

GeneClass::GeneClass(GeneClass &a,GeneClass &b) {
  int i;
  for(i=0;i<NUM_GENES;i++) {
    if(double(rand())/RAND_MAX*100.0 <= a.MutationRate())
      DNA.push_back(SCALE(rand(),0,LONG_MAX,GeneLimits[i].min,GeneLimits[i].max));
    else {
      if((int)((double)(rand())/RAND_MAX*2)) {
	DNA.push_back(a.DNA[i]);
      } else {
	DNA.push_back(b.DNA[i]);
      }

    }
  }
}

GeneClass::~GeneClass() {}

double GeneClass::MutationRate() {
  return (double(DNA[GENE_MUTATE_RATE])/MAX_MUTATE_RATE);
}

double GeneClass::Variance(GeneClass &a) {
  double abs_d(double);
  double average=0;

  for(int i=0;i<NUM_GENES;i++) 
    average+= double(DNA[i] - a.DNA[i])/double(GeneLimits[i].max - GeneLimits[i].min);
  average/=NUM_GENES;
#ifdef _DEBUG
  cout << "Variance: " << average << endl;
#endif
  return abs_d(average);
}

VectorClass GeneClass::Size() {
  VectorClass size((double)DNA[GENE_SIZE_X],
		   (double)DNA[GENE_SIZE_Y],
		   (double)DNA[GENE_SIZE_Z]);
  return size;
}

double GeneClass::LC() {
  return(double(DNA[GENE_LC])/MAX_LC);
}

BrainInfoClass GeneClass::Brain() {
  BrainInfoClass brain(DNA[GENE_NUM_NEURODES],
		       DNA[GENE_NUM_LAYERS],
		       DNA[GENE_NUM_INPUTS],
		       DNA[GENE_NUM_OUTPUTS],
		       double(DNA[GENE_TOPO_DIST])/MAX_TOPO_DIST);
  return brain;
}

bool GeneClass::IsViable() {
  int i;
  if(MIN_VOLUME > DNA[0]*DNA[1]*DNA[2] || 
     DNA[0]*DNA[1]*DNA[2] > MAX_VOLUME) return false;
  
  for(i=0;i<NUM_GENES;i++) {
    if(GeneLimits[i].min > DNA[i] || DNA[i] > GeneLimits[i].max)
      return false;
  }
  return true;
}

double GeneClass::Color() {
  return(double(DNA[GENE_COLOR]) / MAX_COLOR);
}

double GeneClass::MaxSpeed() {
  return(double(DNA[GENE_MAXSPEED]) / MAX_MAXSPEED);
}

double GeneClass::GetEnergyPcnt() {
  return(double(DNA[GENE_ENERGY_TO_OFFSPRING]) / MAX_ECAP);
}

double GeneClass::Metabolism() {
  return(double(DNA[GENE_METABOLISM]) / MAX_METABOLISM);
}

long GeneClass::Reach() {
  return(DNA[GENE_REACH]);
}

long GeneClass::Lifespan() {
  return(DNA[GENE_LIFESPAN]);
}

double GeneClass::GetStrength() {
  return(DNA[GENE_STRENGTH] * 2.0 / MAX_STRENGTH);
}

bool GeneClass::MakeViable() {
  int i,j;
  double tmp;

  do {
    j=0;
    for(i=0;i<3;i++) {
      if(DNA[i] < MIN_SIDE_LEN) DNA[i]=MIN_SIDE_LEN;
      if(DNA[i] > DNA[j]) j = i;
    }
    tmp = DNA[0] * DNA[1] * DNA[2];
  } while(tmp > MAX_VOLUME && (DNA[j]=long(double(DNA[j])*double(MAX_VOLUME/tmp))));


  for(i=0;i<NUM_GENES;i++)
    DNA[i]=SCALE(DNA[i],0,LONG_MAX,GeneLimits[i].min,GeneLimits[i].max);

  return true;
}