classdef bard511
    %{
    Reference
        John F. Bard
        Practical Bilevel Optimization: Algorithms and Applications
        Chapter 5, example 5.1.1, page 197
    %}
    properties
        name = 'bard511';
        category = 'textbook';
        subcategory = 'bard_1998';
        datasets = {};
        x0 = [4.0];
        y0 = [4.0];
    end


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