Program Listing for File genetic_algorithm.hpp

Return to documentation for file (include/cvrp/genetic_algorithm.hpp)

#ifndef GENETIC_ALGORITHM_HPP
#define GENETIC_ALGORITHM_HPP

#include <unordered_set>

#include "cvrp/utils.hpp"

// Still need to account for case if nodes cannot be put into vehilces due to
// small number of vehicles in initial solution
class GASolution : public Solution {
 public:
  GASolution(const std::vector<Node>& nodes,
             const std::vector<Vehicle>& vehicles,
             const std::vector<std::vector<double>>& distanceMatrix,
             const int n_chromosomes = 10, const int generations = 100);
  explicit GASolution(const Problem& p, const int n_chromosomes = 10,
                      const int generations = 100);

  explicit GASolution(const Solution& s, const int n_chromosomes = 10,
                      const int generations = 100);

  void Solve() override;

 private:
  const int n_chromosomes_;
  const int generations_;
  const size_t n_nucleotide_pairs_;
  std::vector<double> costs_;
  const int n_vehicles_;
  std::vector<std::vector<int>> chromosomes_;
  std::vector<std::vector<int>> iterators_;
  int best_ = 0;

  void GenerateRandomSolutions();

  void GenerateGreedySolutions();

  std::vector<int> GenerateRandomSolution() const;

  std::vector<int> GenerateRandomIterSolution() const;

  void HGreXCrossover();

  void RandomSwap();

  void Mutate();

  void DeleteWorstChromosome();

  void RemoveSimilarSolutions();

  void AddBest();

  void MutateWhithinGene();

  void SwapWhithinGene();

  void InsertIterDist();

  bool MutateIterLeft(const int i_chromosome, const int j_in);

  bool MutateIterRight(const int i_chromosome, const int j_in);

  double CalculateCost(const int i) const;

  void CalculateTotalCost();

  int TournamentSelection(const int n = 10) const;

  int TournamentSelectionBad(const int n = 10) const;

  void DeleteBadChromosome();

  void InsertionBySimilarity();

  void DeleteRandomChromosome();

  void GenerateBestSolution();

  bool checkValidity(const int i) const;

  void MakeValid(const int i);
};

#endif  // GENETIC_ALGORITHM_HPP