-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecies.js
119 lines (95 loc) · 2.74 KB
/
species.js
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
/**
* Species class
*
*/
Species = function(id, input, environment, simulation)
{
this.id = id;
this.species_name = input.species_name;
this.attributes = input.attributes;
this.chromosome_count = input.chromosome_count;
this.environment = environment;
this.sim = simulation;
this.organisms = [];
this.template = {};
this.chromosomes = [];
for (var i=0; i < this.chromosome_count; i++)
{
var c = new Chromosome(i, 0, []);
this.chromosomes.push(c);
}
var chromosomes = this.chromosomes;
for (attr in this.attributes) {
var val = this.attributes[attr];
for (var i=0; i < Math.floor(Math.random()*5) + 2; i++) {
var c = rand_element(chromosomes);
var pos = c.genes.length; //l(rand_empty(c.genes, 100));
var g = new Gene(0, pos, attr, val, rand(2));
c.genes[pos] = g;
}
}
this.nucleus = function() {
var nucleus = [];
for (i in this.chromosomes) {
var chromosome = this.chromosomes[i];
var genes1 = [];
var genes2 = [];
for (p in chromosome.genes) {
var gene = chromosome.genes[p];
genes1[p] = new Gene(0, gene.position, gene.attribute, (Math.random() / 2.0 + .75) * gene.effect, rand(2));
genes2[p] = new Gene(0, gene.position, gene.attribute, (Math.random() / 2.0 + .75) * gene.effect, rand(2));
}
var c1 = new Chromosome(0, 0, genes1);
var c2 = new Chromosome(0, c1, genes2);
c1.sister = c2;
var cp = new ChromosomePair(0, c1, c2)
nucleus.push(cp);
}
return nucleus;
}
this.organism = function() {
var o = new Organism(this.sim.org_count, this, this.nucleus());
o.age = rand(o.attributes["mature_age"]) + 1;
o.size = 2;
o.food += 45;
o.sim = this.sim;
return o;
}
this.population = function(size) {
}
this.average_attribute = function(attribute) {
var total = 0;
for (var i = 0; i < this.organisms.length; i++) {
total += this.organisms[i].attributes[attribute];
}
return total / this.organisms.length;
}
this.average_size = function() {
var total = 0;
for (var i = 0; i < this.organisms.length; i++) {
total += this.organisms[i].size;
}
return total / this.organisms.length;
}
this.remove_organism = function(org) {
var index = this.organisms.indexOf(org);
this.organisms.splice(index, 1);
this.sim.remove_organism(org);
};
this.random_organism = function() {
return this.organisms[rand(this.organisms.length)];
}
this.stats = function() {
var stats = {};
var species = this;
$.each(this.attributes, function(attribute, value) {
stats[attribute] = species.average_attribute(attribute);
});
stats['avg_size'] = this.average_size();
stats['population_size'] = this.organisms.length;
return stats;
}
this.can_mate = function() {
return (rand(1000) / Math.max((this.organisms.length - 3000), 0)) > 1;
}
}