forked from Pyomo/pyomo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
standard_repn.py
1510 lines (1371 loc) · 57.9 KB
/
standard_repn.py
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ___________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright 2017 National Technology and Engineering Solutions of Sandia, LLC
# Under the terms of Contract DE-NA0003525 with National Technology and
# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________
from __future__ import division
__all__ = ['StandardRepn', 'generate_standard_repn']
import sys
import logging
import math
import itertools
from pyomo.core.base import (Constraint,
Objective,
ComponentMap)
from pyutilib.misc import Bunch
from pyutilib.math.util import isclose as isclose_default
from pyomo.core.expr import current as EXPR
from pyomo.core.base.objective import (_GeneralObjectiveData,
SimpleObjective)
from pyomo.core.base import _ExpressionData, Expression
from pyomo.core.base.expression import SimpleExpression, _GeneralExpressionData
from pyomo.core.base.var import (SimpleVar,
Var,
_GeneralVarData,
_VarData,
value)
from pyomo.core.base.param import _ParamData
from pyomo.core.base.numvalue import (NumericConstant,
native_numeric_types,
is_fixed)
from pyomo.core.kernel.expression import IIdentityExpression, expression, noclone
from pyomo.core.kernel.variable import IVariable
from pyomo.core.kernel.objective import objective
import six
from six import iteritems
from six import itervalues, iteritems, StringIO
from six.moves import xrange, zip
try:
basestring
except:
basestring = str
logger = logging.getLogger('pyomo.core')
using_py3 = six.PY3
from pyomo.core.base import _VarData, _GeneralVarData, SimpleVar
from pyomo.core.kernel.variable import IVariable, variable
#
# This checks if the first argument is a numeric value. If not
# then this is a Pyomo constant expression, and we can only check if its
# close to 'b' when it is constant.
#
def isclose_const(a, b, rel_tol=1e-9, abs_tol=0.0):
if not a.__class__ in native_numeric_types:
if a.is_constant():
a = value(a)
else:
return False
# Copied from pyutilib.math.isclose
return abs(a-b) <= max( rel_tol * max(abs(a), abs(b)), abs_tol )
#
# The global isclose() function used below. This is either isclose_default
# (defined in pyutilib) or isclose_const
#
isclose = isclose_default
class StandardRepn(object):
"""
This class defines a standard/common representation for Pyomo expressions
that provides an efficient interface for writing all models.
TODO: define what "efficient" means to us.
"""
__slots__ = ('constant', # The constant term
'linear_coefs', # Linear coefficients
'linear_vars', # Linear variables
'quadratic_coefs', # Quadratic coefficients
'quadratic_vars', # Quadratic variables
'nonlinear_expr', # Nonlinear expression
'nonlinear_vars') # Variables that appear in the nonlinear expression
def __init__(self):
self.constant = 0
self.linear_vars = tuple()
self.linear_coefs = tuple()
self.quadratic_vars = tuple()
self.quadratic_coefs = tuple()
self.nonlinear_expr = None
self.nonlinear_vars = tuple()
def __getstate__(self):
"""
This method is required because this class uses slots.
"""
return (self.constant,
self.linear_coefs,
self.linear_vars,
self.quadratic_coefs,
self.quadratic_vars,
self.nonlinear_expr,
self.nonlinear_vars)
def __setstate__(self, state):
"""
This method is required because this class uses slots.
"""
self.constant, \
self.linear_coefs, \
self.linear_vars, \
self.quadratic_coefs, \
self.quadratic_vars, \
self.nonlinear_expr, \
self.nonlinear_vars = state
#
# Generate a string representation of the expression
#
def __str__(self): #pragma: nocover
output = StringIO()
output.write("\n")
output.write("constant: "+str(self.constant)+"\n")
output.write("linear vars: "+str([v_.name for v_ in self.linear_vars])+"\n")
output.write("linear var ids: "+str([id(v_) for v_ in self.linear_vars])+"\n")
output.write("linear coef: "+str(list(self.linear_coefs))+"\n")
output.write("quadratic vars: "+str([(v_[0].name,v_[1].name) for v_ in self.quadratic_vars])+"\n")
output.write("quadratic var ids: "+str([(id(v_[0]), id(v_[1])) for v_ in self.quadratic_vars])+"\n")
output.write("quadratic coef: "+str(list(self.quadratic_coefs))+"\n")
if self.nonlinear_expr is None:
output.write("nonlinear expr: None\n")
else:
output.write("nonlinear expr:\n")
try:
output.write(self.nonlinear_expr.to_string())
output.write("\n")
except AttributeError:
output.write(str(self.nonlinear_expr))
output.write("\n")
output.write("nonlinear vars: "+str([v_.name for v_ in self.nonlinear_vars])+"\n")
output.write("\n")
ret_str = output.getvalue()
output.close()
return ret_str
def is_fixed(self):
if len(self.linear_vars) == 0 and len(self.nonlinear_vars) == 0 and len(self.quadratic_vars) == 0:
return True
return False
def polynomial_degree(self):
if not self.nonlinear_expr is None:
return None
if len(self.quadratic_coefs) > 0:
return 2
if len(self.linear_coefs) > 0:
return 1
return 0
def is_constant(self):
return self.nonlinear_expr is None and len(self.quadratic_coefs) == 0 and len(self.linear_coefs) == 0
def is_linear(self):
return self.nonlinear_expr is None and len(self.quadratic_coefs) == 0
def is_quadratic(self):
return len(self.quadratic_coefs) > 0 and self.nonlinear_expr is None
def is_nonlinear(self):
return not (self.nonlinear_expr is None and len(self.quadratic_coefs) == 0)
def to_expression(self, sort=True):
#
# When an standard representation is created, the ordering of the
# linear and quadratic terms may not be preserved. Hence, the
# sorting option ensures that an expression generated from a
# standard representation has a consistent order.
#
# TODO: Should this replace non-mutable parameters with constants?
#
expr = self.constant
lvars = [(i,v) for i,v in enumerate(self.linear_vars)]
if sort:
lvars = sorted(lvars, key=lambda x: str(x[1]))
for i,v in lvars:
c = self.linear_coefs[i]
if c.__class__ in native_numeric_types:
if isclose_const(c, 1.0):
expr += v
elif isclose_const(c, -1.0):
expr -= v
elif c < 0.0:
expr -= - c*v
else:
expr += c*v
else:
expr += c*v
qvars = [(i,v) for i,v in enumerate(self.quadratic_vars)]
if sort:
qvars = sorted(qvars, key=lambda x: (str(x[1][0]), str(x[1][1])))
for i,v in qvars:
if id(v[0]) == id(v[1]):
term = v[0]**2
else:
term = v[0]*v[1]
c = self.quadratic_coefs[i]
if c.__class__ in native_numeric_types:
if isclose_const(c, 1.0):
expr += term
elif isclose_const(c, -1.0):
expr -= term
else:
expr += c*term
else:
expr += c*term
if not self.nonlinear_expr is None:
expr += self.nonlinear_expr
return expr
"""
Note: This function separates linear terms from nonlinear terms.
Along the way, fixed variable and mutable parameter values *may* be
replaced with constants. However, that is not guaranteed. Thus,
the nonlinear expression may contain subexpressions whose value is
constant. This was done to avoid additional work when a subexpression
is clearly nonlinear. However, this requires that standard
representations be temporary. They should be used to interface
to a solver and then be deleted.
"""
#@profile
def generate_standard_repn(expr, idMap=None, compute_values=True, verbose=False, quadratic=True, repn=None):
#
# Use a custom Results object
#
global Results
if quadratic:
Results = ResultsWithQuadratics
else:
Results = ResultsWithoutQuadratics
#
# Use a custom isclose function
#
global isclose
if compute_values:
isclose = isclose_default
else:
isclose = isclose_const
if True:
#
# Setup
#
if idMap is None:
idMap = {}
idMap.setdefault(None, {})
if repn is None:
repn = StandardRepn()
#
# The expression is a number or a non-variable constant
# expression.
#
if expr.__class__ in native_numeric_types or not expr.is_potentially_variable():
if compute_values:
repn.constant = EXPR.evaluate_expression(expr)
else:
repn.constant = expr
return repn
#
# The expression is a variable
#
elif expr.is_variable_type():
if expr.fixed:
if compute_values:
repn.constant = value(expr)
else:
repn.constant = expr
return repn
repn.linear_coefs = (1,)
repn.linear_vars = (expr,)
return repn
#
# The expression is linear
#
elif expr.__class__ is EXPR.LinearExpression:
if compute_values:
C_ = EXPR.evaluate_expression(expr.constant)
else:
C_ = expr.constant
if compute_values:
linear_coefs = {}
for c,v in zip(expr.linear_coefs, expr.linear_vars):
if c.__class__ in native_numeric_types:
cval = c
elif c.is_expression_type():
cval = EXPR.evaluate_expression(c)
else:
cval = value(c)
if v.fixed:
C_ += cval * v.value
else:
id_ = id(v)
if not id_ in idMap[None]:
key = len(idMap) - 1
idMap[None][id_] = key
idMap[key] = v
else:
key = idMap[None][id_]
if key in linear_coefs:
linear_coefs[key] += cval
else:
linear_coefs[key] = cval
keys = list(linear_coefs.keys())
repn.linear_vars = tuple(idMap[key] for key in keys)
repn.linear_coefs = tuple(linear_coefs[key] for key in keys)
else:
linear_coefs = {}
for c,v in zip(expr.linear_coefs, expr.linear_vars):
if v.fixed:
C_ += c*v
else:
id_ = id(v)
if not id_ in idMap[None]:
key = len(idMap) - 1
idMap[None][id_] = key
idMap[key] = v
else:
key = idMap[None][id_]
if key in linear_coefs:
linear_coefs[key] += c
else:
linear_coefs[key] = c
keys = list(linear_coefs.keys())
repn.linear_vars = tuple(idMap[key] for key in keys)
repn.linear_coefs = tuple(linear_coefs[key] for key in keys)
repn.constant = C_
return repn
#
# Unknown expression object
#
elif not expr.is_expression_type(): #pragma: nocover
raise ValueError("Unexpected expression type: "+str(expr))
#
# WEH - Checking the polynomial degree didn't
# turn out to be a win. But I'm leaving this
# in as a comment for now, since we're not
# done tuning this code.
#
#degree = expr.polynomial_degree()
#if degree == 1:
# return _generate_linear_standard_repn(expr,
# idMap=idMap,
# compute_values=compute_values,
# verbose=verbose,
# repn=repn)
#else:
return _generate_standard_repn(expr,
idMap=idMap,
compute_values=compute_values,
verbose=verbose,
quadratic=quadratic,
repn=repn)
##-----------------------------------------------------------------------
##
## Logic for _generate_standard_repn
##
##-----------------------------------------------------------------------
class ResultsWithQuadratics(object):
__slot__ = ('const', 'nonl', 'linear', 'quadratic')
def __init__(self, constant=0, nonl=0, linear=None, quadratic=None):
self.constant = constant
self.nonl = nonl
self.linear = {}
#if linear is None:
# self.linear = {}
#else:
# self.linear = linear
self.quadratic = {}
#if quadratic is None:
# self.quadratic = {}
#else:
# self.quadratic = quadratic
def __str__(self): #pragma: nocover
return "Const:\t%s\nLinear:\t%s\nQuadratic:\t%s\nNonlinear:\t%s" % (str(self.constant), str(self.linear), str(self.quadratic), str(self.nonl))
class ResultsWithoutQuadratics(object):
__slot__ = ('const', 'nonl', 'linear')
def __init__(self, constant=0, nonl=0, linear=None):
self.constant = constant
self.nonl = nonl
self.linear = {}
#if linear is None:
# self.linear = {}
#else:
# self.linear = linear
def __str__(self): #pragma: nocover
return "Const:\t%s\nLinear:\t%s\nNonlinear:\t%s" % (str(self.constant), str(self.linear), str(self.nonl))
Results = ResultsWithQuadratics
#@profile
def _collect_sum(exp, multiplier, idMap, compute_values, verbose, quadratic):
ans = Results()
nonl = []
varkeys = idMap[None]
for e_ in itertools.islice(exp._args_, exp.nargs()):
if e_.__class__ is EXPR.MonomialTermExpression:
lhs, v = e_._args_
if compute_values and not lhs.__class__ in native_numeric_types:
lhs = value(lhs)
if v.fixed:
if compute_values:
ans.constant += multiplier*lhs*value(v)
else:
ans.constant += multiplier*lhs*v
else:
id_ = id(v)
if id_ in varkeys:
key = varkeys[id_]
else:
key = len(idMap) - 1
varkeys[id_] = key
idMap[key] = v
if key in ans.linear:
ans.linear[key] += multiplier*lhs
else:
ans.linear[key] = multiplier*lhs
elif e_.__class__ in native_numeric_types:
ans.constant += multiplier*e_
elif e_.is_variable_type():
if e_.fixed:
if compute_values:
ans.constant += multiplier*e_.value
else:
ans.constant += multiplier*e_
else:
id_ = id(e_)
if id_ in varkeys:
key = varkeys[id_]
else:
key = len(idMap) - 1
varkeys[id_] = key
idMap[key] = e_
if key in ans.linear:
ans.linear[key] += multiplier
else:
ans.linear[key] = multiplier
elif not e_.is_potentially_variable():
if compute_values:
ans.constant += multiplier * value(e_)
else:
ans.constant += multiplier * e_
else:
res_ = _collect_standard_repn(e_, multiplier, idMap,
compute_values, verbose, quadratic)
#
# Add returned from recursion
#
ans.constant += res_.constant
if not (res_.nonl.__class__ in native_numeric_types and res_.nonl == 0):
nonl.append(res_.nonl)
for i in res_.linear:
ans.linear[i] = ans.linear.get(i,0) + res_.linear[i]
if quadratic:
for i in res_.quadratic:
ans.quadratic[i] = ans.quadratic.get(i, 0) + res_.quadratic[i]
if len(nonl) > 0:
if len(nonl) == 1:
ans.nonl = nonl[0]
else:
ans.nonl = EXPR.SumExpression(nonl)
return ans
#@profile
def _collect_term(exp, multiplier, idMap, compute_values, verbose, quadratic):
#
# LHS is a numeric value
#
if exp._args_[0].__class__ in native_numeric_types:
if exp._args_[0] == 0: # TODO: coverage?
return Results()
return _collect_standard_repn(exp._args_[1], multiplier * exp._args_[0], idMap,
compute_values, verbose, quadratic)
#
# LHS is a non-variable expression
#
else:
if compute_values:
val = value(exp._args_[0])
if val == 0:
return Results()
return _collect_standard_repn(exp._args_[1], multiplier * val, idMap,
compute_values, verbose, quadratic)
else:
return _collect_standard_repn(exp._args_[1], multiplier*exp._args_[0], idMap,
compute_values, verbose, quadratic)
def _collect_prod(exp, multiplier, idMap, compute_values, verbose, quadratic):
#
# LHS is a numeric value
#
if exp._args_[0].__class__ in native_numeric_types:
if exp._args_[0] == 0: # TODO: coverage?
return Results()
return _collect_standard_repn(exp._args_[1], multiplier * exp._args_[0], idMap,
compute_values, verbose, quadratic)
#
# RHS is a numeric value
#
if exp._args_[1].__class__ in native_numeric_types:
if exp._args_[1] == 0: # TODO: coverage?
return Results()
return _collect_standard_repn(exp._args_[0], multiplier * exp._args_[1], idMap,
compute_values, verbose, quadratic)
#
# LHS is a non-variable expression
#
elif not exp._args_[0].is_potentially_variable():
if compute_values:
val = value(exp._args_[0])
if val == 0:
return Results()
return _collect_standard_repn(exp._args_[1], multiplier * val, idMap,
compute_values, verbose, quadratic)
else:
return _collect_standard_repn(exp._args_[1], multiplier*exp._args_[0], idMap,
compute_values, verbose, quadratic)
#
# RHS is a non-variable expression
#
elif not exp._args_[1].is_potentially_variable():
if compute_values:
val = value(exp._args_[1])
if val == 0:
return Results()
return _collect_standard_repn(exp._args_[0], multiplier * val, idMap,
compute_values, verbose, quadratic)
else:
return _collect_standard_repn(exp._args_[0], multiplier*exp._args_[1], idMap,
compute_values, verbose, quadratic)
#
# Both the LHS and RHS are potentially variable ...
#
# Collect LHS
#
lhs = _collect_standard_repn(exp._args_[0], 1, idMap,
compute_values, verbose, quadratic)
lhs_nonl_None = lhs.nonl.__class__ in native_numeric_types and lhs.nonl == 0
#
# LHS is potentially variable, but it turns out to be a constant
# because the variables were fixed.
#
if lhs_nonl_None and len(lhs.linear) == 0 and (not quadratic or len(lhs.quadratic) == 0):
if lhs.constant.__class__ in native_numeric_types and lhs.constant == 0:
return Results()
if compute_values:
val = value(lhs.constant)
if val == 0: # TODO: coverage?
return Results()
return _collect_standard_repn(exp._args_[1], multiplier*val, idMap,
compute_values, verbose, quadratic)
else:
return _collect_standard_repn(exp._args_[1], multiplier*lhs.constant, idMap,
compute_values, verbose, quadratic)
#
# Collect RHS
#
rhs = _collect_standard_repn(exp._args_[1], 1, idMap,
compute_values, verbose, quadratic)
rhs_nonl_None = rhs.nonl.__class__ in native_numeric_types and rhs.nonl == 0
#
# If RHS is zero, then return an empty results
#
if rhs_nonl_None and len(rhs.linear) == 0 and (not quadratic or len(rhs.quadratic) == 0) and rhs.constant.__class__ in native_numeric_types and rhs.constant == 0:
return Results()
#
# If either the LHS or RHS are nonlinear, then simply return the nonlinear expression
#
if not lhs_nonl_None or not rhs_nonl_None:
return Results(nonl=multiplier*exp)
#
# If not collecting quadratic terms and both terms are linear, then simply return the nonlinear expression
#
if not quadratic and len(lhs.linear) > 0 and len(rhs.linear) > 0:
# NOTE: We treat a product of linear terms as nonlinear unless quadratic is True
return Results(nonl=multiplier*exp)
ans = Results()
ans.constant = multiplier*lhs.constant * rhs.constant
if not (lhs.constant.__class__ in native_numeric_types and lhs.constant == 0):
for key, coef in six.iteritems(rhs.linear):
ans.linear[key] = multiplier*coef*lhs.constant
if not (rhs.constant.__class__ in native_numeric_types and rhs.constant == 0):
for key, coef in six.iteritems(lhs.linear):
if key in ans.linear:
ans.linear[key] += multiplier*coef*rhs.constant
else:
ans.linear[key] = multiplier*coef*rhs.constant
if quadratic:
if not (lhs.constant.__class__ in native_numeric_types and lhs.constant == 0):
for key, coef in six.iteritems(rhs.quadratic):
ans.quadratic[key] = multiplier*coef*lhs.constant
if not (rhs.constant.__class__ in native_numeric_types and rhs.constant == 0):
for key, coef in six.iteritems(lhs.quadratic):
if key in ans.quadratic:
ans.quadratic[key] += multiplier*coef*rhs.constant
else:
ans.quadratic[key] = multiplier*coef*rhs.constant
for lkey, lcoef in six.iteritems(lhs.linear):
for rkey, rcoef in six.iteritems(rhs.linear):
ndx = (lkey, rkey) if lkey <= rkey else (rkey, lkey)
if ndx in ans.quadratic:
ans.quadratic[ndx] += multiplier*lcoef*rcoef
else:
ans.quadratic[ndx] = multiplier*lcoef*rcoef
# TODO - Use quicksum here?
el_linear = multiplier*sum(coef*idMap[key] for key, coef in six.iteritems(lhs.linear))
er_linear = multiplier*sum(coef*idMap[key] for key, coef in six.iteritems(rhs.linear))
el_quadratic = multiplier*sum(coef*idMap[key[0]]*idMap[key[1]] for key, coef in six.iteritems(lhs.quadratic))
er_quadratic = multiplier*sum(coef*idMap[key[0]]*idMap[key[1]] for key, coef in six.iteritems(rhs.quadratic))
ans.nonl += el_linear*er_quadratic + el_quadratic*er_linear
return ans
#@profile
def _collect_var(exp, multiplier, idMap, compute_values, verbose, quadratic):
ans = Results()
if exp.fixed:
if compute_values:
ans.constant += multiplier*value(exp)
else:
ans.constant += multiplier*exp
else:
id_ = id(exp)
if id_ in idMap[None]:
key = idMap[None][id_]
else:
key = len(idMap) - 1
idMap[None][id_] = key
idMap[key] = exp
ans.linear[key] = multiplier
return ans
def _collect_pow(exp, multiplier, idMap, compute_values, verbose, quadratic):
#
# Exponent is a numeric value
#
if exp._args_[1].__class__ in native_numeric_types:
exponent = exp._args_[1]
#
# Exponent is not potentially variable
#
# Compute its value if compute_values==True
#
elif not exp._args_[1].is_potentially_variable():
if compute_values:
exponent = value(exp._args_[1])
else:
exponent = exp._args_[1]
#
# Otherwise collect a standard repn
#
else:
res = _collect_standard_repn(exp._args_[1], 1, idMap, compute_values, verbose, quadratic)
#
# If the expression is variable, then return a nonlinear expression
#
if not (res.nonl.__class__ in native_numeric_types and res.nonl == 0) or len(res.linear) > 0 or (quadratic and len(res.quadratic) > 0):
return Results(nonl=multiplier*exp)
exponent = res.constant
if exponent.__class__ in native_numeric_types:
#
# #**0 = 1
#
if exponent == 0:
return Results(constant=multiplier)
#
# #**1 = #
#
# Return the standard repn for arg(0)
#
elif exponent == 1:
return _collect_standard_repn(exp._args_[0], multiplier, idMap, compute_values, verbose, quadratic)
#
# Ignore #**2 unless quadratic==True
#
elif exponent == 2 and quadratic:
res =_collect_standard_repn(exp._args_[0], 1, idMap, compute_values, verbose, quadratic)
#
# If arg(0) is nonlinear, then this is a nonlinear repn
#
if not (res.nonl.__class__ in native_numeric_types and res.nonl == 0) or len(res.quadratic) > 0:
return Results(nonl=multiplier*exp)
#
# If computing values and no linear terms, then the return a constant repn
#
elif compute_values and len(res.linear) == 0:
return Results(constant=multiplier*res.constant**exponent)
#
# If the base is linear, then we compute the quadratic expression for it.
#
else:
ans = Results()
has_constant = (res.constant.__class__
not in native_numeric_types
or res.constant != 0)
if has_constant:
ans.constant = multiplier*res.constant*res.constant
# this is reversed since we want to pop off the end for efficiency
# and the quadratic terms have a convention that the indexing tuple
# of key1, key2 is such that key1 <= key2
keys = sorted(res.linear.keys(), reverse=True)
while len(keys) > 0:
key1 = keys.pop()
coef1 = res.linear[key1]
if has_constant:
ans.linear[key1] = 2*multiplier*coef1*res.constant
ans.quadratic[key1,key1] = multiplier*coef1*coef1
for key2 in keys:
coef2 = res.linear[key2]
ans.quadratic[key1,key2] = 2*multiplier*coef1*coef2
return ans
#
# If args(0) is a numeric value or it is fixed, then we have a constant value
#
if exp._args_[0].__class__ in native_numeric_types or exp._args_[0].is_fixed():
if compute_values:
return Results(constant=multiplier*value(exp._args_[0])**exponent)
else:
return Results(constant=multiplier*exp)
#
# Return a nonlinear expression here
#
return Results(nonl=multiplier*exp)
def _collect_division(exp, multiplier, idMap, compute_values, verbose, quadratic):
if exp._args_[1].__class__ in native_numeric_types or not exp._args_[1].is_potentially_variable(): # TODO: coverage?
# Denominator is trivially constant
if compute_values:
denom = 1.0 * value(exp._args_[1])
else:
denom = 1.0 * exp._args_[1]
else:
res =_collect_standard_repn(exp._args_[1], 1, idMap, compute_values, verbose, quadratic)
if not (res.nonl.__class__ in native_numeric_types and res.nonl == 0) or len(res.linear) > 0 or (quadratic and len(res.quadratic) > 0):
# Denominator is variable, give up: this is nonlinear
return Results(nonl=multiplier*exp)
else:
# Denominaor ended up evaluating to a constant
denom = 1.0*res.constant
if denom.__class__ in native_numeric_types and denom == 0:
raise ZeroDivisionError
if exp._args_[0].__class__ in native_numeric_types or not exp._args_[0].is_potentially_variable():
num = exp._args_[0]
if compute_values:
num = value(num)
return Results(constant=multiplier*num/denom)
return _collect_standard_repn(exp._args_[0], multiplier/denom, idMap, compute_values, verbose, quadratic)
def _collect_reciprocal(exp, multiplier, idMap, compute_values, verbose, quadratic):
if exp._args_[0].__class__ in native_numeric_types or not exp._args_[0].is_potentially_variable(): # TODO: coverage?
if compute_values:
denom = 1.0 * value(exp._args_[0])
else:
denom = 1.0 * exp._args_[0]
else:
res =_collect_standard_repn(exp._args_[0], 1, idMap, compute_values, verbose, quadratic)
if not (res.nonl.__class__ in native_numeric_types and res.nonl == 0) or len(res.linear) > 0 or (quadratic and len(res.quadratic) > 0):
return Results(nonl=multiplier*exp)
else:
denom = 1.0*res.constant
if denom.__class__ in native_numeric_types and denom == 0:
raise ZeroDivisionError
return Results(constant=multiplier/denom)
def _collect_branching_expr(exp, multiplier, idMap, compute_values, verbose, quadratic):
if exp._if.__class__ in native_numeric_types: # TODO: coverage?
if_val = exp._if
elif not exp._if.is_potentially_variable():
if compute_values:
if_val = value(exp._if)
else:
return Results(nonl=multiplier*exp)
else:
res = _collect_standard_repn(exp._if, 1, idMap, compute_values, verbose, quadratic)
if not (res.nonl.__class__ in native_numeric_types and res.nonl == 0) or len(res.linear) > 0 or (quadratic and len(res.quadratic) > 0):
return Results(nonl=multiplier*exp)
elif res.constant.__class__ in native_numeric_types:
if_val = res.constant
else:
return Results(constant=multiplier*exp)
if if_val:
if exp._then.__class__ in native_numeric_types:
return Results(constant=multiplier*exp._then)
return _collect_standard_repn(exp._then, multiplier, idMap, compute_values, verbose, quadratic)
else:
if exp._else.__class__ in native_numeric_types:
return Results(constant=multiplier*exp._else)
return _collect_standard_repn(exp._else, multiplier, idMap, compute_values, verbose, quadratic)
def _collect_nonl(exp, multiplier, idMap, compute_values, verbose, quadratic):
res = _collect_standard_repn(exp._args_[0], 1, idMap, compute_values, verbose, quadratic)
if not (res.nonl.__class__ in native_numeric_types and res.nonl == 0) or len(res.linear) > 0 or (quadratic and len(res.quadratic) > 0):
return Results(nonl=multiplier*exp)
if compute_values:
return Results(constant=multiplier*exp._apply_operation([res.constant]))
else:
return Results(constant=multiplier*exp)
def _collect_negation(exp, multiplier, idMap, compute_values, verbose, quadratic):
return _collect_standard_repn(exp._args_[0], -1*multiplier, idMap, compute_values, verbose, quadratic)
#
# TODO - Verify if code is used
#
def _collect_const(exp, multiplier, idMap, compute_values, verbose, quadratic):
if compute_values:
return Results(constant=multiplier*value(exp))
else:
return Results(constant=multiplier*exp)
def _collect_identity(exp, multiplier, idMap, compute_values, verbose, quadratic):
if exp._args_[0].__class__ in native_numeric_types:
return Results(constant=multiplier*exp._args_[0])
if not exp._args_[0].is_potentially_variable():
if compute_values:
return Results(constant=multiplier*value(exp._args_[0]))
else:
return Results(constant=multiplier*exp._args_[0])
return _collect_standard_repn(exp.expr, multiplier, idMap, compute_values, verbose, quadratic)
def _collect_linear(exp, multiplier, idMap, compute_values, verbose, quadratic):
ans = Results()
if compute_values:
ans.constant = multiplier*value(exp.constant)
else:
ans.constant = multiplier*exp.constant
for c,v in zip(exp.linear_coefs, exp.linear_vars):
if v.fixed:
if compute_values:
ans.constant += multiplier * value(c) * value(v)
else:
ans.constant += multiplier * c * v
else:
id_ = id(v)
if id_ in idMap[None]:
key = idMap[None][id_]
else:
key = len(idMap) - 1
idMap[None][id_] = key
idMap[key] = v
if compute_values:
if key in ans.linear:
ans.linear[key] += multiplier*value(c)
else:
ans.linear[key] = multiplier*value(c)
else:
if key in ans.linear:
ans.linear[key] += multiplier*c
else:
ans.linear[key] = multiplier*c
return ans
def _collect_comparison(exp, multiplier, idMap, compute_values, verbose, quadratic):
return Results(nonl=multiplier*exp)
def _collect_external_fn(exp, multiplier, idMap, compute_values, verbose, quadratic):
if compute_values and exp.is_fixed():
return Results(nonl=multiplier*value(exp))
return Results(nonl=multiplier*exp)
_repn_collectors = {
EXPR.SumExpression : _collect_sum,
EXPR.ProductExpression : _collect_prod,
EXPR.MonomialTermExpression : _collect_term,
EXPR.PowExpression : _collect_pow,
EXPR.DivisionExpression : _collect_division,
EXPR.ReciprocalExpression : _collect_reciprocal,
EXPR.Expr_ifExpression : _collect_branching_expr,
EXPR.UnaryFunctionExpression : _collect_nonl,
EXPR.AbsExpression : _collect_nonl,
EXPR.NegationExpression : _collect_negation,
EXPR.LinearExpression : _collect_linear,
EXPR.InequalityExpression : _collect_comparison,
EXPR.RangedExpression : _collect_comparison,
EXPR.EqualityExpression : _collect_comparison,
EXPR.ExternalFunctionExpression : _collect_external_fn,
#_ConnectorData : _collect_linear_connector,
#SimpleConnector : _collect_linear_connector,
#param._ParamData : _collect_linear_const,
#param.SimpleParam : _collect_linear_const,
#param.Param : _collect_linear_const,
#parameter : _collect_linear_const,
NumericConstant : _collect_const,
_GeneralVarData : _collect_var,
SimpleVar : _collect_var,
Var : _collect_var,
variable : _collect_var,
IVariable : _collect_var,
_GeneralExpressionData : _collect_identity,
SimpleExpression : _collect_identity,
expression : _collect_identity,
noclone : _collect_identity,
_ExpressionData : _collect_identity,
Expression : _collect_identity,
_GeneralObjectiveData : _collect_identity,
SimpleObjective : _collect_identity,
objective : _collect_identity,
}
def _collect_standard_repn(exp, multiplier, idMap,
compute_values, verbose, quadratic):
fn = _repn_collectors.get(exp.__class__, None)
if fn is not None:
return fn(exp, multiplier, idMap, compute_values, verbose, quadratic)
#
# Catch any known numeric constants
#
if exp.__class__ in native_numeric_types:
return _collect_const(exp, multiplier, idMap, compute_values,
verbose, quadratic)
#
# These are types that might be extended using duck typing.
#
try:
if exp.is_variable_type():
fn = _collect_var
if exp.is_named_expression_type():
fn = _collect_identity
except AttributeError: # TODO: coverage?
pass
if fn is not None:
_repn_collectors[exp.__class__] = fn
return fn(exp, multiplier, idMap, compute_values, verbose, quadratic)
raise ValueError( "Unexpected expression (type %s)" % type(exp).__name__) # TODO: coverage?
def _generate_standard_repn(expr, idMap=None, compute_values=True, verbose=False, quadratic=True, repn=None):
if expr.__class__ is EXPR.SumExpression:
#
# This is the common case, so start collecting the sum
#
ans = _collect_sum(expr, 1, idMap, compute_values, verbose, quadratic)
else:
#
# Call generic recursive logic
#
ans = _collect_standard_repn(expr, 1, idMap, compute_values, verbose, quadratic)
#
# Create the final object here from 'ans'
#
repn.constant = ans.constant
#
# Create a list (tuple) of the variables and coefficients
#
v = []