Ultra 0.9.4
Loading...
Searching...
No Matches
fitness.h
Go to the documentation of this file.
1
12
13#if !defined(ULTRA_FITNESS_H)
14#define ULTRA_FITNESS_H
15
16#include "utility/assert.h"
17#include "utility/misc.h"
18
19#include <algorithm>
20#include <cmath>
21#include <functional>
22#include <iterator>
23#include <numeric>
24#include <ranges>
25#include <type_traits>
26#include <vector>
27
28namespace ultra
29{
30
51/// other applications try to minimize it).
52/// We chose this convention since it seemed more natural (a greater fitness
53/// is a better fitness; achieving a better fitness means to maximize the
54/// fitness). The downside is that sometimes we have to manage negative
55/// numbers, but for our purposes it's not so bad.
56/// Anyway maximization and minimization problems are basically the same: the
57/// solution of `max(f(x))` is the same as `-min(-f(x))`. This is usually
58/// all you have to remember when dealing with examples/problems expressed
59/// in the other notation.
60///
61/// \remark
62/// This intentionally excludes `bool`, since `bool{-1} < bool{0}` is
63/// equivalent to `true < false`, which is `false`.
64///
65template<class F> concept Fitness =
66 OrderedArithmeticType<F> && requires { requires F{-1} < F{0}; };
67
75/// container exposing member iterator functions.
76template<class F> concept MultiDimFitness =
77 std::ranges::sized_range<F> && std::ranges::sized_range<const F>
79
87class with_size
88{
89public:
90 explicit with_size(std::size_t s) : size_(s) {}
91 std::size_t operator()() const { return size_; }
92
93private:
94 std::size_t size_;
95};
96
103class fitnd
104{
105public:
106 // Type alias and iterators.
107 using value_type = double;
108 using values_t = std::vector<value_type>;
109 using iterator = values_t::iterator;
110 using const_iterator = values_t::const_iterator;
111 using difference_type = values_t::difference_type;
112
113 constexpr fitnd() = default;
114 constexpr fitnd(std::initializer_list<double>);
115 fitnd(values_t);
116 fitnd(with_size, value_type = std::numeric_limits<value_type>::lowest());
117
118 [[nodiscard]] std::size_t size() const noexcept;
119 [[nodiscard]] bool empty() const noexcept;
120 [[nodiscard]] value_type operator[](std::size_t) const;
121 [[nodiscard]] value_type &operator[](std::size_t);
122
123 [[nodiscard]] iterator begin() noexcept;
124 [[nodiscard]] iterator end() noexcept;
125 [[nodiscard]] const_iterator begin() const noexcept;
126 [[nodiscard]] const_iterator end() const noexcept;
127
128 [[nodiscard]] friend auto operator<=>(const fitnd &, const fitnd &) = default;
129
130 fitnd &operator+=(const fitnd &);
131 fitnd &operator-=(const fitnd &);
132 fitnd &operator*=(const fitnd &);
133 fitnd &operator/=(const fitnd &);
134
135 fitnd &operator*=(value_type);
136 fitnd &operator/=(value_type);
137
138 friend bool load(std::istream &, fitnd *);
139 friend fitnd combine(const fitnd &, const fitnd &);
140
141private:
142 values_t vect_ {};
143};
144
145// ***********************************************************************
146// * Arithmetic operators *
147// ***********************************************************************
148[[nodiscard]] fitnd operator+(fitnd, const fitnd &);
149[[nodiscard]] fitnd operator-(fitnd, const fitnd &);
150[[nodiscard]] fitnd operator*(fitnd, const fitnd &);
151[[nodiscard]] fitnd operator/(fitnd, const fitnd &);
152[[nodiscard]] fitnd operator*(fitnd, fitnd::value_type);
153[[nodiscard]] fitnd operator*(fitnd::value_type, fitnd);
154[[nodiscard]] fitnd operator/(fitnd, fitnd::value_type);
155[[nodiscard]] fitnd operator-(fitnd);
156
157// ***********************************************************************
158// * Serialization *
159// ***********************************************************************
160[[nodiscard]] bool load(std::istream &, fitnd *);
161
162// ***********************************************************************
163// * Other functions *
164// ***********************************************************************
165[[nodiscard]] fitnd abs(fitnd);
166[[nodiscard]] fitnd sqrt(fitnd);
167std::ostream &operator<<(std::ostream &, const fitnd &);
168std::istream &operator>>(std::istream &, fitnd &);
169
170#include "kernel/fitness.tcc"
171
172} // namespace ultra
173
174#endif // include guard
Definition fitness.h:104
fitnd combine(const fitnd &f1, const fitnd &f2)
Definition fitness.cc:424
std::size_t size() const noexcept
Definition fitness.cc:44
iterator begin() noexcept
Definition fitness.cc:86
iterator end() noexcept
Definition fitness.cc:103
bool empty() const noexcept
Definition fitness.cc:52
Definition fitness.h:88
Definition fitness.h:65
Definition fitness.h:76
STL namespace.
fitnd abs(fitnd f)
Definition fitness.cc:400
fitnd sqrt(fitnd f)
Definition fitness.cc:411