-
Notifications
You must be signed in to change notification settings - Fork 0
/
Population.hpp
152 lines (109 loc) · 4.76 KB
/
Population.hpp
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
//
// Population.hpp
//
// Copyright 2012 Darren Kessner
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef _POPULATION_HPP_
#define _POPULATION_HPP_
#include "DataVector.hpp"
#include "Organism.hpp"
#include "shared_ptr.hpp"
#include <vector>
class MatingDistribution
{
public:
typedef std::pair<size_t,size_t> IndexPair;
MatingDistribution() : totalWeight_(0) {}
void push_back(double weight, const IndexPair& indexPair);
const IndexPair& random_index_pair(const Random& random) const;
struct Entry
{
double cumulativeWeight;
IndexPair indexPair;
Entry(double c=0, const IndexPair& i = std::make_pair(0,0)) : cumulativeWeight(c), indexPair(i) {}
};
typedef std::vector<Entry> Entries;
const Entries& entries() const {return entries_;}
bool empty() const {return entries_.empty();}
private:
Entries entries_;
double totalWeight_;
};
bool operator==(const MatingDistribution::Entry& a, const MatingDistribution::Entry& b);
bool operator!=(const MatingDistribution::Entry& a, const MatingDistribution::Entry& b);
bool operator==(const MatingDistribution& a, const MatingDistribution& b);
bool operator!=(const MatingDistribution& a, const MatingDistribution& b);
std::ostream& operator<<(std::ostream& os, const MatingDistribution& md);
std::istream& operator>>(std::istream& is, MatingDistribution::Entry& entry);
std::istream& operator>>(std::istream& is, MatingDistribution& md);
class Population;
typedef shared_ptr<Population> PopulationPtr;
typedef std::vector<PopulationPtr> PopulationPtrs;
typedef shared_ptr<PopulationPtrs> PopulationPtrsPtr;
class Population
{
public:
Population() {}
Population(const Organisms& organisms)
: organisms_(organisms)
{}
struct Config
{
size_t size;
// for generating population from nothing
size_t chromosomePairCount;
unsigned int populationID;
unsigned int idOffset;
// for generating population from a previous generation
MatingDistribution matingDistribution;
Config()
: size(0), chromosomePairCount(0), populationID(0), idOffset(0)
{}
};
typedef std::vector<Config> Configs;
void create_organisms(const Config& config,
const PopulationPtrs& populations = PopulationPtrs(),
const DataVectorPtrs& fitnesses = DataVectorPtrs(), // null ok, but size must match populations
const Random& random = Random());
const std::vector<Organism>& organisms() const {return organisms_;}
size_t size() const {return organisms_.size();}
shared_ptr<Population> randomSubsample(size_t size, Random& random) const;
// binary read/write
void read(std::istream& is);
void write(std::ostream& os) const;
// convenience function: creates new generation from previous
static PopulationPtrsPtr create_populations(const std::vector<Population::Config>& configs,
const PopulationPtrs& previous,
const DataVectorPtrs& fitnesses,
const Random& random);
private:
Organisms organisms_;
friend std::istream& operator>>(std::istream& is, Population& p);
// disallow copying
Population(Population&);
Population& operator=(Population&);
};
bool operator==(const Population::Config& a, const Population::Config& b);
bool operator!=(const Population::Config& a, const Population::Config& b);
std::ostream& operator<<(std::ostream& os, const Population::Config& config);
std::istream& operator>>(std::istream& is, Population::Config& config);
bool operator==(const Population& a, const Population& b);
bool operator!=(const Population& a, const Population& b);
std::ostream& operator<<(std::ostream& os, const Population& p);
std::istream& operator>>(std::istream& is, Population& p);
// multiple generations: each generation has Population::Configs, one Config per population
std::ostream& operator<<(std::ostream& os, const std::vector<Population::Configs>& generation_configs);
std::istream& operator>>(std::istream& is, std::vector<Population::Configs>& generation_configs);
#endif // _POPULATION_HPP_