@@ -812,6 +812,70 @@ function map_expression_to_ast(
812
812
return ex
813
813
end
814
814
815
+ # ###############################################################################
816
+ # EXPRESSION RESTICTIONS
817
+ # ###############################################################################
818
+ """
819
+ restrict(expr::JuMP.AbstractJuMPScalar, supps...)::JuMP.AbstractJuMPScalar
820
+
821
+ Restrict an infinite expression `expr` to be enforced over infinite parameter
822
+ supports `supps`. This is limited to expressions only contain infinite variables
823
+ with the same kind of infinite parameter dependencies. Note that more conveniently
824
+ the expression can be treated as a function for the syntax `expr(supps...)`.
825
+
826
+ **Example**
827
+ ```julia-repl
828
+ julia> ex = @expression(model, 3y - 2)
829
+ 3 y(t) - 2
830
+
831
+ julia> restrict(ex, 0)
832
+ 3 y(0) - 2
833
+
834
+ julia> ex(0)
835
+ 3 y(0) - 2
836
+ ```
837
+ """
838
+ function restrict (expr:: JuMP.AbstractJuMPScalar , supps... )
839
+ # check to make sure all variables depend on the same infinite parameters
840
+ pref_tuples = Set ()
841
+ _interrogate_variables (expr) do v
842
+ if v. index_type <: InfiniteParameterIndex
843
+ error (" Restrictions on expressions with infinite parameters are not supported." )
844
+ elseif ! (v. index_type <: Union{FiniteVariableIndex, PointVariableIndex, FiniteParameterIndex} )
845
+ push! (pref_tuples, raw_parameter_refs (v))
846
+ end
847
+ end
848
+ if length (pref_tuples) > 1
849
+ error (" Unable to restrict expression `$expr ` with supports `$supps `. " *
850
+ " Not all the variables use the same infinite parameters in the " *
851
+ " same format." )
852
+ end
853
+ # restrict the expression using supps and return
854
+ return map_expression (expr) do v
855
+ if isempty (_object_numbers (v))
856
+ return v
857
+ else
858
+ return restrict (v, supps... )
859
+ end
860
+ end
861
+ end
862
+
863
+ # # Make expressions callable for restrictions
864
+ # AffExprs
865
+ function (aff:: JuMP.GenericAffExpr{Float64, GeneralVariableRef} )(supps... )
866
+ return restrict (aff, supps... )
867
+ end
868
+
869
+ # QuadExprs
870
+ function (quad:: JuMP.GenericQuadExpr{Float64, GeneralVariableRef} )(supps... )
871
+ return restrict (quad, supps... )
872
+ end
873
+
874
+ # NonlinearExprs
875
+ function (nl:: JuMP.GenericNonlinearExpr{GeneralVariableRef} )(supps... )
876
+ return restrict (nl, supps... )
877
+ end
878
+
815
879
# ###############################################################################
816
880
# COEFFICIENT METHODS
817
881
# ###############################################################################
0 commit comments