Darwin Neuroevolution Framework
cart_pole.h
1 // Copyright 2019 The Darwin Neuroevolution Framework Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <core/darwin.h>
18 #include <core/properties.h>
19 
20 namespace cart_pole {
21 
23 struct Config : public core::PropertySet {
24  PROPERTY(gravity, float, 9.8f, "Gravitational acceleration");
25  PROPERTY(max_distance, float, 2.4f, "Maximum distance from the center");
26  PROPERTY(max_angle, float, 60.0f, "Maximum angle from vertical");
27  PROPERTY(max_initial_angle, float, 10.0f, "Maximum starting angle from vertical");
28  PROPERTY(pole_length, float, 1.5f, "Pole length");
29  PROPERTY(pole_density, float, 1.0f, "Pole density");
30  PROPERTY(cart_density, float, 0.0f, "Cart density");
31  PROPERTY(cart_friction, float, 0.0f, "Cart friction");
32  PROPERTY(max_force, float, 5.0f, "Maximum force which can be applied to the cart");
33 
34  PROPERTY(input_pole_angle, bool, true, "Use the pole angle as input");
35  PROPERTY(input_angular_velocity, bool, false, "Use the angular velocity as input");
36  PROPERTY(input_cart_distance, bool, true, "Use the cart distance as input");
37  PROPERTY(input_cart_velocity, bool, false, "Use the cart velocity as input");
38 
39  PROPERTY(test_worlds, int, 5, "Number of test worlds per generation");
40  PROPERTY(max_steps, int, 1000, "Maximum number of steps per episode");
41 
42  PROPERTY(discrete_controls,
43  bool,
44  true,
45  "Force the actuator force to fixed +/-discrete_force_magnitude");
46 
47  PROPERTY(discrete_force_magnitude,
48  float,
49  2.5f,
50  "The fixed force magnitude used if discrete_controls is true");
51 };
52 
90 class CartPole : public darwin::Domain {
91  public:
92  explicit CartPole(const core::PropertySet& config);
93 
94  size_t inputs() const override;
95  size_t outputs() const override;
96 
97  bool evaluatePopulation(darwin::Population* population) const override;
98 
99  const Config& config() const { return config_; }
100 
101  float randomInitialAngle() const;
102 
103  private:
104  void validateConfiguration();
105 
106  private:
107  Config config_;
108 };
109 
110 class Factory : public darwin::DomainFactory {
111  unique_ptr<darwin::Domain> create(const core::PropertySet& config) override;
112  unique_ptr<core::PropertySet> defaultConfig(darwin::ComplexityHint hint) const override;
113 };
114 
115 inline void init() {
116  darwin::registry()->domains.add<Factory>("cart_pole");
117 }
118 
119 } // namespace cart_pole
size_t outputs() const override
Number of outputs from a Brain.
Definition: cart_pole.cpp:38
bool discrete_controls
"Force the actuator force to fixed +/-discrete_force_magnitude"
Definition: cart_pole.h:45
Domain: Cart-Pole.
Definition: cart_pole.h:90
ComplexityHint
A generic hint for the initial population & domain setup.
Definition: darwin.h:47
A population implementation encapsulates the fixed-size set of genotypes, together with the rules for...
Definition: darwin.h:161
size_t inputs() const override
Number of inputs to a Brain.
Definition: cart_pole.cpp:34
int max_steps
"Maximum number of steps per episode"
Definition: cart_pole.h:40
bool input_pole_angle
"Use the pole angle as input"
Definition: cart_pole.h:34
Registry * registry()
Accessor to the Registry singleton.
Definition: darwin.h:295
bool input_angular_velocity
"Use the angular velocity as input"
Definition: cart_pole.h:35
bool evaluatePopulation(darwin::Population *population) const override
Assigns fitness values to every genotype.
Definition: cart_pole.cpp:42
float cart_friction
"Cart friction"
Definition: cart_pole.h:31
float max_force
"Maximum force which can be applied to the cart"
Definition: cart_pole.h:32
bool input_cart_velocity
"Use the cart velocity as input"
Definition: cart_pole.h:37
bool input_cart_distance
"Use the cart distance as input"
Definition: cart_pole.h:36
float cart_density
"Cart density"
Definition: cart_pole.h:30
Interface to the domain factory.
Definition: darwin.h:263
float max_angle
"Maximum angle from vertical"
Definition: cart_pole.h:26
Definition: agent.cpp:18
Cart-Pole domain configuration.
Definition: cart_pole.h:23
float pole_density
"Pole density"
Definition: cart_pole.h:29
The foundation for data structures supporting runtime reflection.
Definition: properties.h:388
core::ImplementationsSet< DomainFactory > domains
Registered domains.
Definition: darwin.h:288
float discrete_force_magnitude
"The fixed force magnitude used if discrete_controls is true"
Definition: cart_pole.h:50
Interface to a domain implementation.
Definition: darwin.h:229
int test_worlds
"Number of test worlds per generation"
Definition: cart_pole.h:39
float pole_length
"Pole length"
Definition: cart_pole.h:28
float gravity
"Gravitational acceleration"
Definition: cart_pole.h:24
float max_distance
"Maximum distance from the center"
Definition: cart_pole.h:25
float max_initial_angle
"Maximum starting angle from vertical"
Definition: cart_pole.h:27