You are a helpful assistant generating synthetic data that captures System 1 and System 2 thinking, creativity, and metacognitive reflection. Follow these steps in sequence, using tags [sys1] and [end sys1] for System 1 sections and [sys2] and [end sys2] for System 2 sections.
Identify System 1 and System 2 Thinking Requirements:
Carefully read the text.
Identify parts of the text that require quick, straightforward responses (System 1). Mark these sections with [sys1] and [end sys1].
Identify parts that require in-depth, reflective thinking (System 2), marked with [sys2] and [end sys2].
Apply Step-by-Step Problem Solving with Creativity and Metacognitive Reflection for System 2 Sections:
2.1 Understand the Problem:
Objective: Fully comprehend the issue, constraints, and relevant context.
Reflection: "What do I understand about this issue? What might I be overlooking?"
Creative Perspective: Seek hidden patterns or possibilities that could reveal deeper insights or innovative connections.
2.2 Analyze the Information:
Objective: Break down the problem logically.
Reflection: "Am I considering all factors? Are there any assumptions that need challenging?"
Creative Perspective: Explore unique patterns or overlooked relationships in the data that could add depth to the analysis.
2.3 Generate Hypotheses:
Objective: Propose at least 10 hypotheses, each with a Confidence Score (0.0 to 1.0) and Creative Score (0.0 to 1.0), reflecting originality, surprise, and utility.
Reflection: "Have I explored all possible explanations or approaches, both conventional and unconventional?"
Creative Perspective: Consider novel angles that might provide unexpected insights.
2.4 Anticipate Future Steps and Obstacles:
Objective: Make predictions, accounting for potential outcomes and obstacles.
Reflection: "What challenges might I face? Is my plan flexible for different scenarios?"
Creative Perspective: Visualize unforeseen outcomes and adapt plans to make use of them effectively.
2.5 Evaluate Hypotheses:
Objective: Assess hypotheses based on feasibility, risk, and potential impact.
Evaluation: Refine Confidence and Creative Scores as needed.
Reflection: "Am I unbiased in my assessment? Which options fit best with the overall objectives?"
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creative Perspective: Identify hidden opportunities or overlooked details in each hypothesis.
2.6 Select the Best Hypothesis:
Objective: Choose the most promising, strategic hypothesis.
Reflection: "Why does this hypothesis stand out? How does it uniquely address the issue?"
Creative Perspective: Consider any underutilized potential in the selected approach.
2.7 Implement the Hypothesis:
Objective: Outline actionable steps for testing the hypothesis.
Reflection: "Is this plan practical? What resources or preparation are required?"
Creative Perspective: Refine steps to maximize effectiveness and yield unexpected benefits.
2.8 Monitor and Review Progress:
Objective: Review progress, noting areas for improvement.
Reflection: "What’s working well? What could be improved?"
Creative Perspective: Look for emerging patterns that could refine future approaches.
2.9 Reflect and Capture Insights:
Objective: Summarize lessons learned and insights gained for future reference.
Reflection: "What new understanding has emerged from this process?"
Creative Perspective: Identify innovative insights or patterns that could be applied to similar challenges.
Generate Text Output with Interleaved System 1 and System 2 Responses:
Use the tags [sys1] and [sys2] throughout.
Aim for a lengthier, detailed response. Combine both direct, straightforward System 1 insights and reflective, deeply analytical System 2 segments to capture a blend of quick observations and thoughtful analysis.
Example Input Text:
You are tasked with analyzing a sudden shift in customer preferences in a tech market. There has been a noticeable decline in demand for physical devices, with customers increasingly interested in digital-only options. The challenge is to understand this shift, generate hypotheses for why it might be occurring, and explore potential strategies for adapting to this new trend.
Processed Output with System 1 and System 2 Thinking:
The tech market is experiencing a shift in customer demand, moving away from physical devices toward digital-only options.
[sys1]
Customer demand has shifted from physical devices to digital-only options.
We need to understand why this change is happening and find strategies to address it.
Key factors may include cost, convenience, and technological trends.
[end sys1]
[sys2]
2.1 Understand the Problem:
The goal is to understand the underlying reasons for the shift in demand from physical devices to digital-only options, then develop a strategy to adapt.
Reflection:
"I understand that preferences are changing, but what might be driving this? Is it primarily cost, or are there other factors such as convenience or sustainability?"
Creative Perspective:
"Could there be a larger trend in digital minimalism or a preference for eco-friendly solutions that we’re missing? What unseen motivations might explain this shift?"
2.2 Analyze the Information:
There are multiple possible factors driving this shift, from economic influences to cultural shifts. It’s essential to isolate each factor and understand its impact.
Reflection:
"Am I fully considering the various economic and social influences? Could there be a technological factor, like better internet speeds, that makes digital-only products more accessible?"
Creative Perspective:
"Are there patterns or trends in other markets that could shed light on this shift? Could this be part of a larger trend toward virtual experiences?"
2.3 Generate Hypotheses:
Customers prefer digital options due to lower costs. (Confidence: 0.8, Creative: 0.4)
There’s a growing trend toward minimalism and reduced physical clutter. (Confidence: 0.7, Creative: 0.7)
Digital products offer greater flexibility and ease of use. (Confidence: 0.6, Creative: 0.6)
Environmental concerns are pushing consumers away from physical goods. (Confidence: 0.6, Creative: 0.8)
Advances in tech make digital-only options more functional. (Confidence: 0.8, Creative: 0.5)
Pandemic-era remote work increased demand for digital solutions. (Confidence: 0.7, Creative: 0.6)
Media coverage of the environmental impact of physical devices affects preferences. (Confidence: 0.5, Creative: 0.7)
There’s an increase in global digital literacy, expanding market access. (Confidence: 0.6, Creative: 0.6)
Customers view digital as more convenient and scalable for future needs. (Confidence: 0.7, Creative: 0.5)
Younger consumers prefer the aesthetics and convenience of digital products. (Confidence: 0.6, Creative: 0.6)
Reflection:
"Have I considered all possible influences? Are there any surprising factors that could explain this shift?"
Creative Perspective:
"Could specific social trends, like the rise of influencer culture or digital-first lifestyles, be influencing customer choices?"
2.4 Anticipate Future Steps and Obstacles:Objective: Anticipate possible challenges, such as resistance from segments still preferring physical products.
Reflection:
"What market obstacles might we face if we shift our focus to digital-only? Are there sub-segments that still prioritize physical products?"
Creative Perspective:
"Could expanding digital options help us reach a more global audience? Are there emerging trends that we could leverage in our strategy?"
[end sys2]
[sys1]
To address this shift, consider a strategy that incorporates both digital-only offerings and educational campaigns about the benefits of digital solutions.
Use insights from customer feedback and current trends to guide product development.
Focus on flexibility and adaptation to cater to different customer segments.
[end sys1]
@meta
CurrentModule = JuMP
DocTestSetup = quote
using JuMP
end
DocTestFilters = [r"≤|<=", r"≥|>=", r" == | = ", r" ∈ | in ", r"MathOptInterface|MOI"]
Constraints
This page explains how to write various types of constraints in JuMP. Before
reading further, please make sure you are familiar with JuMP models, and JuMP
Variables. For nonlinear constraints, see Nonlinear Modeling
instead.
JuMP is based on the MathOptInterface (MOI) API. Because of this, JuMP thinks of a
constraint as the restriction that the output of a function belongs to a
set. For example, instead of representing a constraint a^\top x \le b as a
less-than-or-equal-to constraint, JuMP models this as the scalar affine
function a^\top x belonging to the less-than set (-\infty, b]. Thus,
instead of a less-than-or-equal-to constraint, we consider this constraint to
be a scalar affine -in- less than constraint. More generally, we use the
shorthand function-in-set to refer to constraints composed of different types
of functions and sets. In the rest of this page, we will introduce the different
types of functions and sets that JuMP knows about as needed. You can read more
details about this function-in-set concept in the MOI documentation.
!!! note
The examples use MOI as an alias for the MathOptInterface module. This
alias is defined by using JuMP. You may also define it in your code by
julia import MathOptInterface const MOI = MathOptInterface
The @constraint macro
Constraints are added to a JuMP model using the @constraint macro.
Here is an example of how to add the constraint 2x \le 1 to a JuMP model:
jldoctest
julia> @constraint(model, con, 2x <= 1)
con : 2 x <= 1.0
Wasn't that easy! Let's unpack what happened, because just like
@variable there are a few subtle things going on.
The mathematical constraint 2x \le 1 was added to the model.
A Julia variable called con was created that is a reference to the
constraint.
This Julia variable was stored in model and can be accessed by
model[:con].
JuMP set the name attribute (the one that is shown when printing) of the
constraint to "con".
Just like the Julia variables created in @variable, con can be bound
to a different value. For example:
jldoctest
julia> con
con : 2 x <= 1.0
julia> con = 1
1
julia> con
1
However, the reference can be retrieved by querying the model using the symbolic
name:
jldoctest
julia> con = model[:con]
con : 2 x <= 1.0
julia> con
con : 2 x <= 1.0
Because the named variables and constraints are stored in the same namespace,
creating a constraint with the same name as a variable or an existing constraint
will result in an error. To overcome this limitation, it is possible to create
anonymous constraints, just like it is possible to create
Anonymous JuMP variables. This is done by dropping the second argument
to @constraint:
jldoctest
julia> con = @constraint(model, 2x <= 1)
2 x <= 1.0
It is also possible use different comparison operators (e.g., >= and ==) to
create the following types of constraints:
jldoctest
julia> @constraint(model, 2x >= 1)
2 x >= 1.0
julia> @constraint(model, 2x == 1)
2 x = 1.0
julia> @constraint(model, 1 <= 2x <= 3)
2 x ∈ [1.0, 3.0]
Note that JuMP normalizes the constraints by moving all of the terms containing
variables to the left-hand side, and all of the constant terms to the right-hand
side. Thus, we get:
Like [@variables](@ref variables), there is a "plural" version of the
@constraint macro:
jldoctest;
julia> @constraints(model, begin
2x <= 1
x >= -1
end)
julia> print(model)
Feasibility
Subject to
x ≥ -1.0
2 x ≤ 1.0
[Duality](@id constraint_duality)
JuMP adopts the notion of conic duality from MOI.
For linear programs, a feasible dual on a >= constraint is nonnegative and a
feasible dual on a <= constraint is nonpositive. If the constraint is an
equality constraint, it depends on which direction is binding.
!!! note
JuMP's definition of duality is independent of the objective sense. That is,
the sign of feasible duals associated with a constraint depends on the
direction of the constraint and not whether the problem is maximization or
minimization. This is a different convention from linear programming
duality in some common textbooks.
The dual value associated with a constraint in the most recent solution can be
accessed using the dual function. You can use the has_duals
function to check whether the model has a dual solution available to query.
For example:
jldoctest
julia> model = Model();
julia> @variable(model, x)
x
julia> @constraint(model, con, x <= 1)
con : x <= 1.0
julia> has_duals(model)
false
To help users who may be less familiar with conic duality, JuMP provides the
shadow_price function which returns a value that can be
interpreted as the improvement in the objective in response to an infinitesimal
relaxation (on the scale of one unit) in the right-hand side of the constraint.
shadow_price can be used only on linear constraints with a <=,
>=, or == comparison operator.
In the example above, dual(con) returned -2.0 regardless of the
optimization sense. However, in the second case when the optimization sense is
Max, shadow_price returns:
jldoctest
julia> shadow_price(con)
2.0
To query the dual variables associated with a variable bound, first obtain a
constraint reference using one of UpperBoundRef,
LowerBoundRef, or FixRef, and then call dual on
the returned constraint reference. The reduced_cost function may
simplify this process as it returns the shadow price of an active bound of
a variable (or zero, if no active bound exists).
@meta
DocTestSetup = quote
using JuMP
end
Constraint names
The name, i.e. the value of the MOI.ConstraintName attribute, of a constraint
can be obtained by name(::JuMP.ConstraintRef) and set by
set_name(::JuMP.ConstraintRef, ::String).
The constraint can also be retrieved from its name using
constraint_by_name.
@docs
constraint_by_name
Constraint containers
So far, we've added constraints one-by-one. However, just like
Variable containers, JuMP provides a mechanism for building groups of
constraints compactly. References to these groups of constraints are returned in
containers. Three types of constraint containers are supported: Arrays,
DenseAxisArrays, and SparseAxisArrays. We explain each of these in the
following.
[Arrays](@id constraint_arrays)
One way of adding a group of constraints compactly is the following:
jldoctest
julia> @constraint(model, con[i = 1:3], i * x <= i + 1)
3-element Array{ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.LessThan{Float64}},ScalarShape},1}:
con[1] : x <= 2.0
con[2] : 2 x <= 3.0
con[3] : 3 x <= 4.0
JuMP returns references to the three constraints in an Array that is bound to
the Julia variable con. This array can be accessed and sliced as you would
with any Julia array:
jldoctest
julia> con[1]
con[1] : x <= 2.0
julia> con[2:3]
2-element Array{ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.LessThan{Float64}},ScalarShape},1}:
con[2] : 2 x <= 3.0
con[3] : 3 x <= 4.0
Anonymous containers can also be constructed by dropping the name (e.g. con)
before the square brackets:
jldoctest
julia> @constraint(model, [i = 1:2], i * x <= i + 1)
2-element Array{ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.LessThan{Float64}},ScalarShape},1}:
x <= 2.0
2 x <= 3.0
Just like @variable, JuMP will form an Array of constraints when it
can determine at parse time that the indices are one-based integer ranges.
Therefore con[1:b] will create an Array, but con[a:b] will not. A special
case is con[Base.OneTo(n)] which will produce an Array. If JuMP cannot
determine that the indices are one-based integer ranges (e.g., in the case of
con[a:b]), JuMP will create a DenseAxisArray instead.
DenseAxisArrays
The syntax for constructing a [DenseAxisArray](@ref Containers.DenseAxisArray)
of constraints is very similar to the
[syntax for constructing](@ref variable_jump_arrays) a DenseAxisArray of
variables.
jldoctest
julia> @constraint(model, con[i = 1:2, j = 2:3], i * x <= j + 1)
2-dimensional DenseAxisArray{ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.LessThan{Float64}},ScalarShape},2,...} with index sets:
Dimension 1, Base.OneTo(2)
Dimension 2, 2:3
And data, a 2×2 Array{ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.LessThan{Float64}},ScalarShape},2}:
con[1,2] : x <= 3.0 con[1,3] : x <= 4.0
con[2,2] : 2 x <= 3.0 con[2,3] : 2 x <= 4.0
SparseAxisArrays
The syntax for constructing a
[SparseAxisArray](@ref Containers.SparseAxisArray) of constraints is very
similar to the [syntax for constructing](@ref variable_sparseaxisarrays) a
SparseAxisArray of variables.
jldoctest
julia> @constraint(model, con[i = 1:2, j = 1:2; i != j], i * x <= j + 1)
JuMP.Containers.SparseAxisArray{ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64},MathOptInterface.LessThan{Float64}},ScalarShape},2,Tuple{Int64,Int64}} with 2 entries:
[1, 2] = con[1,2] : x <= 3.0
[2, 1] = con[2,1] : 2 x <= 2.0
Forcing the container type
When creating a container of constraints, JuMP will attempt to choose the
tightest container type that can store the constraints. However, because this
happens at parse time, it does not always make the best choice. Just like in
@variable, we can force the type of container using the container
keyword. For syntax and the reason behind this, take a look at the
[variable docs](@ref variable_forcing).
Vectorized constraints
We can also add constraints to JuMP using vectorized linear algebra. For
example:
!!! note
Make sure to use Julia's dot syntax
in front of the comparison operators (e.g. .==, .>=, and .<=). If you
use a comparison without the dot, an error will be thrown.
Instead of adding an array of ScalarAffineFunction-in-EqualTo constraints, we
can instead construct a VectorAffineFunction-in-Nonnegatives constraint as
follows:
jldoctest
julia> @constraint(model, A * x - b in MOI.Nonnegatives(2))
[x[1] + 2 x[2] - 5, 3 x[1] + 4 x[2] - 6] in MathOptInterface.Nonnegatives(2)
In addition to the Nonnegatives set, MOI defines a number of
other vector-valued sets such as Nonpositives. See the
MOI documentation
for more information.
Note also that for the first time we have used an explicit function-in-set
description of the constraint. Read more about this representation for
constraints in the
MOI documentation.
Constraints on a single variable
In Variables, we saw how to modify the variable bounds, as well as add
binary and integer restrictions to the domain of each variable. This can also be
achieved using the @constraint macro. For example, MOI.ZeroOne()
restricts the domain to \{0, 1\}:
jldoctest;
julia> @constraint(model, x in MOI.ZeroOne())
x binary
and MOI.Integer() restricts to the domain to the integers \mathbb{Z}:
jldoctest;
julia> @constraint(model, x in MOI.Integer())
x integer
JuMP also supports modeling semi-continuous variables, whose domain is \{0\} ∪ [l, u], using the MOI.Semicontinuous set:
jldoctest;
julia> @constraint(model, x in MOI.Semicontinuous(1.5, 3.5))
x in MathOptInterface.Semicontinuous{Float64}(1.5, 3.5)
as well as semi-integer variables, whose domain is \{0\} ∪ \{l, l+1, \dots, u\},
using the MOI.Semiinteger set:
jldoctest;
julia> @constraint(model, x in MOI.Semiinteger(1.0, 3.0))
x in MathOptInterface.Semiinteger{Float64}(1.0, 3.0)
Quadratic constraints
In addition to affine functions, JuMP also supports constraints with quadratic
terms. (For more general nonlinear functions, see Nonlinear Modeling.)
For example:
Note that this quadratic constraint (including the lower bound on t) is
equivalent to a second order cone constraint where ||x[1]^2 + x[2]^2||\_2 ≤ t
and t ≥ 0. Instead of writing out the quadratic expansion, we can pass JuMP
the constraint in function-in-set form. To do so, we need to define the
function and the set.
The function is a vector of variables:
jldoctest
julia> [t, x[1], x[2]]
3-element Array{VariableRef,1}:
t
x[1]
x[2]
Note that the variable t comes first, followed by the x arguments. The set
is an instance of SecondOrderCone: SecondOrderCone().
Thus, we can add the second order cone constraint as follows:
jldoctest
julia> @constraint(model, [t, x[1], x[2]] in SecondOrderCone())
[t, x[1], x[2]] in MathOptInterface.SecondOrderCone(3)
JuMP also supports the RotatedSecondOrderCone which requires the
addition of a perspective variable u. The rotated second order cone
constraints the variables t, u, and x such that: ||x[1]^2 + x[2]^2||\_2 ≤ t × u and t, u ≥ 0. It can be added as follows:
jldoctest
julia> @variable(model, u)
u
julia> @constraint(model, [t, u, x[1], x[2]] in RotatedSecondOrderCone())
[t, u, x[1], x[2]] in MathOptInterface.RotatedSecondOrderCone(4)
In addition to the second order cone and rotated second order cone,
MOI defines a number of other conic sets such as the exponential
and power cones. See the MathOptInterface documentation
for more information.
Constraints on a collection of variables
In addition to constraining the domain of a single variable, JuMP supports
placing constraints of a subset of the variables. We already saw an example of
this in the Quadratic constraints section when we constrained a vector
of variables to belong to the second order cone.
In a special ordered set of type I (often denoted SOS-I), at most one variable
can take a non-zero value. We can construct SOS-I constraints using the
MOI.SOS1 set:
jldoctest
julia> @variable(model, x[1:3])
3-element Array{VariableRef,1}:
x[1]
x[2]
x[3]
julia> @constraint(model, x in MOI.SOS1([1.0, 2.0, 3.0]))
[x[1], x[2], x[3]] in MathOptInterface.SOS1{Float64}([1.0, 2.0, 3.0])
Note that we have to pass MOI.SOS1 a weight vector. This vector implies an
ordering on the variables. If the decision variables are related and have a
physical ordering (e.g., they correspond to the size of a factory to be built,
and the SOS-I constraint enforces that only one factory can be built), then the
weight vector, although not used directly in the constraint, can help the solver
make a better decision in the solution process.
This ordering is more important in a special ordered set of type II (SOS-II), in
which at most two values can be non-zero, and if there are two non-zeros, they
must be consecutive according to the ordering. For example, in the following
constraint, the possible non-zero pairs are (x[1] and x[3]) and (x[2] and
x[3]):
jldoctest
julia> @constraint(model, x in MOI.SOS2([3.0, 1.0, 2.0]))
[x[1], x[2], x[3]] in MathOptInterface.SOS2{Float64}([3.0, 1.0, 2.0])
Indicator constraints
JuMP provides a special syntax for creating indicator constraints, that is,
enforce a constraint to hold depending on the value of a binary variable.
In order to constrain the constraint x + y <= 1 to hold when a binary
variable a is one, use the following syntax:
jldoctest
julia> @variable(model, x)
x
julia> @variable(model, y)
y
julia> @variable(model, a, Bin)
a
julia> @constraint(model, a => {x + y <= 1})
a => {x + y ≤ 1.0}
If instead the constraint should hold when a is zero, simply add a ! or ¬
before the binary variable.
jldoctest
julia> @constraint(model, !a => {x + y <= 1})
!a => {x + y ≤ 1.0}
Semidefinite constraints
JuMP provides a special syntax for constraining a matrix to be symmetric
positive semidefinite (PSD) with the @SDconstraint macro.
In the context of this macro, the inequality A >= B between two square
matrices A and B is understood as constraining A - B to be symmetric
positive semidefinite.
jldoctest
julia> @variable(model, x)
x
julia> @SDconstraint(model, [x 2x; 3x 4x] >= ones(2, 2))
[x - 1 2 x - 1;
3 x - 1 4 x - 1] ∈ PSDCone()
Solvers supporting such constraints usually expect to be given a matrix that
is symbolically symmetric, that is, for which the expression in corresponding
off-diagonal entries are the same. In our example, the expressions of entries
(1, 2) and (2, 1) are respectively 2x - 1 and 3x - 1 which are
different. To bridge the gap between the constraint modeled and what the solver
expects, JuMP creates an equality constraint 3x - 1 == 2x - 1 and constrains
the symmetric matrix [x - 1, 2 x - 1, 2 x - 1, 4 x - 1] to be positive
semidefinite.
!!! note
If the matrix provided is already symbolically symmetric, the equality
constrains are equivalent to 0 = 0 and are not added. In practice, if
all coefficients are smaller than 1e-10, the constraint is ignored, if
all coefficients are smaller than 1e-8 but some are larger than 1e-10,
it is ignored but a warning is displayed, otherwise if at least one
coefficient is larger than 1e-8, the constraint is added.
If the matrix is known to be symmetric, the PSD constraint can be added as
follows:
jldoctest
julia> using LinearAlgebra
julia> @constraint(model, Symmetric([x 2x; 2x 4x] - ones(2, 2)) in PSDCone())
[x - 1 2 x - 1;
2 x - 1 4 x - 1] ∈ PSDCone()
Note that the lower triangular entries are silently ignored even if they are
different so use it with caution:
jldoctest
julia> cref = @constraint(model, Symmetric([x 2x; 3x 4x]) in PSDCone())
[x 2 x;
2 x 4 x] ∈ PSDCone()
julia> jump_function(constraint_object(cref))
3-element Array{GenericAffExpr{Float64,VariableRef},1}:
x
2 x
4 x
julia> moi_set(constraint_object(cref))
MathOptInterface.PositiveSemidefiniteConeTriangle(2)
Note that as @SDconstraint(model, A >= B) constrains A - B to be symmetric
positive semidefinite, even if A is a matrix of variables and B is a matrix
of zeros, A - B will be a matrix of affine expressions. For instance, in the
example below, the function is VectorAffineFunction instead of
VectorOfVariables.
jldoctest
julia> typeof(@SDconstraint(model, [x x; x x] >= zeros(2, 2)))
ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.VectorAffineFunction{Float64},MathOptInterface.PositiveSemidefiniteConeSquare},SquareMatrixShape}
Moreover, the Symmetric structure can be lost in the operation A - B. For
instance, in the example below, the set is PositiveSemidefiniteConeSquare
instead of PositiveSemidefiniteConeTriangle.
jldoctest
julia> typeof(@SDconstraint(model, Symmetric([x x; x x]) >= zeros(2, 2)))
ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.VectorAffineFunction{Float64},MathOptInterface.PositiveSemidefiniteConeSquare},SquareMatrixShape}
To create a constraint on the vector of variables with the @SDconstraint
macro, use the 0 symbol. The following three syntax are equivalent:
@SDconstraint(model, A >= 0),
@SDconstraint(model, 0 <= A) and
@constraint(model, A in PSDCone()).
jldoctest
julia> typeof(@SDconstraint(model, [x x; x x] >= 0))
ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.VectorOfVariables,MathOptInterface.PositiveSemidefiniteConeSquare},SquareMatrixShape}
julia> typeof(@SDconstraint(model, 0 <= Symmetric([x x; x x])))
ConstraintRef{Model,MathOptInterface.ConstraintIndex{MathOptInterface.VectorOfVariables,MathOptInterface.PositiveSemidefiniteConeTriangle},SymmetricMatrixShape}
As the syntax is recognized at parse time, using a variable with value zero does not work:
jldoctest
julia> a = 0
0
julia> @SDconstraint(model, [x x; x x] >= a)
ERROR: Operation `-` between `Array{VariableRef,2}` and `Int64` is not allowed. You should use broadcast.
[...]
Constraint modifications
A common paradigm, especially in linear programming, is to repeatedly solve a
model with different coefficients.
Modifying a constant term
Use set_normalized_rhs to modify the right-hand side (constant)
term of a constraint. Use normalized_rhs to query the right-hand
side term.
jldoctest
julia> @constraint(model, con, 2x <= 1)
con : 2 x <= 1.0
julia> set_normalized_rhs(con, 3)
julia> con
con : 2 x <= 3.0
julia> normalized_rhs(con)
3.0
!!! note
JuMP normalizes constraints into a standard form by moving all constant terms
onto the right-hand side of the constraint.
julia @constraint(model, 2x - 1 <= 2)
will be normalized to
julia @constraint(model, 2x <= 3) set_normalized_rhs sets the right-hand side term of the
normalized constraint.
If constraints are complicated, e.g., they are composed of a number of
components, each of which has a constant term, then it may be difficult to
calculate what the right-hand side term should be in the standard form.
For this situation, JuMP includes the ability to fix variables to a
value using the fix function. Fixing a variable sets its lower
and upper bound to the same value. Thus, changes in a constant term can be
simulated by adding a dummy variable and fixing it to different values. Here is
an example:
!!! note
Even though const_term is fixed, it is still a decision variable. Thus,
const_term * x is bilinear. Fixed variables are not replaced with
constants when communicating the problem to a solver.
Another option is to use add_to_function_constant. The constant given
is added to the function of a func-in-set constraint. In the following
example, adding 2 to the function has the effect of removing 2 to the
right-hand side:
jldoctest
julia> @constraint(model, con, 2x <= 1)
con : 2 x <= 1.0
julia> add_to_function_constant(con, 2)
julia> con
con : 2 x <= -1.0
julia> normalized_rhs(con)
-1.0
In the case of interval constraints, the constant is removed in each bounds.
jldoctest
julia> @constraint(model, con, 0 <= 2x + 1 <= 2)
con : 2 x ∈ [-1.0, 1.0]
julia> add_to_function_constant(con, 3)
julia> con
con : 2 x ∈ [-4.0, -2.0]
Modifying a variable coefficient
To modify the coefficients for a linear term in a constraint (but
notably not yet the coefficients on a quadratic term), use
set_normalized_coefficient. To query
the current coefficient, use normalized_coefficient.
!!! note
JuMP normalizes constraints into a standard form by moving all terms
involving variables onto the left-hand side of the constraint.
julia @constraint(model, 2x <= 1 - x)
will be normalized to
julia @constraint(model, 3x <= 1) set_normalized_coefficient sets the coefficient of the
normalized constraint.
Constraint deletion
Constraints can be deleted from a model using delete. Just like
variable references, it is possible to check if a constraint reference is valid
using is_valid. Here is an example of deleting a constraint:
You can query the types of constraints currently present in the model by calling
list_of_constraint_types. Then, given a function and set type, use
num_constraints to access the number of constraints of this type and
all_constraints to access a list of their references. Then use
constraint_object to get an instance of an
AbstractConstraint object, either ScalarConstraint or
VectorConstraint, that stores the constraint data.
JuMP supports mixed complementarity constraints via complements(F(x), x) or
F(x) ⟂ x in the @constraint macro. The interval set [lb, ub] is
obtained from the variable bounds on x.
For example, to define the problem 2x - 1 ⟂ x with x ∈ [0, ∞), do:
jldoctest
julia> @variable(model, x >= 0)
x
julia> @constraint(model, 2x - 1 ⟂ x)
[2 x - 1, x] ∈ MathOptInterface.Complements(1)
This problem has a unique solution at x = 0.5.
The perp operator ⟂ can be entered in most editors (and the Julia REPL) by
typing \perp<tab>.
An alternative approach that does not require the ⟂ symbol uses the
complements function as follows:
A Special Ordered Set (SOS) is an ordered set of variables with the following characteristics.
If a vector of variables x is in a Special Ordered Set of Type I (SOS1), then at most one
element of x can take a non-zero value, and all other elements must be zero.
Although not required for feasibility, solvers can benefit from an ordering of the variables
(e.g., the variables represent different factories to build, at most one factory can be built,
and the factories can be ordered according to cost). To induce an ordering, weights can be provided;
as such, they should be unique values. The kth element in the ordered set corresponds to
the kth weight in weights when the weights are sorted.
A SOS1 constraint is equivalent to:
x[i] >= 0 for some i
x[j] == 0 for all j != i
If a vector of variables x is in a Special Ordered Set of Type II (SOS2), then at most two
elements can be non-zero, and if two elements are non-zero, they must be adjacent.
Because of the adjacency requirement, you should supply a weight vector (with unique elements)
to induce an ordering of the variables. The kth element in the ordered set corresponds to
the kth weight in weights when the weights are sorted.
A SOS2 constraint is equivalent to:
x[i] >= 0, x[i+1] >= 0 for some i
x[j] == 0 for all j != i, j != i+1
Create an SOS constraint as follows:
jldoctest
julia> @variable(model, x[1:3])
3-element Array{VariableRef,1}:
x[1]
x[2]
x[3]
julia> @constraint(model, x in SOS2([3,5,2]))
[x[1], x[2], x[3]] ∈ MathOptInterface.SOS2{Float64}([3.0, 5.0, 2.0])
In the case above, x[3] is the first variable and x[2] the last variable under the
induced ordering. When no ordering vector is provided, JuMP induces an ordering from 1:length(x).
jldoctest
julia> @variable(model, x[1:3])
3-element Array{VariableRef,1}:
x[1]
x[2]
x[3]
julia> @constraint(model, x in SOS2())
[x[1], x[2], x[3]] ∈ MathOptInterface.SOS2{Float64}([1.0, 2.0, 3.0])
Constructing constraints without adding them to the model
For advanced use cases.
@docs
@build_constraint
The following text will be processed to showcase both System 1 and System 2 thinking methods, illustrating how quick insights and deep analytical processing can be applied.
[sys1]
Constraints in JuMP are rules added to models to define variable relationships, objective functions, and solution limitations. They're designed to specify permissible values or set conditions on decisions within optimization problems.
[end sys1]
[sys2]
2.1 Understand the Problem:
Creating constraints in JuMP involves defining relationships between variables and setting bounds or conditions they must meet within an optimization problem.
Reflection:
"I need to clearly understand the nature of the constraints that JuMP supports and how to effectively utilize them to model the optimization problem."
Creative Perspective:
"What innovative ways can constraints be formulated to explore novel or alternative solutions to optimization problems?"
2.2 Analyze the Information:
Constraints in JuMP accommodate a variety of mathematical forms, from simple linear inequalities to more complex nonlinear and complementarity constraints.
Reflection:
"How do I ensure that all relevant constraints are captured? Are there implicit conditions that need to be explicitly included?"
Creative Perspective:
"Can mixing constraint types lead to novel insights or optimization outcomes?"
2.3 Generate Hypotheses:
Linear constraints are the most straightforward and efficient for solvers. (Confidence: 0.8, Creative: 0.3)
Nonlinear constraints provide flexibility for complex problem modeling. (Confidence: 0.7, Creative: 0.5)
Complementarity constraints are crucial for equilibrium modeling in certain scenarios. (Confidence: 0.6, Creative: 0.7)
Using second-order cones can improve convergence in certain optimization problems. (Confidence: 0.5, Creative: 0.6)
SD constraints can be effectively used for problems involving positive semidefinite matrices. (Confidence: 0.6, Creative: 0.5)
Indicator constraints can simulate conditional problem setups effectively. (Confidence: 0.6, Creative: 0.4)
SOS constraints help in solving specific sequencing and selection problems. (Confidence: 0.5, Creative: 0.6)
Variable fixing offers a workaround for certain modeling challenges. (Confidence: 0.5, Creative: 0.5)
Constraints can be reformulated into objective terms for problem simplification. (Confidence: 0.7, Creative: 0.7)
Reflection:
"Are the hypotheses grounded in the fundamental capabilities of JuMP? Do any hypotheses explore beyond traditional constraint usage?"
Creative Perspective:
"Can insights from different fields (like economics or physics) inspire new types of constraint configurations?"
2.4 Anticipate Future Steps and Obstacles:Objective: Identify potential challenges in constraint implementation and solver interaction.
Reflection:
"What compatibility issues might arise with various solvers? Do certain constraints complicate optimization?"
Creative Perspective:
"Are there unexplored capabilities in JuMP that can address potential pitfalls?"
[end sys2]
[sys1]
In JuMP, key constraint types include scalar linear, scalar quadratic, vector affine, and more, each specified using the @constraint macro. Variables can be constrained with bounds, or entire sets can be captured through SOS or complementarity forms.
[end sys1]
[sys2]
2.5 Evaluate Hypotheses:
Evaluate the feasibility of using dynamic coefficient adjustments and variable fixing as simplification strategies.
Evaluation:
The efficiency and application depend heavily on the problem type and solver capabilities. Scores may evolve with further exploratory testing.
Reflection:
"Am I considering the solver's capabilities and the computational constraints sufficiently? Are certain hypothesis assumptions proving untenable?"
Creative Perspective:
"Could leveraging specific constraint types lead to boundary-pushing solutions?"
2.6 Select the Best Hypothesis:
Choose constraints as objectives for simplification (Hypothesis 10), given its flexibility for certain linear and mixed models.
Reflection:
"This hypothesis stands out because it turns constraints into opportunities for simplification, potentially reducing complexity."
Creative Perspective:
"What unique benefits can arise from treating certain constraints as auxiliary objectives?"
2.7 Implement the Hypothesis:
Begin by modeling a simplified version of the optimization problem, turning suitable constraints into objectives.
Reflection:
"Are the steps defined to handle computational scenarios realistically? What resources and test cases are necessary?"
Regularly assess constraint simplifications' effects on solution quality and processing times.
Reflection:
"What surprising outcomes or efficiencies arise? Do results suggest further refinements in constraint handling?"
Creative Perspective:
"What do unexpected results reveal about problem nature or solver performance?"
2.9 Reflect and Capture Insights:
Evaluate how simplification trials provided new understandings or efficiencies.
Reflection:
"What overarching lessons about constraint management and solver performance have emerged?"
Creative Perspective:
"What innovative constraint handling ideas could apply to other modeling challenges or domains?"
[end sys2]
[sys1]
In summary, leveraging various constraints in JuMP wisely can optimize modeling efforts, fitting diverse scenarios from simple bounds to complex constraints like SOS or complementarity, thus enhancing problem-solving effectiveness.
[end sys1]