@@ -117,7 +117,7 @@ def value(obj, exception=True):
117
117
# Test if we have a duck types for Pyomo expressions
118
118
#
119
119
try :
120
- obj .is_expression_type ()
120
+ obj .is_numeric_type ()
121
121
except AttributeError :
122
122
#
123
123
# If not, then try to coerce this into a numeric constant. If that
@@ -137,7 +137,6 @@ def value(obj, exception=True):
137
137
#
138
138
# Here, we try to catch the exception
139
139
#
140
-
141
140
try :
142
141
tmp = obj (exception = True )
143
142
if tmp is None :
@@ -257,7 +256,7 @@ def is_numeric_data(obj):
257
256
# this likely means it is a string
258
257
return False
259
258
try :
260
- # Test if this is an expression object that
259
+ # Test if this is an expression object that
261
260
# is not potentially variable
262
261
return not obj .is_potentially_variable ()
263
262
except AttributeError :
@@ -330,15 +329,15 @@ def as_numeric(obj):
330
329
Args:
331
330
obj: The numeric value that may be wrapped.
332
331
333
- Raises: TypeError if the object is in native_types and not in
332
+ Raises: TypeError if the object is in native_types and not in
334
333
native_numeric_types
335
334
336
335
Returns: A NumericConstant object or the original object.
337
336
"""
338
337
if obj .__class__ in native_numeric_types :
339
338
val = _KnownConstants .get (obj , None )
340
339
if val is not None :
341
- return val
340
+ return val
342
341
#
343
342
# Coerce the value to a float, if possible
344
343
#
@@ -367,11 +366,20 @@ def as_numeric(obj):
367
366
#
368
367
return retval
369
368
#
370
- # Ignore objects that are duck types to work with Pyomo expressions
369
+ # Ignore objects that are duck typed to work with Pyomo expressions
371
370
#
372
371
try :
373
- obj .is_expression_type ()
374
- return obj
372
+ if obj .is_numeric_type ():
373
+ return obj
374
+ else :
375
+ try :
376
+ _name = obj .name
377
+ except AttributeError :
378
+ _name = str (obj )
379
+ raise TypeError (
380
+ "The '%s' object '%s' is not a valid type for Pyomo "
381
+ "numeric expressions" % (type (obj ).__name__ , _name ))
382
+
375
383
except AttributeError :
376
384
pass
377
385
#
@@ -386,10 +394,11 @@ def as_numeric(obj):
386
394
# Generate errors
387
395
#
388
396
if obj .__class__ in native_types :
389
- raise TypeError ("Cannot treat the value '%s' as a constant" % str (obj ))
397
+ raise TypeError ("%s values ('%s') are not allowed in Pyomo "
398
+ "numeric expressions" % (type (obj ).__name__ , str (obj )))
390
399
raise TypeError (
391
- "Cannot treat the value '%s' as a constant because it has unknown "
392
- "type '%s'" % (str (obj ), type (obj ).__name__ ))
400
+ "Cannot treat the value '%s' as a numeric value because it has "
401
+ "unknown type '%s'" % (str (obj ), type (obj ).__name__ ))
393
402
394
403
395
404
def check_if_numeric_type_and_cache (obj ):
@@ -415,7 +424,7 @@ def check_if_numeric_type_and_cache(obj):
415
424
retval = NumericConstant (obj )
416
425
try :
417
426
#
418
- # Create the numeric constant and add to the
427
+ # Create the numeric constant and add to the
419
428
# list of known constants.
420
429
#
421
430
# Note: we don't worry about the size of the
@@ -647,7 +656,7 @@ def __lt__(self,other):
647
656
Less than operator
648
657
649
658
This method is called when Python processes statements of the form::
650
-
659
+
651
660
self < other
652
661
other > self
653
662
"""
@@ -658,7 +667,7 @@ def __gt__(self,other):
658
667
Greater than operator
659
668
660
669
This method is called when Python processes statements of the form::
661
-
670
+
662
671
self > other
663
672
other < self
664
673
"""
@@ -669,7 +678,7 @@ def __le__(self,other):
669
678
Less than or equal operator
670
679
671
680
This method is called when Python processes statements of the form::
672
-
681
+
673
682
self <= other
674
683
other >= self
675
684
"""
@@ -680,7 +689,7 @@ def __ge__(self,other):
680
689
Greater than or equal operator
681
690
682
691
This method is called when Python processes statements of the form::
683
-
692
+
684
693
self >= other
685
694
other <= self
686
695
"""
@@ -691,7 +700,7 @@ def __eq__(self,other):
691
700
Equal to operator
692
701
693
702
This method is called when Python processes the statement::
694
-
703
+
695
704
self == other
696
705
"""
697
706
return _generate_relational_expression (_eq , self , other )
@@ -701,7 +710,7 @@ def __add__(self,other):
701
710
Binary addition
702
711
703
712
This method is called when Python processes the statement::
704
-
713
+
705
714
self + other
706
715
"""
707
716
return _generate_sum_expression (_add ,self ,other )
@@ -711,7 +720,7 @@ def __sub__(self,other):
711
720
Binary subtraction
712
721
713
722
This method is called when Python processes the statement::
714
-
723
+
715
724
self - other
716
725
"""
717
726
return _generate_sum_expression (_sub ,self ,other )
@@ -721,7 +730,7 @@ def __mul__(self,other):
721
730
Binary multiplication
722
731
723
732
This method is called when Python processes the statement::
724
-
733
+
725
734
self * other
726
735
"""
727
736
return _generate_mul_expression (_mul ,self ,other )
@@ -731,7 +740,7 @@ def __div__(self,other):
731
740
Binary division
732
741
733
742
This method is called when Python processes the statement::
734
-
743
+
735
744
self / other
736
745
"""
737
746
return _generate_mul_expression (_div ,self ,other )
@@ -741,7 +750,7 @@ def __truediv__(self,other):
741
750
Binary division (when __future__.division is in effect)
742
751
743
752
This method is called when Python processes the statement::
744
-
753
+
745
754
self / other
746
755
"""
747
756
return _generate_mul_expression (_div ,self ,other )
@@ -751,7 +760,7 @@ def __pow__(self,other):
751
760
Binary power
752
761
753
762
This method is called when Python processes the statement::
754
-
763
+
755
764
self ** other
756
765
"""
757
766
return _generate_other_expression (_pow ,self ,other )
@@ -761,7 +770,7 @@ def __radd__(self,other):
761
770
Binary addition
762
771
763
772
This method is called when Python processes the statement::
764
-
773
+
765
774
other + self
766
775
"""
767
776
return _generate_sum_expression (_radd ,self ,other )
@@ -771,7 +780,7 @@ def __rsub__(self,other):
771
780
Binary subtraction
772
781
773
782
This method is called when Python processes the statement::
774
-
783
+
775
784
other - self
776
785
"""
777
786
return _generate_sum_expression (_rsub ,self ,other )
@@ -781,7 +790,7 @@ def __rmul__(self,other):
781
790
Binary multiplication
782
791
783
792
This method is called when Python processes the statement::
784
-
793
+
785
794
other * self
786
795
787
796
when other is not a :class:`NumericValue <pyomo.core.expr.numvalue.NumericValue>` object.
@@ -792,7 +801,7 @@ def __rdiv__(self,other):
792
801
"""Binary division
793
802
794
803
This method is called when Python processes the statement::
795
-
804
+
796
805
other / self
797
806
"""
798
807
return _generate_mul_expression (_rdiv ,self ,other )
@@ -802,7 +811,7 @@ def __rtruediv__(self,other):
802
811
Binary division (when __future__.division is in effect)
803
812
804
813
This method is called when Python processes the statement::
805
-
814
+
806
815
other / self
807
816
"""
808
817
return _generate_mul_expression (_rdiv ,self ,other )
@@ -812,7 +821,7 @@ def __rpow__(self,other):
812
821
Binary power
813
822
814
823
This method is called when Python processes the statement::
815
-
824
+
816
825
other ** self
817
826
"""
818
827
return _generate_other_expression (_rpow ,self ,other )
@@ -822,7 +831,7 @@ def __iadd__(self,other):
822
831
Binary addition
823
832
824
833
This method is called when Python processes the statement::
825
-
834
+
826
835
self += other
827
836
"""
828
837
return _generate_sum_expression (_iadd ,self ,other )
@@ -852,7 +861,7 @@ def __idiv__(self,other):
852
861
Binary division
853
862
854
863
This method is called when Python processes the statement::
855
-
864
+
856
865
self /= other
857
866
"""
858
867
return _generate_mul_expression (_idiv ,self ,other )
@@ -862,7 +871,7 @@ def __itruediv__(self,other):
862
871
Binary division (when __future__.division is in effect)
863
872
864
873
This method is called when Python processes the statement::
865
-
874
+
866
875
self /= other
867
876
"""
868
877
return _generate_mul_expression (_idiv ,self ,other )
@@ -872,7 +881,7 @@ def __ipow__(self,other):
872
881
Binary power
873
882
874
883
This method is called when Python processes the statement::
875
-
884
+
876
885
self **= other
877
886
"""
878
887
return _generate_other_expression (_ipow ,self ,other )
@@ -882,7 +891,7 @@ def __neg__(self):
882
891
Negation
883
892
884
893
This method is called when Python processes the statement::
885
-
894
+
886
895
- self
887
896
"""
888
897
return _generate_sum_expression (_neg , self , None )
@@ -892,7 +901,7 @@ def __pos__(self):
892
901
Positive expression
893
902
894
903
This method is called when Python processes the statement::
895
-
904
+
896
905
+ self
897
906
"""
898
907
return self
@@ -901,7 +910,7 @@ def __abs__(self):
901
910
""" Absolute value
902
911
903
912
This method is called when Python processes the statement::
904
-
913
+
905
914
abs(self)
906
915
"""
907
916
return _generate_other_expression (_abs ,self , None )
@@ -912,25 +921,32 @@ def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
912
921
913
922
def to_string (self , verbose = None , labeler = None , smap = None ,
914
923
compute_values = False ):
915
- """
916
- Return a string representation of the expression tree.
924
+ """Return a string representation of the expression tree.
917
925
918
926
Args:
919
- verbose (bool): If :const:`True`, then the the string
927
+ verbose (bool): If :const:`True`, then the string
920
928
representation consists of nested functions. Otherwise,
921
- the string representation is an algebraic equation.
929
+ the string representation is an infix algebraic equation.
922
930
Defaults to :const:`False`.
923
- labeler: An object that generates string labels for
924
- variables in the expression tree. Defaults to :const:`None`.
931
+ labeler: An object that generates string labels for
932
+ non-constant in the expression tree. Defaults to
933
+ :const:`None`.
934
+ smap: A SymbolMap instance that stores string labels for
935
+ non-constant nodes in the expression tree. Defaults to
936
+ :const:`None`.
937
+ compute_values (bool): If :const:`True`, then fixed
938
+ expressions are evaluated and the string representation
939
+ of the resulting value is returned.
925
940
926
941
Returns:
927
942
A string representation for the expression tree.
943
+
928
944
"""
929
945
if compute_values and self .is_fixed ():
930
946
try :
931
947
return str (self ())
932
948
except :
933
- pass
949
+ pass
934
950
if not self .is_constant ():
935
951
if smap is not None :
936
952
return smap .getSymbol (self , labeler )
0 commit comments