File: generic_lp_client.h

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (114 lines) | stat: -rw-r--r-- 4,114 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Copyright (c) 1997-2024
   Ewgenij Gawrilow, Michael Joswig, and the polymake team
   Technische Universität Berlin, Germany
   https://polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
*/

#pragma once

#include "polymake/client.h"
#include "polymake/linalg.h"
#include "polymake/polytope/solve_LP.h"

namespace polymake { namespace polytope {

template <typename Scalar, typename Ineq>
Set<Int> initial_basis_from_known_vertex(const GenericMatrix<Ineq, Scalar>& H, const Vector<Scalar>& V)
{
   const Set<Int> zero_rows = orthogonal_rows(H, V);
   const Set<Int> basis_zero_rows = basis_rows(H.minor(zero_rows, All));
   if (basis_zero_rows.size() == H.cols()-1)
      return select(zero_rows, basis_zero_rows);
   return Set<Int>();
}

template <typename Scalar>
void store_LP_Solution(BigObject& p, BigObject& lp, bool maximize, const LP_Solution<Scalar>& S)
{
   if (S.status == LP_status::valid) {
      lp.take(maximize ? Str("MAXIMAL_VALUE") : Str("MINIMAL_VALUE")) << S.objective_value;
      lp.take(maximize ? Str("MAXIMAL_VERTEX") : Str("MINIMAL_VERTEX")) << S.solution;
      p.take("FEASIBLE") << true;

   } else if (S.status == LP_status::unbounded) {
      if (maximize)
         lp.take("MAXIMAL_VALUE") << std::numeric_limits<Scalar>::infinity();
      else
         lp.take("MINIMAL_VALUE") << -std::numeric_limits<Scalar>::infinity();
      p.take("FEASIBLE") << true;
   } else {
      p.take("FEASIBLE") << false;
   }

   if (S.lineality_dim >= 0)
      p.take("LINEALITY_DIM") << S.lineality_dim;
}

template <typename Scalar, typename Solver, typename=void>
struct allow_initial_basis : std::false_type {

   bool select_arg(bool feasibility_known, const Set<Int>&) const
   {
      return feasibility_known;
   }
};

template <typename Scalar, typename Solver>
struct allow_initial_basis<Scalar, Solver,
   accept_valid_type<decltype(std::declval<Solver>().solve(std::declval<Matrix<Scalar>>(), std::declval<Matrix<Scalar>>(),
                                                           std::declval<Vector<Scalar>>(), true, std::declval<Set<Int>>()))>> : std::true_type {

   const Set<Int>& select_arg(bool, const Set<Int>& initial_basis) const
   {
      return initial_basis;
   }
};

template <typename Scalar, typename Solver>
void generic_lp_client(BigObject& p, BigObject& lp, bool maximize, const Solver& LP_solver)
{
   std::string H_name;
   const Matrix<Scalar> H = LP_solver.needs_feasibility_known()
                            ? p.give_with_property_name("FACETS | INEQUALITIES", H_name)
                            : p.give("FACETS | INEQUALITIES"),
                        E = p.lookup("AFFINE_HULL | EQUATIONS");
   const Vector<Scalar> Obj = lp.give("LINEAR_OBJECTIVE");

   if (H.cols() != E.cols() &&
       H.cols() && E.cols())
      throw std::runtime_error("lp_client - dimension mismatch between Inequalities and Equations");

   Set<Int> initial_basis;
   const allow_initial_basis<Scalar, Solver> allow_basis{};

   if (allow_basis) {
      const Vector<Scalar> V = p.lookup("ONE_VERTEX");
      if (V.dim())
         initial_basis = E.rows() ? initial_basis_from_known_vertex(H/E, V) : initial_basis_from_known_vertex(H, V);
   }

   const bool feasibility_known = LP_solver.needs_feasibility_known() && H_name == "FACETS";

   const LP_Solution<Scalar> S = LP_solver.solve(H, E, Obj, maximize, allow_basis.select_arg(feasibility_known, initial_basis));
   store_LP_Solution(p, lp, maximize, S);
}

} }


// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: