Added 18/06/2025
Miscellaneous
basic_unconstrained
Dimension
{
"x": 4,
"y": 4,
"F": 1,
"G": 0,
"H": 0,
"f": 1,
"g": 0,
"h": 0
}
Solution
{
"optimality": "global",
"x": [1, -2, 3, -4],
"y": [1, -2, 3, -4],
"F": 0,
"G": [],
"H": [],
"f": 5,
"g": [],
"h": []
}
$title basic_unconstrained
$onText
minimise (x1 - 1)^2 + (x2 + 2)^2 + (x3 - 3)^2 + (x4 - 4)^2
subject to y = argmin((y1 - 1)^2 + (y2 + 2)^2 + (y3 - 3)^2 + (y4 - 4)^2 )
$offText
set i / 1*4 /;
variables obj_val_upper, obj_val_lower, x(i), y(i);
equations obj_eq_upper, obj_eq_lower;
parameters a(i) / '1' = -1.0, '2' = 2.0, '3' = -3.0, '4' = 4.0 /;
* Objective functions
obj_eq_upper.. obj_val_upper =e= sum(i, sqr(x(i) + a(i)));
obj_eq_lower.. obj_val_lower =e= sum(i, sqr(y(i) + a(i)));
* Solve
model basic_unconstrained / all /;
$echo bilevel x min obj_val_lower y obj_eq_lower > "%emp.info%"
solve basic_unconstrained us emp min obj_val_upper;
\subsection{basic\_unconstrained}
\label{subsec:basic_unconstrained}
% Description
A trivial example with no constraints and no dependencies between the upper and lower-level programs is used to diagnose solvers.
It's unique global optimum is found at $x=[1, -2, 3, -4]$ and $y=[1, -2, 3, -4]$.
% Equation
\begin{flalign*}
\minimise_{x, y} \quad
& (x_1-1)^2 + (x_2+2)^2 + (x_3-3)^2 + (x_4+4)^2 \\
\subjectto \quad
& y \in \argmin_{y}
\left((y_1-1)^2 + (y_2+2)^2 + (y_3-3)^2 + (y_4+4)^2\right)
\end{flalign*}
classdef basic_unconstrained
%{
A trivial example with no constraints and no dependencies between
the upper and lower-level programs is used to diagnose solvers.
minimise (x1 - 1)^2 + (x2 + 2)^2 + (x3 - 3)^2 + (x4 - 4)^2
subject to y = argmin((y1 - 1)^2 + (y2 + 2)^2 + (y3 - 3)^2 + (y4 - 4)^2 )
%}
properties(Constant)
name = 'basic_unconstrained';
category = 'miscellaneous';
subcategory = '';
datasets = {};
x0 = [1.0, -2.0, 3.0, -4.0];
y0 = [1.0, -2.0, 3.0, -4.0];
end
methods(Static)
% Upper-level objective function
function val = F(x, ~, ~)
val = (x(1)-1)^2 + (x(2)+2)^2 + (x(3)-3)^2 + (x(4)+4)^2;
end
% Upper-level inequality constraints
function val = G(~, ~, ~)
val = [];
end
% Upper-level equality constraints
function val = H(~, ~, ~)
val = [];
end
% Lower-level objective function
function val = f(~, y, ~)
val = (y(1)-1)^2 + (y(2)+2)^2 + (y(3)-3)^2 + (y(4)+4)^2 + 5.0;
end
% Lower-level inequality constraints
function val = g(~, ~, ~)
val = [];
end
% Lower-level equality constraints
function val = h(~, ~, ~)
val = [];
end
% If the bilevel program is parameterized by data, this function should
% provide code to read data file and return an appropriate structure.
function val = read_data(~)
val = [];
end
% Key are the function/variable names
% Values are their dimention
function n = dimentions(key, ~)
n = dictionary( ...
'x', 4, ...
'y', 4, ...
'F', 1, ...
'G', 0, ...
'H', 0, ...
'f', 1, ...
'g', 0, ...
'h', 0 ...
);
if isKey(n,key)
n = n(key);
end
end
end
end
from bolib3 import np
"""
A trivial example with no constraints and no dependencies between the upper and lower-level.
minimise (x1 - 1)^2 + (x2 + 2)^2 + (x3 - 3)^2 + (x4 - 4)^2
subject to y = argmin((y1 - 1)^2 + (y2 + 2)^2 + (y3 - 3)^2 + (y4 - 4)^2 )
"""
# Properties
name: str = 'basic_unconstrained'
category: str = 'miscellaneous'
subcategory: str = ''
datasets: list = []
# Feasible point
x0 = [1.0, -2.0, 3.0, -4.0]
y0 = [1.0, -2.0, 3.0, -4.0]
# Methods
def F(x, y, data=None):
"""
Upper-level objective function
(quadratic)
"""
return (x[0]-1)**2 + (x[1]+2)**2 + (x[2]-3)**2 + (x[3]+4)**2
def G(x, y, data=None):
"""
Upper-level inequality constraints
(none)
"""
return np.empty(0)
def H(x, y, data=None):
"""
Upper-level equality constraints
(none)
"""
return np.empty(0)
def f(x, y, data=None):
"""
Lower-level objective function
(quadratic)
"""
return (y[0]-1)**2 + (y[1]+2)**2 + (y[2]-3)**2 + (y[3]+4)**2 + 5.0
def g(x, y, data=None):
"""
Lower-level inequality constraints
(none)
"""
return np.empty(0)
def h(x, y, data=None):
"""
Lower-level equality constraints
(none)
"""
return np.empty(0)
def read_data(filepath=''):
"""
If the bilevel program is parameterized by data, this function should
provide code to read data file and return an appropriate python structure.
"""
pass
def dimensions(key='', data=None):
"""
If the argument 'key' is not specified, then:
- a dictionary mapping variable/function names (str) to the corresponding dimension (int) is returned.
If the first argument 'key' is specified, then:
- a single integer representing the dimension of the variable/function with the name {key} is returned.
"""
n = {
"x": 4, # Upper-level variables
"y": 4, # Lower-level variables
"F": 1, # Upper-level objective functions
"G": 0, # Upper-level inequality constraints
"H": 0, # Upper-level equality constraints
"f": 1, # Lower-level objective functions
"g": 0, # Lower-level inequality constraints
"h": 0, # Lower-level equality constraints
}
if key in n:
return n[key]
return n