lp_example.cpp
Go to the documentation of this file.
1 //=======================================================================
3 //
5 // accompanying file LICENSE_1_0.txt or copy at
7 //=======================================================================
16 #include "paal/lp/glp.hpp"
18
19 #include <iostream>
20
21 using LP = paal::lp::glp;
22
23 void print_solution(paal::lp::problem_type status, const LP &lp_instance) {
24  std::cout << "-----------------------------------------" << std::endl;
25  if (status == paal::lp::OPTIMAL) {
26  std::cout << "Optimal solution cost: " << lp_instance.get_obj_value()
27  << std::endl;
28  } else {
30  }
31  std::cout << lp_instance << std::endl;
32  // TODO example should show how to read a valuation of a variable, one
33  // has to search documentation quite extensively to find that
34 }
35
36 int main() {
37  // sample problem
38  LP lp_instance;
39
40  lp_instance.set_optimization_type(paal::lp::MAXIMIZE);
41  // add_column(column_cost, lower_bound, upper_bound, symbolic_name)
42  auto X = lp_instance.add_column(500, 0, paal::lp::lp_traits::PLUS_INF, "x");
43  auto Y = lp_instance.add_column(300, 0, paal::lp::lp_traits::PLUS_INF, "y");
44
45  // TODO example should should show how to create an expression with
46  // 0 or 1 variable. This is the most standard use case, we use that
47  // every time we create an expression in the loop
48  auto expr = X + Y;
49  std::cout << expr << std::endl;
50  std::cout << (expr >= 7) << std::endl;
52  auto row = lp_instance.add_row(expr <= 10);
53  lp_instance.add_row(15 <= 200 * X + 100 * Y <= 1200);
54
55  // solve the LP
56  auto status = lp_instance.solve_simplex();
57  print_solution(status, lp_instance);
58
60  expr += Y;
62  // modify row bound
63  lp_instance.set_row_upper_bound(row, 20);
64  // modify column bound
65  lp_instance.set_col_lower_bound(X, -50);
66  // modify row expression
67  lp_instance.set_row_expression(row, X + Y * 1.01);
68
69  // resolve the LP
70  status = lp_instance.resolve_simplex(paal::lp::DUAL);
71  print_solution(status, lp_instance);
72
73  // delete row
74  lp_instance.delete_row(row);
75
76  // resolve the LP
77  status = lp_instance.resolve_simplex();
78  print_solution(status, lp_instance);
79
80  return 0;
81 }
The common LP solvers base class. Responsible for:
Definition: lp_base.hpp:55
col_id add_column(double cost_coef=0, double lb=0., double ub=lp_traits::PLUS_INF, const std::string &name="")
Definition: lp_base.hpp:92
problem_type
LP problem type.
RowIter delete_row(RowIter row)
Definition: lp_base.hpp:178
row_id add_row(const double_bounded_expression &constraint=double_bounded_expression{}, const std::string &name="")
Definition: lp_base.hpp:109