-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start moving GA code to separate files
- Loading branch information
1 parent
67271d1
commit 8d0b384
Showing
17 changed files
with
263 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
#include <pythonic.h> | ||
#include <random> | ||
|
||
|
||
namespace GA { | ||
|
||
using genome = std::vector<double>; | ||
|
||
using population = std::vector<genome>; | ||
using light_population = std::vector<genome*>; | ||
|
||
using parents_t = std::vector<std::pair<genome, genome>>; | ||
using light_parents_t = std::vector<std::pair<genome*, genome*>>; | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
void renew_random(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// Created by Vova on 18.09.2020. | ||
// | ||
|
||
#include "crossover.h" | ||
|
||
|
||
namespace GA | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Created by Vova on 18.09.2020. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "../ga_base.h" | ||
|
||
namespace GA | ||
{ | ||
population perform_crossover_matting(parents_t& parents); // Deprecated | ||
light_parents_t distribute_pairs(light_population& pop, size_t pair_amount, bool allow_gay_marriage = false); | ||
|
||
genome mat_parents(const std::pair<genome*, genome*>& parents, const normalizer& normaaaaa, matting_mode mode = matting_mode::low_variance_genetic); // For test | ||
population perform_crossover(parents_t& parents, const normalizer& normaaaaa, matting_mode mode = matting_mode::low_variance_genetic); // Fancy | ||
population perform_crossover(light_parents_t& parents, const normalizer& normaaaaa, matting_mode mode = matting_mode::low_variance_genetic); // Fancy | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// Created by Vova on 17.09.2020. | ||
// | ||
|
||
#include "matting.h" | ||
|
||
|
||
namespace GA | ||
{ | ||
population make_new_generation(population& pop, const std::vector<double>& fitnesses, const normalizer& normaaaaa, | ||
const double hyper_elite_fit_pow, const double usual_elite_fit_pow, const double parent_fit_pow, | ||
const size_t usual_elite_number, const size_t hyper_elite_number, const size_t best_genome_number, const genome& best_genome, | ||
const matting_mode mode) | ||
{ | ||
light_population souls; | ||
|
||
souls.reserve(pop.size()); | ||
for (genome& val : pop) souls.push_back(&val); | ||
|
||
light_population parents = select_matting_pool(souls, fitnesses, pop.size(), parent_fit_pow); | ||
light_population usual_elite = select_matting_pool(souls, fitnesses, usual_elite_number, usual_elite_fit_pow); | ||
light_population hyper_elite = select_matting_pool(souls, fitnesses, hyper_elite_number, hyper_elite_fit_pow); | ||
|
||
|
||
size_t child_number = pop.size() - usual_elite_number - hyper_elite_number - best_genome_number; | ||
|
||
|
||
// cout << "Parents: " << endl; | ||
// for (auto parent : parents) cout << *parent << endl; | ||
|
||
// cout << "Elite: " << endl; | ||
// for (auto& p : elite) cout << *p << endl; | ||
|
||
|
||
light_parents_t formed_pairs = distribute_pairs(parents, child_number); | ||
|
||
// cout << "Best: " << 1 / find_best_genome(pop, fitnesses).first << " " << find_best_genome(pop, fitnesses).second << endl; | ||
|
||
// cout << endl << "Pairs: " << endl; | ||
// for (auto& p : formed_pairs) cout << *p.first << " " << *p.second << endl; | ||
|
||
population res = perform_crossover(formed_pairs, normaaaaa, mode); | ||
|
||
res.reserve(pop.size()); | ||
|
||
// cout << res << endl; | ||
|
||
for (auto& elite_person : usual_elite) res.emplace_back(*elite_person); | ||
for (auto& elite_person : hyper_elite) res.emplace_back(*elite_person); | ||
|
||
for (size_t index = 0; index < best_genome_number; index++) res.emplace_back(best_genome); | ||
|
||
return res; | ||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Created by Vova on 17.09.2020. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "../ga_base.h" | ||
|
||
#include "crossover.h" | ||
|
||
|
||
namespace GA | ||
{ | ||
enum class matting_mode | ||
{ | ||
uniform, | ||
|
||
high_variance_normal, | ||
middle_variance_normal, | ||
low_variance_normal, | ||
|
||
random_variance_normal, | ||
|
||
high_variance_genetic, | ||
low_variance_genetic | ||
}; | ||
|
||
population make_new_generation(population& pop, const std::vector<double>& fitnesses, const normalizer& normaaaaa, | ||
double hyper_elite_fit_pow, double usual_elite_fit_pow, double parent_fit_pow, | ||
size_t usual_elite_number, size_t hyper_elite_number, size_t best_genome_number, const genome& best_genome, | ||
matting_mode mode = matting_mode::low_variance_genetic); | ||
|
||
population select_matting_pool(const population& genomes, const std::vector<double>& fitnesses, size_t amount, double fit_dependence); | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Created by Vova on 17.09.2020. | ||
// | ||
|
||
#include "mutation.h" | ||
|
||
|
||
namespace GA | ||
{ | ||
void mutate(genome& target_genome, const std::vector<double>& sigmas, const double target_gene_number, const normalizer& normaaa) | ||
{ | ||
auto real_gene_number = static_cast<size_t>(cut(std::round(normaaa.generate(target_gene_number, target_gene_number / 2)), 0, target_genome.size())); | ||
std::vector<size_t> mutate_indexes = uniform_int_distribute(0, target_genome.size() - 1, real_gene_number); | ||
// vector<double> mutate_values = normal_distribute(0, sigma, real_gene_number); | ||
// cout << mutate_indexes << " " << mutate_values << endl; | ||
for (size_t index = 0; index < mutate_indexes.size(); index++) { | ||
double this_mutate_value = normaaa.generate(0, sigmas[index]); | ||
target_genome[mutate_indexes[index]] += this_mutate_value; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Created by Vova on 17.09.2020. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "../ga_base.h" | ||
|
||
namespace GA | ||
{ | ||
void mutate(genome& target_genome, const std::vector<double>& sigmas, double target_gene_number, const normalizer& normaaa); | ||
|
||
} |
Oops, something went wrong.