File: visual_graph.rules

package info (click to toggle)
polymake 4.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 31,528 kB
  • sloc: cpp: 152,204; perl: 43,222; javascript: 30,700; ansic: 2,937; java: 2,654; python: 641; sh: 244; xml: 117; makefile: 62
file content (311 lines) | stat: -rw-r--r-- 11,898 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#  Copyright (c) 1997-2020
#  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.
#-------------------------------------------------------------------------------


# @topic objects/Visual::PolytopeGraph
# @category Visualization
# Visualization of the graph of a polyhedron.
# @relates objects/Polytope

package Visual::PolytopeGraph;

use Polymake::Struct (
   [ '@ISA' => 'Container' ],
   [ '$Polytope' => '#%' ],
   [ '$bounded' => '#%' ],
);

# where to keep the view transformation matrix etc.
method representative { $_[0]->Polytope }

method basis_graph { $_[0]->elements->[0] }

# In the spring embedding of the graph with a superposed linear objective function,
# controls the relative force magnitude along the z-axis
custom $embedding_objective_factor=1;

sub LP_influence_on_embedding {
   my ($self, $lp)=@_;
   if (instanceof Visual::GraphEmbedding($self->basis_graph->Coord)) {
      my $obj=$lp->lookup("ABSTRACT_OBJECTIVE");
      if (!defined($obj)) {
         if ($self->bounded) {
            $obj=$self->Polytope->VERTICES * $lp->LINEAR_OBJECTIVE;
         } else {
            $obj=objective_values_for_embedding($self->Polytope, $lp);
         }
      }
      if ($self->basis_graph->Graph->ADJACENCY->has_gaps) {
         $obj=$obj->slice(nodes($self->basis_graph->Graph->ADJACENCY));
      }
      $self->basis_graph->Coord->merge_options('z-ordering' => convert_to<Float>($obj), 'z-factor' => $embedding_objective_factor);
   }
}

# Illustrate the behavior of a linear objective function on the polytope.
# Color the nodes according to the value the objective function takes
# on the vertices.
# 
# The spring embedder applies an additional force, which tries to arrange the nodes in the z-axis direction
# corresponding to the objective function values.
# 
# @param LinearProgram lp a LinearProgram object attached to the polytope.
# @option [complete color] Color min minimal face color (default: yellow)
# @option [complete color] Color max maximal face color (default: red)
# @return Visual::PolytopeGraph

user_method VERTEX_COLORS(; LinearProgram { min => $Visual::Color::min, max => $Visual::Color::max }) {
   my ($self, $lp, $decor)=@_;
   if (defined $lp) {
      if ($lp->parent != $self->Polytope) {
         croak("LinearProgram object does not belong to this Polytope");
      }
   } else {
      $lp=$self->Polytope->LP;
   }
   $_=get_RGB($_) for values %$decor;
   $self->basis_graph->NodeColor=vertex_colors($self->Polytope, $lp, $decor);
   LP_influence_on_embedding($self,$lp);
   visualize($self);
}

# Illustrate the behavior of a linear objective function on the polytope.
# The vertices belonging to [[MINIMAL_FACE]] and [[MAXIMAL_FACE]] are drawn in distinct colors
# 
# The spring embedder applies an additional force, which tries to arrange the nodes in the z-axis direction
# corresponding to the objective function values.
# 
# @param LinearProgram lp a LinearProgram object attached to the polytope
# @option [complete color] Color min minimal face decoration (default: yellow nodes)
# @option [complete color] Color max maximal face decoration (default: red nodes)
# @return Visual::PolytopeGraph

user_method MIN_MAX_FACE(; LinearProgram { min => $Visual::Color::min, max => $Visual::Color::max }) {
   my ($self, $lp, $decor)=@_;
   if (defined $lp) {
      if ($lp->parent != $self->Polytope) {
         croak("LinearProgram object does not belong to this Polytope");
      }
   } else {
      $lp=$self->Polytope->LP;
   }
   foreach (values %$decor) {
      unless (is_like_hash($_)) {
         my $c = get_RGB($_);
         $_ = { NodeColor => $c };
      }
   }

   if (!$self->bounded || isfinite($lp->MINIMAL_VALUE)) {
      $self->basis_graph->add_node_subset($lp->MINIMAL_FACE, $decor->{min});
   }
   if (!$self->bounded || isfinite($lp->MAXIMAL_VALUE)) {
      $self->basis_graph->add_node_subset($lp->MAXIMAL_FACE, $decor->{max});
   }
   LP_influence_on_embedding($self,$lp);
   visualize($self);
}

