-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonic_dsl.py
More file actions
120 lines (99 loc) · 3.74 KB
/
pythonic_dsl.py
File metadata and controls
120 lines (99 loc) · 3.74 KB
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
from advanced_simplex import Simplex
from functions import AlgebraicExpression, ObjectiveFunction
class Expression:
def __init__(self, terms=None, const=0):
self.terms = terms if terms else {}
self.const = const
def __neg__(self):
new_terms = {var: -coef for var, coef in self.terms.items()}
return Expression(new_terms, -self.const)
def __add__(self, other):
new_terms = self.terms.copy()
if isinstance(other, Expression):
for var, coef in other.terms.items():
new_terms[var] = new_terms.get(var, 0) + coef
return Expression(new_terms, self.const + other.const)
else:
return Expression(new_terms, self.const + other)
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
new_terms = self.terms.copy()
if isinstance(other, Expression):
for var, coef in other.terms.items():
new_terms[var] = new_terms.get(var, 0) - coef
return Expression(new_terms, self.const - other.const)
else:
return Expression(new_terms, self.const - other)
def __rsub__(self, other):
expr = self.__sub__(other)
expr.const *= -1
for var in expr.terms:
expr.terms[var] *= -1
return expr
def __mul__(self, other):
new_terms = {var: coef * other for var, coef in self.terms.items()}
return Expression(new_terms, self.const * other)
def __rmul__(self, other):
return self.__mul__(other)
def __le__(self, other):
return (self, "<=", other)
def __ge__(self, other):
return (self, ">=", other)
def __eq__(self, other):
return (self, "==", other)
class Var(Expression):
def __init__(self, name, low=None, cat='Continuous'):
super().__init__({name: 1})
self.name = name
self.low = low
self.cat = cat
class Model:
def __init__(self, name=""):
self.name = name
self.objective = None
self.constraints = []
self.vars = {}
def __iadd__(self, other):
if isinstance(other, tuple):
if len(other) == 2 and other[0] in ['maximize', 'minimize']:
self.objective = other
else:
self.constraints.append(other)
return self
def solve(self):
sense, obj_expr = self.objective
obj_func_str = f"z = {self._expr_to_str(obj_expr)}"
objective_function = ObjectiveFunction(obj_func_str)
constraints = []
for left, op, right in self.constraints:
right_expr = Expression(const=right) if not isinstance(right, Expression) else right
expr = left - right_expr
const_str = f" {op} {expr.const * -1}"
constraints.append(AlgebraicExpression(self._expr_to_str(expr) + const_str))
# Collect all variables
all_vars = set(objective_function.variables)
for c in constraints:
all_vars.update(c.variables)
simplex = Simplex(
num_variables=len(all_vars),
objective_function=objective_function,
method=Simplex.MAXIMIZE if sense == 'maximize' else Simplex.MINIMIZE,
constraints=constraints,
)
simplex.solve_problem()
return simplex
def _expr_to_str(self, expr):
terms = []
for var, coef in expr.terms.items():
if coef == 1:
terms.append(var)
elif coef == -1:
terms.append(f"- {var}")
else:
terms.append(f"{coef} * {var}")
return " + ".join(terms).replace("+ -", "-")
def maximize(expr):
return ("maximize", expr)
def minimize(expr):
return ("minimize", expr)