-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEvaluation.hpp
126 lines (108 loc) · 3.67 KB
/
Evaluation.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
//
// EvaluationServer.hpp
// NSGA-Parallel-Backend
//
// Created by a1091793 on 25/11/2015.
//
//
#ifndef EvaluationServer_h
#define EvaluationServer_h
#include <tuple>
#include <vector>
#include <boost/foreach.hpp>
#include "Population.hpp"
class ObjectivesAndConstraintsBase
{
public:
typedef ProblemDefinitions::ObjectivesAndConstraintsT ObjectivesAndConstraintsT;
typedef ProblemDefinitions::RealDVsT RealDVsT;
typedef ProblemDefinitions::UnorderedDVsT UnorderedDVsT;
typedef ProblemDefinitions::OrderedDVsT OrderedDVsT;
/**
* Evaluate a solution.
* @param real_decision_vars
* @param int_decision_vars
* @return
*/
virtual
ObjectivesAndConstraintsT
operator()(const RealDVsT & real_decision_vars, const UnorderedDVsT & unordered_decision_vars,
const OrderedDVsT & ordered_dvs) = 0;
/**
* Evaluate a solution, including saving details of the evaluation in save_dir.
* @param real_decision_vars
* @param int_decision_vars
* @param save_dir Where the evaluation is saved to.
* @return
*/
virtual
ObjectivesAndConstraintsT
operator()(const RealDVsT& real_decision_vars, const UnorderedDVsT& unordered_decision_vars,
const OrderedDVsT& ordered_dvs, const boost::filesystem::path & save_dir) = 0;
};
class DummyObjectivesAndConstraints : public ObjectivesAndConstraintsBase
{
private:
//std::pair<std::vector<double>, std::vector<double> > dummy_return;
public:
ObjectivesAndConstraintsT
operator()(const RealDVsT& real_decision_vars, const UnorderedDVsT& unordered_decision_vars,
const OrderedDVsT& ordered_dvs)
{
return (std::pair<std::vector<double>, std::vector<double> >());
}
ObjectivesAndConstraintsT
operator()(const RealDVsT& real_decision_vars, const UnorderedDVsT& unordered_decision_vars,
const OrderedDVsT& ordered_dvs, const boost::filesystem::path& save_dir)
{
return (std::pair<std::vector<double>, std::vector<double> >());
};
};
class EvaluatePopulationBase
{
public:
/**
* Evaluation all memebers of a population
* @param population
*/
virtual void
operator()(PopulationSPtr population) = 0;
/**
* Evaluate all memebers of a population, and save each memeber. Implementations of this
* function will need to create directories for each member of the population to be
* saved into.
* @param population
* @param save_dir where the members of population are saved to. Make subdirectories in this folder.
*/
virtual void
operator()(PopulationSPtr population, const boost::filesystem::path & save_dir) = 0;
};
class EvaluatePopulation : public EvaluatePopulationBase
{
ObjectivesAndConstraintsBase & eval;
public:
EvaluatePopulation(ObjectivesAndConstraintsBase & _eval)
: eval(_eval)
{
}
void
operator()(PopulationSPtr population)
{
for(IndividualSPtr ind: *population)
{
ind->setObjectivesAndConstraints(eval(ind->getRealDVVector(), ind->getUnorderedDVVector(), ind->getOrderedDVVector()));
}
}
void
operator()(PopulationSPtr population, const boost::filesystem::path & save_dir)
{
int i = 0;
for(IndividualSPtr ind: *population)
{
boost::filesystem::path save_ind_dir = save_dir / ("individual_" + std::to_string(i++));
if (!boost::filesystem::exists(save_ind_dir)) boost::filesystem::create_directories(save_ind_dir);
ind->setObjectivesAndConstraints(eval(ind->getRealDVVector(), ind->getUnorderedDVVector(), ind->getOrderedDVVector(), save_ind_dir));
}
}
};
#endif /* EvaluationServer_h */