TCPSPSuite
ilp.hpp
1 #ifndef ILP_HPP
2 #define ILP_HPP
3 
4 #include "../contrib/ilpabstraction/src/common.hpp" // for ModelSt...
5 #include "../datastructures/maybe.hpp" // for Maybe
6 #include "../instance/solution.hpp" // for Solution
7 #include "../manager/timer.hpp" // for Timer
8 #include "../util/log.hpp" // for Log
9 
10 #include <chrono>
11 #include <iomanip> // for operator<<
12 #include <string> // for string
13 #include <vector> // for vector
14 class AdditionalResultStorage;
15 class Instance;
16 class SolverConfig;
17 class Traits;
18 
19 /* This is the basic discrete-time formulation of Kone et al. taken from
20  * 10.1016/j.cor.2009.12.011
21  *
22  * The objective function was modified to optimize for RACP instead of RCPSP.
23  * The (global only!) deadline is automatically upheld by the fact that the
24  * start-indicator variable is only created up to the point where the last job
25  * must start the latest.
26  */
27 template <class MIPSolverT>
28 class ILPBase {
29 public:
30  ILPBase(const Instance & instance, AdditionalResultStorage & additional,
31  const SolverConfig & sconf);
32  ~ILPBase();
33 
34  using MIPSolver = MIPSolverT;
35  using Model = typename MIPSolverT::Model;
36  using Variable = typename MIPSolverT::Variable;
37  using Expression = typename MIPSolverT::Expression;
38  using VarType = ilpabstraction::VariableType;
39  using ParamType = ilpabstraction::ParamType;
40  using ModelStatus = ilpabstraction::ModelStatus;
41  using Constraint = typename MIPSolverT::Constraint;
42  using MIPFeatures = ilpabstraction::Features;
43 
44  Maybe<double> get_lower_bound();
45 
46  static const Traits & get_requirements();
47 
48  Solution get_solution();
49 
50 protected:
51  const Instance & instance;
52  class Callback : public MIPSolverT::Callback {
53  public:
54  Callback(const Timer & timer, AdditionalResultStorage & additional_storage,
55  Log & l);
56  virtual void on_poll(typename MIPSolver::Callback::Context & ctx) override;
57  virtual void on_message(typename MIPSolver::Callback::Context & ctx,
58  std::string & message) override;
59 
60  private:
61  const double LOG_INTERVAL = 10; // seconds
62  const int REPEAT_HEADER = 30;
63 
64  void log_intermediate();
65  double last_intermediate_time = 0;
66 
67  const Timer & timer;
68  AdditionalResultStorage & additional_storage;
69 
70  double last_log;
71  int lines_before_header;
72  Log & l;
73  };
74 
75  void prepare_base_variables();
76  void prepare_pre();
77  void prepare_post();
78 
79  void base_run();
80 
81  // constraint (4)
82  void prepare_edge_constraints();
83 
84  // constraints 7-9 are upheld automatically
85 
86  // objective (3)
87  void prepare_objective();
88 
89  void do_initialization();
90  void solve(Maybe<unsigned int> time_limit);
91  void compute_values();
92 
93  Solution get_solution_by_start_vars();
94 
95  MIPSolver env;
96  Model model;
97 
98  /* Solution
99  *
100  * Must be set by the implementation if start_points are not being used
101  */
102  std::vector<Maybe<unsigned int>> computed_solution_start_times;
103 
104  /* Start points
105  * IMPLEMENTATION must define these for:
106  * * job dependencies
107  * * automatic solution retrieval
108  * If they are generated, implementation must make sure to introduce
109  * constraints on them! */
110  void generate_vars_start_points();
111  bool start_points_set;
112  std::vector<Variable> start_points;
113 
114  // Duration variables
115  // IMPLEMENTATION must define these
116  std::vector<Variable> duration_variables;
117 
118  // Maximum usage of the resources
119  // IMPLEMENTATION must define these
120  std::vector<Variable> max_usage_variables;
121 
122  // Window-not-modified indicators
123  // IMPLEMENTATION must define these
124  std::vector<Variable> window_not_modified_var;
125 
126  // IMPLEMENTATION must define these using constraints
127  Variable overshoot_cost_variable;
128  Variable investment_cost_variable;
129 
130  unsigned int earliest_release;
131  unsigned int latest_deadline;
132 
133  int timelimit;
134  int seed;
135  bool optimized;
136 
137  bool initialize_with_early;
138  bool collect_kappa_stats;
139 
140  decltype(std::chrono::high_resolution_clock::now()) prepare_started;
141  decltype(std::chrono::high_resolution_clock::now()) prepare_finished;
142 
143  static const Traits required_traits;
144 
145  AdditionalResultStorage & additional_storage;
146  const SolverConfig & sconf;
147 
148  Timer timer;
149  Callback cb;
150 
151  Log l;
152 };
153 
154 #endif
Instance
a TCPSP instance
Definition: instance.hpp:24
Solution
a soultion for a TCPSP instance
Definition: solution.hpp:17