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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
# 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.
#-------------------------------------------------------------------------------
object Polytope {
# @category Optimization
# Linear program applied to the polytope
property LP : LinearProgram<Scalar> : multiple;
# @notest Rule defined "in stock" - currently without use
rule LP.ABSTRACT_OBJECTIVE : VertexPerm.LP.ABSTRACT_OBJECTIVE, VertexPerm.PERMUTATION {
$this->LP->ABSTRACT_OBJECTIVE=permuted($this->VertexPerm->LP->ABSTRACT_OBJECTIVE, $this->VertexPerm->PERMUTATION);
}
rule LP.DIRECTED_GRAPH.ADJACENCY, LP.MAXIMAL_VALUE, LP.MINIMAL_VALUE, LP.MAXIMAL_FACE, LP.MINIMAL_FACE \
: LP.LINEAR_OBJECTIVE, GRAPH.ADJACENCY, VERTICES, FAR_FACE {
$this->LP->DIRECTED_GRAPH->ADJACENCY=dgraph($this, $this->LP);
}
weight 2.50;
precondition : POINTED;
precondition : FEASIBLE;
rule LP.DIRECTED_GRAPH.ADJACENCY, LP.MAXIMAL_VALUE, LP.MINIMAL_VALUE, LP.MAXIMAL_FACE, LP.MINIMAL_FACE \
: LP.ABSTRACT_OBJECTIVE, GRAPH.ADJACENCY, VERTICES {
$this->LP->DIRECTED_GRAPH->ADJACENCY=dgraph($this, $this->LP);
}
weight 2.50;
precondition : POINTED;
precondition : FEASIBLE;
rule LP.MAXIMAL_FACE, LP.MAXIMAL_VALUE : LP.LINEAR_OBJECTIVE, VERTICES, GRAPH.ADJACENCY, FAR_FACE {
pseudo_simplex($this, $this->LP, 1);
}
precondition : POINTED;
precondition : FEASIBLE;
rule LP.MINIMAL_FACE, LP.MINIMAL_VALUE : LP.LINEAR_OBJECTIVE, VERTICES, GRAPH.ADJACENCY, FAR_FACE {
pseudo_simplex($this, $this->LP, 0);
}
precondition : POINTED;
precondition : FEASIBLE;
rule LP.MAXIMAL_VERTEX : VERTICES, FAR_FACE, LP.MAXIMAL_FACE {
my $f=$this->LP->MAXIMAL_FACE-$this->FAR_FACE;
if (@$f) {
$this->LP->MAXIMAL_VERTEX=$this->VERTICES->[$f->[0]];
}
}
weight 0.10;
rule LP.MINIMAL_VERTEX : VERTICES, FAR_FACE, LP.MINIMAL_FACE {
my $f=$this->LP->MINIMAL_FACE-$this->FAR_FACE;
if (@$f) {
$this->LP->MINIMAL_VERTEX=$this->VERTICES->[$f->[0]];
}
}
weight 0.10;
rule ONE_VERTEX : LP.MINIMAL_VERTEX | LP.MAXIMAL_VERTEX {
$this->ONE_VERTEX=$this->LP->give("MINIMAL_VERTEX | MAXIMAL_VERTEX");
}
precondition : POINTED;
weight 0.10;
rule ONE_VERTEX, FEASIBLE : FACETS | INEQUALITIES, CONE_AMBIENT_DIM {
my $lp=$this->add("LP", temporary, LINEAR_OBJECTIVE => new Vector<Scalar>($this->CONE_AMBIENT_DIM));
$this->ONE_VERTEX=$lp->MINIMAL_VERTEX;
$this->FEASIBLE=defined($lp->MINIMAL_VERTEX);
}
weight 3.20;
rule VALID_POINT = ONE_VERTEX;
}
object LinearProgram {
rule RANDOM_EDGE_EPL : DIRECTED_GRAPH.ADJACENCY {
$this->RANDOM_EDGE_EPL=random_edge_epl($this->DIRECTED_GRAPH->ADJACENCY);
}
}
# @category Optimization
# Read a linear programming problem given in LP-Format or MILP-Format (as used by cplex & Co.)
# and convert it to a [[Polytope<Scalar>]] object with an added LP property or MILP property
#
# @tparam Scalar coordinate type of the resulting polytope; default is [[Rational]].
# @param [complete file] String file filename of a linear programming problem in LP-Format
# @param Vector testvec If present, after reading in each line of the LP it is checked whether testvec fulfills it
# @param String prefix If testvec is present, all variables in the LP file are assumed to have the form $prefix$i
# @option Bool nocheck Do not automatically compute [[FEASIBLE]] for the polytope (recommended for very large LPs)
# @option Bool create_lp Create an LP property regardless of the format of the given file. If the file has
# MILP-Format, the created LP property will have an attachment INTEGER_VARIABLES
# @return Polytope<Scalar>
user_function lp2poly<Scalar=Rational>(String; Vector, String, {nocheck => false, create_lp => false}) {
require LPparser;
my ($filename, $testvec, $prefix, $options) = @_;
my $parser = new LPparser($filename, $testvec, $prefix);
my $P = new Polytope<Scalar>($parser->name);
$P->description = "Linear problem converted from file " . $parser->LPfile . "\n";
my $labels = new Array<String>($parser->X);
my $d = $labels->size();
# Specifying the dimension first is required for empty inequalities and coordinate labels
$P->CONE_AMBIENT_DIM = $d;
$P->COORDINATE_LABELS = $labels;
push @{$parser->Ineq}, { cols => $d };
push @{$parser->Eq}, { cols => $d };
$parser->Obj->{_dim} = $d;
$P->INEQUALITIES = new SparseMatrix<Scalar>($parser->Ineq);
$P->EQUATIONS = new SparseMatrix<Scalar>($parser->Eq);
my $lp = new LinearProgram<Scalar>($parser->lp_name // ());
if (@{$parser->Int} && !$options->{create_lp}){
$lp = new MixedIntegerLinearProgram<Scalar>($parser->lp_name // ());
# Convert boolean Array of integer variables to a set for MILP
my $integer_variables = new Array<Bool>($parser->Int);
my $integer_set = new Set<Int>();
for(my $ind = 0; $ind < $integer_variables->size(); $ind++){
if($integer_variables->[$ind]){
$integer_set->collect($ind + 1);
}
}
$lp->INTEGER_VARIABLES = $integer_set;
}
$lp->description = "Objective sense was ".($parser->objsense eq "+" ? "MAXIMIZE" : "MINIMIZE")."\n";
$lp->LINEAR_OBJECTIVE = new SparseVector<Scalar>($parser->Obj);
if (@{$parser->Int} && $options->{create_lp}) {
$lp->attach("INTEGER_VARIABLES", new Array<Bool>($parser->Int));
}
$lp->attach("NAMES_OF_INEQUALITIES", new Array<String>($parser->Ineq_labels));
$lp->attach("NAMES_OF_EQUATIONS", new Array<String>($parser->Eq_labels));
$P->commit unless($options->{nocheck});
if (@{$parser->Int} && !$options->{create_lp}){
$P->add("MILP", $lp);
}
else{
$P->add("LP", $lp);
}
$P;
}
# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End:
|