Program Listing for File utils.hpp

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

#ifndef UTILS_HPP
#define UTILS_HPP

#include <string>
#include <tuple>
#include <vector>
struct Node {
 public:
  int x_, y_, id_, demand_;
  bool is_routed_;

  Node(const int x = 0, const int y = 0, const int id = 0, const int demand = 0,
       const bool is_routed = true)
      : x_(x), y_(y), id_(id), demand_(demand), is_routed_(is_routed) {}

  friend std::ostream &operator<<(std::ostream &os, const Node &node);
};

std::ostream &operator<<(std::ostream &os, const Node &node);

struct Vehicle {
 public:
  int id_, load_, capacity_;
  double cost_ = 0;
  std::vector<int> nodes_;

  Vehicle(const int id = 0, const int load = 0, const int capacity = 0)
      : id_(id), load_(load), capacity_(capacity) {}

  friend std::ostream &operator<<(std::ostream &os, const Vehicle &v);

  void CalculateCost(const std::vector<std::vector<double>> &distanceMatrix);
};

std::ostream &operator<<(std::ostream &os, const Vehicle &v);

void PrintVehicleRoute(const Vehicle &v);

struct Problem {
 public:
  Problem(const int noc = 1000, const int demand_range = 40, const int nov = 50,
          const int capacity = 800, const int grid_range = 1000,
          std::string distribution = "uniform", const int n_clusters = 5,
          const int cluster_range = 10);

  std::vector<Node> nodes_;
  std::vector<Vehicle> vehicles_;
  std::vector<std::vector<double>> distanceMatrix_;
  Node depot_;
  int capacity_;
};

// Solution class should not call problems's constructor so not inheriting.
class Solution {
 public:
  Solution(std::vector<Node> nodes, const std::vector<Vehicle> &vehicles,
           std::vector<std::vector<double>> distanceMatrix);

  explicit Solution(const Problem &p);

  Solution(const Solution &s) = default;

  Solution &operator=(const Solution &s) = default;

  Solution(Solution &&s) = default;

  Solution &operator=(Solution &&s) = default;

  virtual ~Solution() = default;

  void CreateInitialSolution();

  bool CheckSolutionValid() const;

  virtual void Solve() = 0;

  std::tuple<bool, Node> find_closest(const Vehicle &v) const;

  void PrintSolution(const std::string &option = "") const;

  std::vector<Node> GetNodes() const {
    return nodes_;
  }

  std::vector<Vehicle> GetVehicles() const {
    return vehicles_;
  }

 protected:
  std::vector<Node> nodes_;
  std::vector<Vehicle> vehicles_;
  std::vector<std::vector<double>> distanceMatrix_;
  Node depot_;
  int capacity_;
};

#endif  // UTILS_HPP