# Show the growth direction of a linear objective function via arrowed edges.
# @param LinearProgram lp a LinearProgram object attached to the polytope
# @return Visual::PolytopeGraph

user_method DIRECTED_GRAPH(; LinearProgram) {
   my ($self, $lp)=@_;
   if (defined $lp) {
      if ($lp->parent != $self->Polytope) {
         croak("LinearProgram object does not belong to this Polytope");
      }
   } else {
      $lp=$self->Polytope->LP;
   }
   $self->Name.=" DIRECTED with LP " . $lp->name;
   my $G=$self->basis_graph;
   if ($G->Graph->ADJACENCY->has_gaps) {
      $G->Graph = new graph::Graph(ADJACENCY => induced_subgraph($lp->DIRECTED_GRAPH->ADJACENCY, nodes($G->Graph->ADJACENCY)));
   } else {
      $G->Graph=$lp->DIRECTED_GRAPH;
   }
   $G->ArrowStyle=1;
   visualize($self);
}



###########################################################################################

object Polytope {

# @category Visualization
# Visualize the [[GRAPH]] of a polyhedron.
# @option Int seed random seed value for the string embedder
# @return Visual::PolytopeGraph

user_method VISUAL_GRAPH(%Visual::Graph::decorations, { seed => undef }) {
   my ($this, $decor, $seed)=@_;
   my $VG=$this->GRAPH->VISUAL( Name => $this->name,
                                (exists $decor->{NodeBorderColor} || exists $decor->{VertexColor} ? () : (NodeColor => $Visual::Color::vertices)),
                                $decor, $seed );
   visualize( new Visual::PolytopeGraph( Name => "GRAPH of " . $this->name,
                                         Polytope => $this,
                                         $VG ));
}


# @category Visualization
# Visualize the [[DUAL_GRAPH]] of a polyhedron.
# @option Int seed random seed value for the string embedder
# @return Visual::Graph

user_method VISUAL_DUAL_GRAPH(%Visual::Graph::decorations, { seed => undef }) {
   my ($this, $decor, $seed)=@_;
   $this->DUAL_GRAPH->VISUAL( Name => $this->name,
                              Title => "DUAL_GRAPH of ".$this->name,
                              (exists $decor->{NodeBorderColor} || exists $decor->{VertexColor} ? () : (NodeColor => $Visual::Color::vertices)),
                              $decor, $seed );
}

}
#########################################################################################
#
#  Face lattice visualization
#

# @topic objects/Visual::PolytopeLattice
# @category Visualization
# Visualization of the [[HASSE_DIAGRAM]] of a polyhedron as a multi-layer graph..
# @relates objects/Polytope

package Visual::PolytopeLattice;

use Polymake::Struct (
   [ '@ISA' => 'Container' ],
   [ '$Polytope' => '#%' ],
);

# where to keep the view transformation matrix etc.
method representative { $_[0]->Polytope }

method basis_lattice { $_[0]->elements->[0] }


# Illustrate the behavior of a linear objective function on the polytope.
# Draw the filters of the [[MAXIMAL_FACE]] and [[MINIMAL_FACE]] in distinct colors.
#
# @param LinearProgram lp a LinearProgram object attached to the polytope
# @option [complete color] Color min minimal face decoration (default: yellow border and ingoing edges)
# @option [complete color] Color max maximal face decoration (default: red border and ingoing edges)
# @return Visual::PolytopeLattice

