-
Notifications
You must be signed in to change notification settings - Fork 0
/
Specimen.cpp
95 lines (76 loc) · 2.12 KB
/
Specimen.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
//
// Created by jan on 15.09.17.
//
#include "Specimen.h"
#include <stdlib.h>
#include <math.h>
#include <iostream>
class Sim;
Specimen::Specimen(std::vector<float>& input_neuron, std::vector<float>& output_neuron, World& world) : world(world)
{
this->input_neuron = input_neuron;
this->output_neuron = output_neuron;
random_initialise();
}
void Specimen::random_initialise()
{
x_pos = ((float) rand() * world.get_size_x())/RAND_MAX;
y_pos = ((float) rand() * world.get_size_y())/RAND_MAX;
orientation = ((float) rand() * 2 * M_PI)/RAND_MAX;
species = ((float) rand())/RAND_MAX;
energy = 1;
life_time_equivalent = 0;
active = true;
}
void Specimen::simulate_frame() {
//output neurons: 0 - forward speed, 1 - angular speed, 2 - eating speed (negative means no eating)
//input neurons: 0 - food kind feeler
if (active)
{
float forward_speed, angular_speed, eating_speed;
forward_speed = std::min(output_neuron[0] + 1, energy);
angular_speed =
output_neuron[1] >= 0 ? std::min(output_neuron[1], energy) : std::max(output_neuron[1], -energy);
eating_speed = output_neuron[2];
orientation += angular_speed * 0.2;
x_pos += forward_speed * cos(orientation);
y_pos += forward_speed * sin(orientation);
energy -= forward_speed / 1000;
life_time_equivalent += 1 / energy;
if (life_time_equivalent >= 20000)
{
die();
}
}
}
void Specimen::die()
{
active = false;
}
float Specimen::get_x_pos() const {
return x_pos;
}
float Specimen::get_y_pos() const {
return y_pos;
}
float Specimen::get_orientation() const {
return orientation;
}
float Specimen::get_species() const {
return species;
}
float Specimen::get_food_feeler_angle() const {
return food_feeler_angle;
}
float Specimen::get_specimen_feeler_angle() const {
return specimen_feeler_angle;
}
float Specimen::get_food_level() const {
return energy;
}
float Specimen::get_life_time_equivalent() const {
return life_time_equivalent;
}
bool Specimen::is_active() const {
return active;
}