classdef bard722
    %{
    Reference
        John F. Bard
        Practical Bilevel Optimization: Algorithms and Applications
        Chapter 7, example 7.2.2, page 281
    %}
    properties(Constant)
        name = 'bard722';
        category = 'textbook';
        subcategory = 'bard_1998';
        datasets = {};
        x0 = [1.0, 0.4];
        y0 = [0.8];
    end


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