classdef dempe_dutta_2_2
    %{
    Reference:
        Stephan Dempe and Joydeep Dutta.
        Is bilevel programming a special case of a mathematical program with complementarity constraints?
        Mathematical Programming, volume 131, pages 37–48, year 2012.
        Example 2.2
    
    Upper level program:
        minimise x,y    F(x,y)
        subject to      G(x,y) >= 0
                        H(x,y) == 0
                        y solves the lower level program
    
    Lower level program:
        minimise y      f(x,y)
        subject to      g(x,y) >= 0
                        h(x,y) == 0
    %}
    properties(Constant)
        name = 'dempe_dutta_2_2';
        category = 'textbook';
        subcategory = 'dempe_dutta_2012';
        datasets = {};
        x0 = [0.0];
        y0 = [0.0, 0.0];
    end


    methods(Static)
        % Upper-level objective function (linear)
        function val = F(x, ~, ~)
            val = x(1);
        end
        
        % Upper-level inequality constraints (bound)
        function val = G(x, ~, ~)
            val = [x(1)];
        end
        
        % Upper-level equality constraints (none)
        function val = H(~, ~, ~)
            val = [];
        end
        
        % Lower-level objective function (linear)
        function val = f(~, y, ~)
            val = y(1);
        end
        
        % Lower-level inequality constraints (quadratic)
        function val = g(x, y, ~)
            val = [
                y(1)^2 - y(2) - x(1);
                y(1)^2 + y(2)
            ];
        end
        
        % Lower-level equality constraints (none)
        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', 1, ...
                'y', 2, ...
                'F', 1, ...
                'G', 1, ...
                'H', 0, ...
                'f', 1, ...
                'g', 2, ...
                'h', 0 ...
            );
            if isKey(n,key)
                n = n(key);
            end
        end
    end
end