user_method MIN_MAX_FACE(; LinearProgram { min => $Visual::Color::min, max => $Visual::Color::max }) {
   my ($self, $lp, $decor)=@_;
   if (defined $lp) {
      if ($lp->parent != $self->Polytope) {
         croak("LinearProgram object does not belong to this Polytope");
      }
   } else {
      $lp=$self->Polytope->LP;
   }
   foreach (values %$decor) {
      unless (is_like_hash($_)) {
         my $c = get_RGB($_);
         $_ = { NodeBorderColor => $c, EdgeColor => $c };
      }
      $_->{show_filter}=1;
   }

   if (defined (my $min_face=$lp->MINIMAL_FACE)) {
      $self->basis_lattice->add_subcomplex($self->Polytope->HASSE_DIAGRAM, [ $min_face ], $decor->{min});
   }
   if (defined (my $max_face=$lp->MAXIMAL_FACE)) {
      $self->basis_lattice->add_subcomplex($self->Polytope->HASSE_DIAGRAM, [ $max_face ], $decor->{max});
   }
   visualize($self);
}

#########################################################################################

object Polytope {

# @category Visualization
# Visualize the [[HASSE_DIAGRAM]] of a polyhedron as a multi-layer graph.
# @option Int seed random seed value for the node placement
# @return Visual::PolytopeLattice

user_method VISUAL_FACE_LATTICE(%Visual::Lattice::decorations, { seed => undef }) : \
            HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.DECORATION, HASSE_DIAGRAM.INVERSE_RANK_MAP, HASSE_DIAGRAM.TOP_NODE, HASSE_DIAGRAM.BOTTOM_NODE {
   my ($this, $decor, $seed)=@_;
   $decor->{Name} ||= $this->name;
   visualize( new Visual::PolytopeLattice( Name => "Face lattice of ".$this->name,
                                           Polytope => $this,
                                           $this->HASSE_DIAGRAM->VISUAL(AtomLabels=>$this->lookup("VERTEX_LABELS"),
                                                                        $decor,$seed)
                                         ));
}


# @category Visualization
# Visualize the dual [[HASSE_DIAGRAM|face lattice]] of a polyhedron as a multi-layer graph.
# @option Int seed random seed value for the node placement
# @return Visual::PolytopeLattice

user_method VISUAL_DUAL_FACE_LATTICE(%Visual::Lattice::decorations, { seed => undef }) : \
            HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.DECORATION, HASSE_DIAGRAM.INVERSE_RANK_MAP, HASSE_DIAGRAM.TOP_NODE, HASSE_DIAGRAM.BOTTOM_NODE {
   my ($this, $decor, $seed)=@_;
   $decor->{Name} ||= $this->name;
   visualize( new Visual::PolytopeLattice( Name => "Dual face lattice of ".$this->name,
                                           Polytope => $this,
                                           $this->HASSE_DIAGRAM->VISUAL_DUAL(AtomLabels=>$this->lookup("FACET_LABELS"),
                                                                             $decor,$seed)
                                         ));
}

}

object Cone {

# @category Visualization
# Visualize the [[HASSE_DIAGRAM]] of a cone as a multi-layer graph.
# @option Int seed random seed value for the node placement
# @return Visual::Lattice

user_method VISUAL_FACE_LATTICE(%Visual::Lattice::decorations, { seed => undef }) : \
            HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.DECORATION, HASSE_DIAGRAM.INVERSE_RANK_MAP, HASSE_DIAGRAM.TOP_NODE, HASSE_DIAGRAM.BOTTOM_NODE {
   my ($this, $decor, $seed)=@_;
   $decor->{Name} ||= $this->name;
   $this->HASSE_DIAGRAM->VISUAL(AtomLabels=>$this->lookup("RAY_LABELS"),$decor,$seed);
}


# @category Visualization
# Visualize the dual [[HASSE_DIAGRAM|face lattice]] of a cone as a multi-layer graph.
# @option Int seed random seed value for the node placement
# @return Visual::Lattice

user_method VISUAL_DUAL_FACE_LATTICE(%Visual::Lattice::decorations, { seed => undef }) : \
            HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.DECORATION, HASSE_DIAGRAM.INVERSE_RANK_MAP, HASSE_DIAGRAM.TOP_NODE, HASSE_DIAGRAM.BOTTOM_NODE {
   my ($this, $decor, $seed)=@_;
   $decor->{Name} ||= $this->name;
   $this->HASSE_DIAGRAM->VISUAL_DUAL(AtomLabels=>$this->lookup("FACET_LABELS"),$decor,$seed);
}

}

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