mc2lib
rit.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2016, Marco Elver
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the
15  * distribution.
16  *
17  * * Neither the name of the software nor the names of its contributors
18  * may be used to endorse or promote products derived from this
19  * software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #ifndef MC2LIB_CODEGEN_RIT_HPP_
35 #define MC2LIB_CODEGEN_RIT_HPP_
36 
37 #include <functional>
38 #include <random>
39 #include <vector>
40 
41 #include "../sets.hpp"
42 #include "../simplega.hpp"
43 #include "../types.hpp"
44 
45 namespace mc2lib {
46 namespace codegen {
47 
48 template <class URNG, class OperationFactory>
50  : public simplega::Genome<typename OperationFactory::ResultType::Ptr> {
51  public:
52  typedef typename OperationFactory::ResultType Operation;
54 
55  explicit RandInstTest(URNG& urng, const OperationFactory* factory,
56  std::size_t len)
57  : urng_(urng), factory_(factory), fitness_(0.0f) {
58  this->genome_.resize(len);
59 
60  for (auto& op_ptr : this->genome_) {
61  op_ptr = (*factory)(urng);
62  }
63  }
64 
65  explicit RandInstTest(const RandInstTest& parent1,
66  const RandInstTest& parent2,
67  std::vector<typename Operation::Ptr> g)
68  : simplega::Genome<typename Operation::Ptr>(std::move(g)),
69  urng_(parent1.urng_),
70  factory_(parent1.factory_),
71  fitness_(0.0f) {}
72 
73  void Mutate(float rate) override {
74  std::uniform_int_distribution<std::size_t> dist_idx(
75  0, this->genome_.size() - 1);
76  std::unordered_set<std::size_t> used;
77  std::size_t selection_count = static_cast<std::size_t>(
78  static_cast<float>(this->genome_.size()) * rate);
79 
80  while (selection_count) {
81  auto idx = dist_idx(urng_);
82  if (used.find(idx) != used.end()) {
83  continue;
84  }
85 
86  this->genome_[idx] = MakeRandom();
87 
88  used.insert(idx);
89  --selection_count;
90  }
91  }
92 
93  float Fitness() const override { return fitness_; }
94 
95  void set_fitness(float fitness) { fitness_ = fitness; }
96 
97  const AddrSet& fitaddrs() const { return fitaddrs_; }
98 
99  AddrSet* fitaddrsptr() { return &fitaddrs_; }
100 
101  typename Operation::Ptr MakeRandom() const { return (*factory_)(urng_); }
102 
103  typename Operation::Ptr MakeRandom(const AddrSet& subset_addrs,
104  std::size_t max_tries = 1000) const {
105  return (*factory_)(urng_,
106  [&subset_addrs](types::Addr addr) {
107  return subset_addrs.Contains(addr);
108  },
109  max_tries);
110  }
111 
112  typename Operation::Threads threads() {
113  return ExtractThreads(this->get_ptr());
114  }
115 
116  private:
117  URNG& urng_;
118  const OperationFactory* factory_;
119 
120  float fitness_;
121  AddrSet fitaddrs_;
122 };
123 
124 } // namespace codegen
125 } // namespace mc2lib
126 
127 #endif /* MC2LIB_CODEGEN_RIT_HPP_ */
128 
129 /* vim: set ts=2 sts=2 sw=2 et : */
float Fitness() const override
Fitness accessor.
Definition: rit.hpp:93
RandInstTest(URNG &urng, const OperationFactory *factory, std::size_t len)
Definition: rit.hpp:55
RandInstTest(const RandInstTest &parent1, const RandInstTest &parent2, std::vector< typename Operation::Ptr > g)
Definition: rit.hpp:65
Definition: cats.hpp:47
Container genome_
Raw genome of individual genes of T.
Definition: simplega.hpp:216
void Mutate(float rate) override
Mutate this Genome.
Definition: rit.hpp:73
Simple Genome interface.
Definition: simplega.hpp:129
const OperationFactory * factory_
Definition: rit.hpp:118
Container * get_ptr()
Modifiable genome accessor.
Definition: simplega.hpp:161
AddrSet fitaddrs_
Definition: rit.hpp:121
OperationFactory::ResultType Operation
Definition: rit.hpp:52
Definition: rit.hpp:49
bool Contains(const Element &e) const
Definition: sets.hpp:158
Abstracts over container library&#39;s set implementation.
Definition: sets.hpp:85
Operation::Threads threads()
Definition: rit.hpp:112
Genome()
Default constructor.
Definition: simplega.hpp:138
URNG & urng_
Definition: rit.hpp:117
Types< true >::Addr Addr
Address type.
Definition: types.hpp:66
sets::Set< sets::Types< types::Addr, std::hash< types::Addr > > > AddrSet
Definition: rit.hpp:53
const AddrSet & fitaddrs() const
Definition: rit.hpp:97
AddrSet * fitaddrsptr()
Definition: rit.hpp:99
float fitness_
Definition: rit.hpp:120
Operation::Ptr MakeRandom() const
Definition: rit.hpp:101
void set_fitness(float fitness)
Definition: rit.hpp:95
Operation::Ptr MakeRandom(const AddrSet &subset_addrs, std::size_t max_tries=1000) const
Definition: rit.hpp:103