File: lp.rules

package info (click to toggle)
polymake 3.2r4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,564 kB
  • sloc: cpp: 153,464; perl: 40,590; ansic: 2,829; java: 2,654; python: 589; sh: 219; xml: 117; makefile: 63
file content (187 lines) | stat: -rw-r--r-- 6,351 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
186
187
#  Copyright (c) 1997-2018
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.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 (as used by cplex & Co.)
# and convert it to a [[Polytope<Scalar>]] object.
#
# **WARNING** The property FEASIBLE is **NOT** computed upon creation.
# This is done to avoid possibly long computation times before the object becomes available to the caller.
# This is **NOT** in keeping with standard practice in polymake, but after, all, these are linear programs
# and not polytopes.
#
# @tparam Scalar coordinate type of the resulting polytope; default is [[Rational]].
# @param 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 Int nocheck Do not automatically compute [[FEASIBLE]] for the polytope (recommended for very large LPs)
# @return Polytope<Scalar>

user_function lp2poly<Scalar=Rational>(String; Vector, String, {nocheck => 0}) {
   require LPparser;
   my ($filename, $testvec, $prefix, $options) = @_;

   my $parser = defined($testvec) 
                         ? new LPparser($filename, $testvec, $prefix) 
                         : new LPparser($filename);

   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;

   # FIXME #702: change the following kludge to 
   # $P->INEQUALITIES = new SparseMatrix<Scalar>($parser->Ineq)
   my @AoH = $parser->Ineq;
   my $Mineq = new SparseMatrix<Scalar>(scalar @AoH, $d);
   my $i=0;
   foreach my $ineq (@AoH) {
      keys %{$ineq};
      while (my ($k,$v) = each %{$ineq}) {
         $Mineq->[$i]->[$k] = $v;
      }
      $i++;
   }
   $P->INEQUALITIES = $Mineq;

   # FIXME #702: change the following kludge to 
   # $P->EQUATIONS = new SparseMatrix<Scalar>($parser->Eq)
   @AoH = $parser->Eq;
   my $MEq = new SparseMatrix<Scalar>(scalar @AoH, $d);
   $i=0;
   foreach my $eq (@AoH) {
      keys %{$eq};
      while (my ($k,$v) = each %{$eq}) {
         $MEq->[$i]->[$k] = $v;
      }
      $i++;
   }
   $P->EQUATIONS = $MEq;

   my $lp=new LinearProgram<Scalar>;
   $lp->description="Objective sense was ".($parser->objsense eq "+" ? "MAXIMIZE" : "MINIMIZE")."\n";

   # FIXME #702: change the following kludge to 
   # $P->LINEAR_OBJECTIVE = new SparseVector<Scalar>($parser->Obj)
   my $ObjHash = $parser->Obj;
   my $ObjVect = new SparseVector<Scalar>($d);
   keys %{$ObjHash};
   while (my ($k,$v) = each %{$ObjHash}) {
      $ObjVect->[$k] = $v;
   }
   $lp->LINEAR_OBJECTIVE=$ObjVect;

   if (@{$parser->Int}) {
      $lp->attach("INTEGER_VARIABLES", new Array<Bool>($parser->Int));
   }
   $P->commit unless($options->{nocheck});
   $P->add("LP",$lp);
   $P;
}


# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: