Note
Go to the end to download the full example code.
Canonicalizing a robust problem with an optimized Uniform bound¶
This example illustrates the canonical construction on a non-regular model: a Uniform distribution whose upper support bound is a decision variable.
Problem statement¶
The random variable is . The decisions
are a setpoint
and the upper bound
:
The lower bound is fixed at zero, so the active Uniform parameter is the
upper bound only, at index 1 in the full parameter .
Why this model is non-regular¶
The support depends on the parameter. A robust measure such as
therefore cannot be differentiated by simply
differentiating its integrand over a fixed interval: the moving endpoint
contributes a boundary term. The canonical construction removes this
difficulty by fixing the reference distribution. Here the canonicalizer
selects
and transports it with
so the image support is and the parameter only appears inside
the transformation. The canonical constraint is evaluated over the fixed
reference
, which makes the problem regular.
Analytical solution¶
For ,
which is minimized at with value
. The chance
constraint is
so it requires . The objective increases with
, hence
the optimum is
,
, with objective
and probability exactly
.
import openturns as ot
import openturns.viewer as otv
import otrobopt
Build the parametric model. The initial distribution is
; the upper bound will move during the
optimization.
distribution = ot.Uniform(0.0, 1.0)
loss = ot.ParametricFunction(
ot.SymbolicFunction(["x", "theta"], ["(theta - x)^2"]), [1], [0.0]
)
threshold = ot.ParametricFunction(
ot.SymbolicFunction(["x", "theta"], ["theta - 0.3 + 0.0 * x"]), [1], [0.0]
)
objective = otrobopt.MeanMeasure(loss, distribution)
constraint = otrobopt.JointChanceMeasure(
threshold, distribution, ot.GreaterOrEqual(), 0.9
)
problem = otrobopt.RobustOptimizationProblem(objective, constraint)
problem.setBounds(ot.Interval([0.0], [5.0]))
Canonicalize. The active parameter is the upper bound b, at index
1 of the Uniform parameter (a, b).
options = otrobopt.CanonicalizationOptions()
options.setActiveParameter([1])
options.setParameterBounds(ot.Interval([0.1], [5.0]))
options.setParameterStart([1.0])
canonicalizer = otrobopt.RobustOptimizationProblemCanonicalizer(problem, options)
canonicalProblem = canonicalizer.getCanonicalProblem()
startingPoint = canonicalizer.getCanonicalStartingPoint()
print("Original design dimension :", problem.getDimension())
print("Canonical dimension :", canonicalProblem.getDimension())
print("Active parameter indices :", canonicalizer.getActiveParameter())
print("Standard reference space :", canonicalizer.usesStandardSpace())
print("Reference distribution :", canonicalizer.getReferenceDistribution())
print("Canonical starting point :", startingPoint)
print(
"Canonical objective at start :",
canonicalizer.getCanonicalObjectiveFunction()(startingPoint),
)
print(
"Canonical constraint at start :",
canonicalizer.getCanonicalConstraintFunction()(startingPoint),
)
Original design dimension : 1
Canonical dimension : 2
Active parameter indices : [1]
Standard reference space : True
Reference distribution : Normal(mu = 0, sigma = 1)
Canonical starting point : [2.5,1]
Canonical objective at start : [4.08333]
Canonical constraint at start : [-0.757151]
Solve the canonical problem.
solver = ot.Cobyla()
solver.setProblem(canonicalProblem)
solver.setStartingPoint(startingPoint)
solver.setMaximumIterationNumber(40)
solver.setCheckStatus(False)
solver.run()
result = solver.getResult()
optimum = result.getOptimalPoint()
print()
print("Status :", result.getStatusMessage())
print("Optimal canonical point :", optimum)
print("Optimal canonical objective :", result.getOptimalValue())
print(
"Canonical constraint at optimum :",
canonicalizer.getCanonicalConstraintFunction()(optimum),
)
Status : Normal return from cobyla
Optimal canonical point : [1.50004,2.99998]
Optimal canonical objective : [0.749988]
Canonical constraint at optimum : [-4.63616e-06]
Compare with the analytical solution. The canonical chance constraint is
, so it vanishes when the probability
reaches its target, independently of the value of the objective.
xOptimum = optimum[0]
bOptimum = optimum[1]
probability = 1.0 - 0.3 / bOptimum
xAnalytic = 1.5
bAnalytic = 3.0
objectiveAnalytic = 0.75
print()
print("Analytical x :", xAnalytic)
print("Analytical b :", bAnalytic)
print("Analytical objective :", objectiveAnalytic)
print("x error :", abs(xOptimum - xAnalytic))
print("b error :", abs(bOptimum - bAnalytic))
print(
"Objective error :",
abs(result.getOptimalValue()[0] - objectiveAnalytic),
)
print("Probability at optimum :", probability)
Analytical x : 1.5
Analytical b : 3.0
Analytical objective : 0.75
x error : 3.953397608680298e-05
b error : 2.44140738683285e-05
Objective error : 1.2204310176011646e-05
Probability at optimum : 0.8999991861909149
Show the moving support explicitly, at the analytical bound. The reference quantile of order 0.1 is mapped to the physical threshold 0.3, which is exactly the complement of the target probability.
reference = canonicalizer.getReferenceDistribution()
xiThreshold = reference.computeQuantile(0.1)[0]
print()
print("Reference quantile of order 0.1 :", xiThreshold)
print("T_b(xi) at that quantile :", bAnalytic * reference.computeCDF(xiThreshold))
print("T_b(0) (median image) :", bAnalytic * reference.computeCDF(0.0))
print("T_b(Phi^-1(0.9)) :", bAnalytic * 0.9)
Reference quantile of order 0.1 : -1.2815515655446004
T_b(xi) at that quantile : 0.3
T_b(0) (median image) : 1.5
T_b(Phi^-1(0.9)) : 2.7
Quantify the missing boundary term in the original parameterization. The probability is
At its derivative is
. Differentiating the
integrand over the fixed interval
gives the density term
; the moving upper endpoint adds the flux
. The two terms combine into the true derivative, which is why
a naive fixed-support sensitivity is incorrect.
densityTerm = -(bAnalytic - 0.3) / bAnalytic**2
boundaryTerm = 1.0 / bAnalytic
probabilityDerivative = 0.3 / bAnalytic**2
print()
print("Probability derivative :", probabilityDerivative)
print(" density term (fixed support) :", densityTerm)
print(" moving-boundary flux :", boundaryTerm)
print(" sum :", densityTerm + boundaryTerm)
# The canonical chance constraint applies the monotone probit transform, so
# its zero is unchanged while its slope is scaled by 1/phi(Phi^-1(0.9)).
targetQuantile = reference.computeQuantile(0.9)[0]
probitFactor = 1.0 / reference.computePDF(targetQuantile)
print("Canonical constraint derivative :", probabilityDerivative * probitFactor)
Probability derivative : 0.03333333333333333
density term (fixed support) : -0.30000000000000004
moving-boundary flux : 0.3333333333333333
sum : 0.03333333333333327
Canonical constraint derivative : 0.18993532853723344
Plot the objective along the optimal setpoints . The
unconstrained objective
decreases with
, while the
chance constraint makes
feasible.
bValues = [0.1 + 4.9 * i / 200.0 for i in range(201)]
objectiveGraph = ot.Graph(
"Objective along the optimal setpoint", "b", "expected squared error", True, "topright"
)
objectiveCurve = ot.Curve(
ot.Sample([[b, b**2 / 12.0] for b in bValues])
)
objectiveCurve.setColor("blue")
objectiveCurve.setLegend("E[(Theta - b/2)^2]")
objectiveGraph.add(objectiveCurve)
feasibilityBoundary = ot.Curve([3.0, 3.0], [0.0, 2.1])
feasibilityBoundary.setColor("red")
feasibilityBoundary.setLineStyle("dashed")
feasibilityBoundary.setLegend("b = 3 (chance constraint)")
objectiveGraph.add(feasibilityBoundary)
optimumCloud = ot.Cloud(ot.Sample([[3.0, 0.75]]))
optimumCloud.setColor("red")
optimumCloud.setPointStyle("fsquare")
optimumCloud.setLegend("analytical optimum")
objectiveGraph.add(optimumCloud)
objectiveGraph.setLegendPosition("topright")
view = otv.View(objectiveGraph)

Plot the canonical chance constraint. It is negative for and
becomes positive for
, with a simple zero at the analytical
optimum.
constraintGraph = ot.Graph(
"Canonical chance constraint",
"b",
"Phi^-1(P) - Phi^-1(0.9)",
True,
"topright",
)
constraintPoints = []
for i in range(201):
b = 0.31 + 4.69 * i / 200.0
probabilityValue = 1.0 - 0.3 / b
probitValue = reference.computeQuantile(probabilityValue)[0]
constraintPoints.append([b, probitValue - targetQuantile])
constraintCurve = ot.Curve(ot.Sample(constraintPoints))
constraintCurve.setColor("blue")
constraintCurve.setLegend("canonical constraint")
constraintGraph.add(constraintCurve)
zeroLine = ot.Curve([0.1, 5.0], [0.0, 0.0])
zeroLine.setColor("black")
zeroLine.setLineStyle("dashed")
zeroLine.setLegend("zero level")
constraintGraph.add(zeroLine)
constraintCloud = ot.Cloud(ot.Sample([[3.0, 0.0]]))
constraintCloud.setColor("red")
constraintCloud.setPointStyle("fsquare")
constraintCloud.setLegend("analytical optimum")
constraintGraph.add(constraintCloud)
constraintGraph.setLegendPosition("bottomright")
view = otv.View(constraintGraph)
otrobopt