5 #ifndef DIGNEA_SOLUTION_H
6 #define DIGNEA_SOLUTION_H
8 #include <dignea/utilities/exceptions/OutOfRange.h>
12 #include <nlohmann/json.hpp>
24 template <
typename V,
typename O>
31 Solution(
const int &nVars,
const int &nObjs);
33 Solution(
const int &nVars,
const int &nObjs,
const int &nCons);
41 bool operator==(
const Solution &other)
const;
50 virtual void setObjectives(
const vector<O> &objs);
57 virtual const vector<V> &
getVariables()
const {
return variables; }
59 virtual void setVariables(
const vector<V> &vars);
68 virtual void setConstraints(
const vector<O> &constr);
82 this->nVars = numberOfVars;
98 this->nObjs = numberOfObjs;
114 this->nCons = nConstraints;
123 virtual void setRank(
int id) { this->rank = id; }
131 virtual int getRank()
const {
return this->rank; }
147 virtual void setVarAt(
const int &,
const V &v);
149 virtual void setObjAt(
const int &,
const O &obj);
151 virtual void setConstAt(
const int &,
const O &cons);
153 virtual V getVarAt(
const int &index)
const;
155 virtual O getObjAt(
const int &index)
const;
157 virtual O getConstAt(
const int &index)
const;
191 virtual json to_json()
const;
216 template <
typename V,
typename O>
222 constraintCoeff(0.0f),
227 crowDistance(-1.0) {}
238 template <
typename V,
typename O>
244 constraintCoeff(0.0f),
250 variables.resize(nVars, (V)0);
251 objectives.resize(nObjs, (O)0.0);
264 template <
typename V,
typename O>
270 constraintCoeff(0.0f),
288 template <
typename V,
typename O>
293 fitness(copy.fitness),
294 constraintCoeff(copy.constraintCoeff),
299 crowDistance(copy.crowDistance) {
303 for (
int i = 0; i <
nVars; i++) {
306 for (
int i = 0; i <
nObjs; i++) {
309 for (
int i = 0; i <
nCons; i++) {
322 template <
typename V,
typename O>
324 if (copy ==
nullptr) {
326 "Copy Solution is nullptr in Solution(Solution* copy)"));
328 this->nVars = copy->
nVars;
329 this->nObjs = copy->
nObjs;
330 this->nCons = copy->
nCons;
333 this->variables.resize(this->nVars);
334 this->objectives.resize(this->nObjs);
335 this->constraints.resize(this->nCons);
336 this->rank = copy->rank;
337 this->crowDistance = copy->crowDistance;
338 for (
int i = 0; i < this->nVars; i++) {
341 for (
int i = 0; i < this->nObjs; i++) {
344 for (
int i = 0; i < this->nCons; i++) {
349 template <
typename V,
typename O>
354 objectives.shrink_to_fit();
355 variables.shrink_to_fit();
356 constraints.shrink_to_fit();
367 template <
typename V,
typename O>
369 if (index < 0 || index >= this->nVars) {
370 std::string where =
"setVarAt with index = " + to_string(index);
371 throw(OutOfRange(where));
373 this->variables[index] = v;
384 template <
typename V,
typename O>
386 if (index < 0 || index >= this->nObjs) {
387 std::string where =
"setObjAt with index = " + to_string(index);
388 throw(OutOfRange(where));
390 this->objectives[index] = obj;
401 template <
typename V,
typename O>
403 if (index < 0 || index >= this->nVars) {
404 std::string where =
"getVarAt with index = " + to_string(index);
405 throw(OutOfRange(where));
407 return this->variables[index];
418 template <
typename V,
typename O>
420 if (index < 0 || index >= this->nObjs) {
421 std::string where =
"getObjAt with index = " + to_string(index);
422 throw(OutOfRange(where));
424 return this->objectives[index];
435 template <
typename V,
typename O>
437 if (index < 0 || index >= this->nCons) {
438 std::string where =
"setConstAt with index = " + to_string(index);
439 throw(OutOfRange(where));
441 this->constraints[index] = value;
452 template <
typename V,
typename O>
454 if (index < 0 || index >= this->nCons) {
455 std::string where =
"getConstAt with index = " + to_string(index);
456 throw(OutOfRange(where));
458 return this->constraints[index];
468 template <
typename V,
typename O>
470 if (objs.size() != (
unsigned int)this->nObjs) {
471 std::string where =
"objs.size() != nObjs in Solution::setObjectives";
472 throw(OutOfRange(where));
474 this->objectives = objs;
484 template <
typename V,
typename O>
486 if (vars.size() != (
unsigned int)this->nVars) {
487 auto vsize = vars.size();
488 std::string where =
"vars.size() = " + to_string(vsize) +
489 " != nVars = " + to_string(this->nVars) +
490 " in Solution::setVariables";
491 throw(OutOfRange(where));
493 this->variables = vars;
503 template <
typename V,
typename O>
505 if (constr.size() != (
unsigned int)this->nCons) {
507 "constr.size() != nConst in Solution::setConstraints";
508 throw(OutOfRange(where));
510 this->constraints = constr;
521 template <
typename V,
typename O>
535 crowDistance = copy.crowDistance;
548 template <
typename V,
typename O>
550 if (other.
fitness != this->fitness) {
553 if ((this->nVars != other.
nVars) || (this->nObjs != other.
nObjs) ||
554 (this->nCons != other.
nCons)) {
557 for (
int i = 0; i < other.
nVars; i++) {
558 if (other.
variables[i] != this->variables[i]) {
562 for (
int i = 0; i < other.
nObjs; i++) {
563 if (other.
objectives[i] != this->objectives[i]) {
567 for (
int i = 0; i < other.
nCons; i++) {
568 if (other.
constraints[i] != this->constraints[i]) {
575 if (other.rank != this->rank) {
578 if (other.crowDistance != this->crowDistance) {
590 template <
typename V,
typename O>
593 data[
"num_vars"] = this->nVars;
594 data[
"num_objs"] = this->nObjs;
595 data[
"num_cons"] = this->nCons;
596 data[
"fitness"] = this->fitness;
597 data[
"const_coeff"] = this->constraintCoeff;
598 data[
"vars"] = this->variables;
599 data[
"objs"] = this->objectives;
600 data[
"cons"] = this->constraints;
601 data[
"rank"] = this->rank;
602 data[
"crow_distance"] = this->crowDistance;
nlohmann::json json
Definition: MinKnap.h:85
Class to represent a solution to the optimization problems in the tool.
Definition: Solution.h:25
virtual float getFitness() const
Get the fitness of the solution.
Definition: Solution.h:164
int nVars
Definition: Solution.h:194
virtual const vector< V > & getVariables() const
Get the variables of the solution.
Definition: Solution.h:57
virtual O getConstAt(const int &index) const
Returns the ith constraint value if exists.
Definition: Solution.h:453
vector< V > variables
Definition: Solution.h:201
int nCons
Definition: Solution.h:196
int nObjs
Definition: Solution.h:195
virtual void setObjectives(const vector< O > &objs)
Updates the objective values of the solution.
Definition: Solution.h:469
virtual void setCrowDistance(float d)
Set the Crow Distance object.
Definition: Solution.h:138
virtual int getNumberOfCons() const
Get the number of constraint in the solution.
Definition: Solution.h:106
void setConstraintCoeff(float constraintCoeff)
Set the constraint coefficient value.
Definition: Solution.h:187
virtual void setFitness(const float &f)
Set the fitness of the solution.
Definition: Solution.h:171
Solution & operator=(const Solution ©)
Copies a solution using the assignment operator.
Definition: Solution.h:522
virtual json to_json() const
Creates and returns a JSON representation of the solution.
Definition: Solution.h:591
virtual void setNumberOfCons(const int &nConstraints)
Set the number of constraints of the solution.
Definition: Solution.h:113
virtual void setVarAt(const int &, const V &v)
Updates the ith variable of the solution with the value v.
Definition: Solution.h:368
virtual O getObjAt(const int &index) const
Returns the ith objective value if exists.
Definition: Solution.h:419
virtual void setNumberOfVars(const int &numberOfVars)
Set the number of variables (dimension) of the solution.
Definition: Solution.h:81
virtual void setObjAt(const int &, const O &obj)
Updates the ith objective with the value obj.
Definition: Solution.h:385
virtual float getCrowDistance() const
Get the Crow Distance object.
Definition: Solution.h:145
float fitness
Definition: Solution.h:197
virtual const vector< O > & getConstraints() const
Get the constraint values of the solution.
Definition: Solution.h:66
virtual void setConstAt(const int &, const O &cons)
Updates the ith constraint value with the value.
Definition: Solution.h:436
vector< O > objectives
Definition: Solution.h:200
vector< O > constraints
Definition: Solution.h:202
float constraintCoeff
Definition: Solution.h:198
virtual int getRank() const
Get the identifier of the solution in the population. Only used in the Non-Dominated Sorting operator...
Definition: Solution.h:131
bool operator==(const Solution &other) const
Compares whether two solutions are equal.
Definition: Solution.h:549
virtual void setRank(int id)
Set the identifier of the solution in the population. Only used in the Non-Dominated Sorting operator...
Definition: Solution.h:123
virtual void setConstraints(const vector< O > &constr)
Updates the constraints of the solution.
Definition: Solution.h:504
float getConstraintCoeff() const
Get the constraint coefficient of the solutions. This value means different for every problem....
Definition: Solution.h:180
Solution()
Creates a Solution with all parameters set to zero.
Definition: Solution.h:217
virtual int getNumberOfObjs() const
Get the number of objectives of the solution.
Definition: Solution.h:90
virtual void setNumberOfbjs(const int &numberOfObjs)
Set the number of objectives of the solution.
Definition: Solution.h:97
virtual const vector< O > & getObjectives() const
Returns the objective values of the solution.
Definition: Solution.h:48
virtual int getNumberOfVars() const
Get the number of variables (dimension) of the solution.
Definition: Solution.h:74
virtual V getVarAt(const int &index) const
Returns the ith variable if its in the valid range.
Definition: Solution.h:402
virtual void setVariables(const vector< V > &vars)
Updates the variable values (dimension) of the solution.
Definition: Solution.h:485