dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
ScrambleMutation.h
Go to the documentation of this file.
1 
11 #ifndef DIGNEA_SCRAMBLEMUTATION_H
12 #define DIGNEA_SCRAMBLEMUTATION_H
13 
14 #include <dignea/core/Mutation.h>
15 #include <dignea/utilities/random/PseudoRandom.h>
16 
17 #include <algorithm>
18 #include <random>
25 template <class S>
26 class ScrambleMutation : public Mutation<S> {
27  public:
28  ScrambleMutation() = default;
29 
30  virtual ~ScrambleMutation() = default;
31 
32  void run(S &ind, const double &probability, Problem<S> *) override;
33 
34  std::string getName() const override { return "Scramble Mutation"; };
35 };
36 
48 template <class S>
49 void ScrambleMutation<S>::run(S &ind, const double &probability,
50  Problem<S> *problem) {
51  int start = 0;
52  int end = 0;
53  while ((start == end)) {
54  // Cannot change first and last element
55  // -2 to skip the last element too
56  start = PseudoRandom::randInt(1, ind.getNumberOfVars() - 2);
57  end = PseudoRandom::randInt(1, ind.getNumberOfVars() - 2);
58  }
59  try {
60  std::random_device rd;
61  std::default_random_engine gen(rd());
62  vector vars = ind.getVariables();
63  std::shuffle(vars.begin() + start, vars.begin() + end, gen);
64  ind.setVariables(vars);
65  } catch (OutOfRange const &err) {
66  std::string msg =
67  std::string(err.what()) +
68  " from Scramble operator with start: " + to_string(start).c_str() +
69  " and end: " + to_string(end).c_str() +
70  " with ind.vars().size() = " + to_string(ind.getNumberOfVars());
71  throw OutOfRange(msg);
72  }
73 }
74 
75 #endif // DIGNEA_Scramble_H
Abstract Mutation interface.
Definition: Mutation.h:21
Class to represent a Problem in the tool. It includes the basic information for a problem a few metho...
Definition: Problem.h:29
static int randInt(int minBound, int maxBound)
Returns a random integer int the range [minBound, maxBound].
Definition: PseudoRandom.cpp:38
Scramble Mutation Operator for Permutation Based Problems like TSP. Fixes the first and last genes to...
Definition: ScrambleMutation.h:26
void run(S &ind, const double &probability, Problem< S > *) override
Performs Scramble Mutation Operation.
Definition: ScrambleMutation.h:49