Open georgios-vassos1 opened 10 months ago
Thank you for the runnable example. However, I don't see a bug: you are solving a different problem in each case. For example, see what happens when you invoke:
> Cost(w, x, Variable(1))
[1] 4.807175e-11
CVXR
correctly zeroes out the variables and returns the z
that minimizes f(z)
. In your graphical.opt
, you are providing the z
value, which affects the last constraint in Cost
. Concretely, the call to Cost
in your cvxr.opt
has two CVXR
unknown variables, whereas your graphical solution has only one. So what problem are you trying to solve? Perhaps a mathematical formulation would clarify everything.
Please be aware that the provided problem statement is intentionally simplified and does not fully represent the complexity of my actual problem. Its purpose is solely to illustrate the error I am encountering. While this simplified example does not encompass the entirety of my main issue, the error persists in this straightforward illustration. This simplified problem is implemented in the code I provided.
Identify the optimal quantity $z\ge0$ to order from the $p$ sources.
This computation involves solving a linear program defined as:
$$C(z)\doteq C(z;w,x)\doteq\min_{q_1,...,qp}\sum{i=1}^{p}w{i}q{i} $$
subject to the contraints: $$\sum{i=1}^{p}q{i}=z,\quad\text{and}\quad0 \leq q{i} \leq x{i} \ \text{for all} \ i=1,...,p$$
Suppose $f$ is a known auxiliary convex function.
$$\min_{z}[C(z) + f(z)]$$
subject to the constraints:
$$0 \leq z \leq \sum{i=1}^{p}x{i}$$
In summary, the problem involves minimizing the cost associated with ordering from $p$ sources, considering the capacity constraints and execution costs. The overall objective is to find the optimal order quantity $z$. The computation involves solving a linear program to determine the cost for each feasible order quantity and evaluating the auxiliary convex function $f(z)$ with the additional constraints on $z$.
Failure to Generate Correct Results in Nested Optimization Scenario
The code fails to generate accurate results when handling a nested optimization situation as detailed in the previous comment. The primary issue arises from utilizing the decision variable of the outer optimization program z
to constrain computations within the inner optimization program, particularly in computing allocation costs.
I am new to CVXR and perhaps I am overlooking the correct approach to model this problem.
library(CVXR)
# Problem instance
p <- 4L
w <- matrix(c(2.0, 1.0, 3.0, 5.0), ncol = 1L)
x <- c(5.0, 5.0, 5.0, 5.0)
# Auxiliary component
f <- function(z) (z-10.0)^2.0
# Decision variables
q <- Variable(p)
z <- Variable(1L)
# CVXR model
problem <- Problem(
Minimize(
f(z) + solve(
Problem(
Minimize(t(w) %*% q),
list(
q >= 0.0,
q <= x,
matrix(1.0, ncol = p) %*% q == z
)
),
solver = "ECOS"
)$value
),
list(
z >= 0.0,
z <= sum(x)
)
)
# Solution inspection
result <- solve(problem, solver = "ECOS")
result$getValue(z) [1] 10 result$value [1] 4.454504e-10
I have previously clarified in my comment that these values are incorrect.
The crux of the matter lies in how we integrate the decision variable from the outer optimization program into the inner optimization program within this nested structure. In such a scenario, the inner program lacks a mechanism to discern that this decision variable is not meant for optimization but rather should be treated as a fixed value, solely governed by the outer optimization.
Issue Description
The code encounters an issue when attempting to solve a nested optimization problem using CVXR. While the initial convex minimization problem is solved successfully, extending the optimization to include the sum of the output from the first minimization and an auxiliary function leads to code failure. Despite the convex nature of the objective function in the second optimization problem, visually confirmed through representative instances, a bug prevents the successful resolution of the nested optimization.
Code Snippet
Example with Output
Running the problem instance in my code above, if you print
manual$opt.z
, it will return the correct value for the optimal order (manual$opt.z
) and the correct minimum value of the objective (manual$opt.v
).You can visually verify these are the correct values with the help of the plot produced by
graphical.opt
when you setplt=TRUE
.To ensure the correctness of
cvxr.opt
, I compare its results with those ofgraphical.opt
. However, there is a discrepancy, as the outputs fromcvx.opt
are:The optimal ordering quantity is incorrect as it should have been
9
instead of10
, and same goes for the optimal value of objective that is4.454504e-10
when it should have been14
. Moreover, when I evaluate the objective functionCost(w, x, cvx$opt.z) + f(cvx$opt.z)
atcvx$opt.z
I get the value15
which is different fromcvx$opt.v
.Version