Add license to examples
[linpy.git] / examples / squares.py
1 #!/usr/bin/env python3
2 #
3 # Copyright 2014 MINES ParisTech
4 #
5 # This file is part of LinPy.
6 #
7 # LinPy is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # LinPy is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with LinPy. If not, see <http://www.gnu.org/licenses/>.
19
20 from linpy import *
21 import matplotlib.pyplot as plt
22 from matplotlib import pylab
23
24 a, x, y, z = symbols('a x y z')
25
26 sq1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2)
27 sq2 = Le(2, x) & Le(x, 4) & Le(2, y) & Le(y, 4)
28 sq3 = Le(0, x) & Le(x, 3) & Le(0, y) & Le(y, 3)
29 sq4 = Le(1, x) & Le(x, 2) & Le(1, y) & Le(y, 2)
30 sq5 = Le(1, x) & Le(x, 2) & Le(1, y)
31 sq6 = Le(1, x) & Le(x, 2) & Le(1, y) & Le(y, 3)
32 sq7 = Le(0, x) & Le(x, 2) & Le(0, y) & Eq(z, 2) & Le(a, 3)
33 p = Le(2*x+1, y) & Le(-2*x-1, y) & Le(y, 1)
34
35 universe = Polyhedron([])
36 q = sq1 - sq2
37 e = Empty
38
39 print('sq1 =', sq1) #print correct square
40 print('sq2 =', sq2) #print correct square
41 print('sq3 =', sq3) #print correct square
42 print('sq4 =', sq4) #print correct square
43 print('universe =', universe) #print correct square
44 print()
45 print('¬sq1 =', ~sq1) #test complement
46 print()
47 print('sq1 + sq1 =', sq1 + sq2) #test addition
48 print('sq1 + sq2 =', Polyhedron(sq1 + sq2)) #test addition
49 print()
50 print('universe + universe =', universe + universe)#test addition
51 print('universe - universe =', universe - universe) #test subtraction
52 print()
53 print('sq2 - sq1 =', sq2 - sq1) #test subtraction
54 print('sq2 - sq1 =', Polyhedron(sq2 - sq1)) #test subtraction
55 print('sq1 - sq1 =', Polyhedron(sq1 - sq1)) #test subtraction
56 print()
57 print('sq1 ∩ sq2 =', sq1 & sq2) #test intersection
58 print('sq1 ∪ sq2 =', sq1 | sq2) #test union
59 print()
60 print('sq1 ⊔ sq2 =', Polyhedron(sq1 | sq2)) # test convex union
61 print()
62 print('check if sq1 and sq2 disjoint:', sq1.isdisjoint(sq2)) #should return false
63 print()
64 print('sq1 disjoint:', sq1.disjoint()) #make disjoint
65 print('sq2 disjoint:', sq2.disjoint()) #make disjoint
66 print()
67 print('is square 1 universe?:', sq1.isuniverse()) #test if square is universe
68 print('is u universe?:', universe.isuniverse()) #test if square is universe
69 print()
70 print('is sq1 a subset of sq2?:', sq1.issubset(sq2)) #test issubset()
71 print('is sq4 less than sq3?:', sq4.__lt__(sq3)) # test lt(), must be a strict subset
72 print()
73 print('lexographic min of sq1:', sq1.lexmin()) #test lexmin()
74 print('lexographic max of sq1:', sq1.lexmax()) #test lexmin()
75 print()
76 print('lexographic min of sq2:', sq2.lexmin()) #test lexmax()
77 print('lexographic max of sq2:', sq2.lexmax()) #test lexmax()
78 print()
79 print('Polyhedral hull of sq1 + sq2 is:', q.aspolyhedron()) #test polyhedral hull
80 print()
81 print('is sq1 bounded?', sq1.isbounded()) #bounded should return True
82 print('is sq5 bounded?', sq5.isbounded()) #unbounded should return False
83 print()
84 print('sq6:', sq6)
85 print('sample Polyhedron from sq6:', sq6.sample())
86 print()
87 print('sq7 with out constraints involving y and a', sq7.project([a, z, x, y]))
88 print()
89 print('the verticies for s are:', p.vertices())
90
91
92 # plotting the intersection of two squares
93 square1 = Le(0, x) & Le(x, 2) & Le(0, y) & Le(y, 2)
94 square2 = Le(1, x) & Le(x, 3) & Le(1, y) & Le(y, 3)
95
96 fig = plt.figure()
97 plot = fig.add_subplot(1, 1, 1, aspect='equal')
98 square1.plot(plot, facecolor='red', alpha=0.3)
99 square2.plot(plot, facecolor='blue', alpha=0.3)
100
101 squares = Polyhedron(square1 + square2)
102 squares.plot(plot, facecolor='blue', alpha=0.3)
103
104 pylab.show()