Skip to content

Commit 50162ab

Browse files
committed
added generate offspring
1 parent 780cf7a commit 50162ab

File tree

8 files changed

+78
-9
lines changed

8 files changed

+78
-9
lines changed

game/players/ai_player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ namespace Players{
7070

7171
this->input_data->update_value(0, 0, (double)(px-fx)/w);
7272
this->input_data->update_value(0, 1, (double)(py-fy)/h);
73-
this->input_data->update_value(0, 2, get_angle(px, py, fx, fy) / (2*PI));
73+
// this->input_data->update_value(0, 2, get_angle(px, py, fx, fy) / (2*PI));
7474
}
7575

7676

game/players/ai_player.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ namespace Players{
2929
private:
3030
Chromosome* chromosome = nullptr;
3131
NN* nn = new NN;
32-
Layer* input_layer = new Layer(3, true);
33-
Matrix* input_data = new Matrix(3, 1);
32+
Layer* input_layer = new Layer(2, true);
33+
Matrix* input_data = new Matrix(2, 1);
3434

3535
void setup_nn();
3636
void setup_chromosome();

genetic/population.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#include <cstdint>
2+
#include <stdexcept>
23
#include <unistd.h>
34
#include "population.h"
45
#include "../game/board.h"
56
#include "../helpers/utils.h"
7+
#include "chromosome.h"
8+
#include "gene.h"
69

10+
using std::invalid_argument;
711
using Game::Board;
12+
using Chromosomes::Chromosome;
13+
using Genes::Gene;
814
using Utils::random_int;
915

1016
namespace Genetic{
@@ -38,7 +44,7 @@ namespace Genetic{
3844

3945
void Population::generate_food_positions(uint8_t total, uint8_t w, uint8_t h){
4046
for(size_t _ = 0; _ < total; _++)
41-
this->food_positions.push_back(vec2{random_int(0, h-1), random_int(0, w-1)});
47+
this->food_positions.push_back(vec2{(int16_t)random_int(0, h-1), (int16_t)random_int(0, w-1)});
4248
// TODO: refactor this random position to a especialist class or something like this
4349
}
4450

@@ -134,7 +140,8 @@ namespace Genetic{
134140

135141
void Population::next_gen(){
136142
Individual** parents = this->select_parents();
137-
//generate the offspring
143+
Chromosome* offspring = this->generate_offspring(parents[0]->player->get_chromossome(), parents[1]->player->get_chromossome());
144+
138145
//reset individuals
139146
//replicate
140147
//mutate
@@ -170,6 +177,30 @@ namespace Genetic{
170177
return parents;
171178
}
172179

180+
Chromosome* Population::generate_offspring(Chromosome* ch1, Chromosome* ch2){
181+
if(ch1->get_size() != ch2->get_size())
182+
throw invalid_argument("Both Chromosomes must have the same size!");
183+
184+
uint64_t ch_size = ch1->get_size();
185+
186+
Chromosome* offspring = new Chromosome(ch_size);
187+
Gene* offspring_genes = new Gene[ch_size];
188+
189+
Gene* ch1_genes = ch1->get_genes();
190+
Gene* ch2_genes = ch2->get_genes();
191+
192+
uint64_t pivot = random_int(0, ch_size);
193+
for(size_t i = 0; i < ch_size; i++){
194+
if(i < pivot)
195+
offspring_genes[i].set_gene_value(ch1_genes[i].get_gene_value());
196+
else
197+
offspring_genes[i].set_gene_value(ch2_genes[i].get_gene_value());
198+
}
199+
offspring->copy_genes(offspring_genes);
200+
201+
return offspring;
202+
}
203+
173204
void Population::append_individual(Individual* ind){
174205
this->individuals.push_back(ind);
175206
}

genetic/population.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
#include "../game/players/ai_player.h"
66
#include "../game/board.h"
77
#include "../helpers/utils.h"
8+
#include "../genetic/chromosome.h"
89

910
using std::vector;
1011
using Game::Board;
1112
using Players::AIPlayer;
1213
using Utils::vec2;
1314
using Players::Directions;
15+
using Chromosomes::Chromosome;
1416

1517
namespace Genetic {
1618
typedef struct Individual{
@@ -29,6 +31,7 @@ namespace Genetic {
2931
void next_gen();
3032
void run();
3133
uint32_t get_gen();
34+
Chromosome* generate_offspring(Chromosome* ch1, Chromosome* ch2);
3235
uint16_t get_total_alive();
3336
uint8_t get_total_win();
3437
uint16_t get_best_score();

helpers/utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ namespace Utils {
3838
return distr(eng);
3939
}
4040

41-
int16_t random_int(uint8_t start, uint8_t end){
41+
int64_t random_int(uint64_t start, uint64_t end){
4242
random_device device;
4343
mt19937 eng(device());
44-
uniform_int_distribution<int16_t> distr(start, end);
45-
int16_t result = distr(eng);
44+
uniform_int_distribution<int64_t> distr(start, end);
45+
int64_t result = distr(eng);
4646
return result;
4747
}
4848

helpers/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Utils {
1717
}vec2;
1818

1919
double random(double start, double end);
20-
int16_t random_int(uint8_t start, uint8_t end);
20+
int64_t random_int(uint64_t start, uint64_t end);
2121
void append_to_file(string filename, string data);
2222
void create_file(string filename, string data);
2323
vector<Matrix*> parse_weigths(string filename);

manual_tests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,20 @@ int main(){
127127

128128
cout << "parent 1 " << parents[0] << endl;
129129
cout << "parent 2 " << parents[1] << endl;
130+
131+
cout << endl << "!!!!testig generate offsping!!!!" << endl;
132+
Population p2(0);
133+
Chromosome chromosome1(10);
134+
Chromosome chromosome2(10);
135+
136+
cout << "Chromosome 1: ";
137+
chromosome1.show();
138+
cout << "Chromosome 2: ";
139+
chromosome2.show();
140+
141+
Chromosome* offsping = p2.generate_offspring(&chromosome1, &chromosome2);
142+
cout << "offsping: ";
143+
offsping->show();
144+
delete offsping;
130145
}
131146

tests/population_test.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#include <gtest/gtest.h>
2+
#include <stdexcept>
23
#include "../genetic/population.h"
34
#include "../game/players/player.h"
5+
#include "../genetic/chromosome.h"
6+
#include "../genetic/gene.h"
47

8+
using std::invalid_argument;
59
using Genetic::Population;
610
using Genetic::Individual;
711
using Players::Directions;
12+
using Chromosomes::Chromosome;
13+
using Genes::Gene;
814

915
namespace {
1016
TEST(ValueTest, OnlyTwoIndividualsSelectParentsTest){
@@ -90,5 +96,19 @@ namespace {
9096
ASSERT_EQ(parents[1], ind2);
9197
delete[] parents;
9298
}
99+
100+
101+
TEST(ValuesTest, GenerateOffSpringChromosomesWithDifferentSizesTest){
102+
Population p(0);
103+
Chromosome ch1(10);
104+
Chromosome ch2(3);
105+
ASSERT_THROW({ p.generate_offspring(&ch1, &ch2); }, invalid_argument);
106+
}
93107

108+
TEST(ValuesTest, GenerateOffSpringChromosomesWithSameSizesTest){
109+
Population p(0);
110+
Chromosome ch1(10);
111+
Chromosome ch2(10);
112+
ASSERT_NO_THROW({ p.generate_offspring(&ch1, &ch2); });
113+
}
94114
}

0 commit comments

Comments
 (0)