Skip to content

Cost Functions

jengstud edited this page May 17, 2021 · 4 revisions

In PARODIS, cost functions are subclasses of the abstract CostFunction class. The reason for this is that we allow cost functions to introduce their own slack variables and constraints, and for managing and collecting these to compile them into a YALMIP optimizer object, this offers a suitable way of doing so.

Cost Functions must implement the following abstract method:

  • [expr] = buildExpression(x, u, d, params, Ns, slacks) which returns the actual expression of the cost function over all scenarios that will be weighted and added to the cost function that is passed to the solver. This function needs to be able to deal with both symbolic expressions and numerical expressions

Optionally, these further methods can be defined:

  • [slacks] = getSlacks(model, agent, params) which must return a struct where each field is an introduced slack variable, i.e. a new YALMIP sdpvar
  • [constraints] = getConstraints(model, agent, slacks, params) which returns a matrix of YALMIP constraint expressions. These constraints can depend on any slacks defined by getSlacks of any other available cost functions, as well as any other parameters of the model and agent
  • [exprSingle] = buildExpressionSingleScen(x, u, d, params, slacks), which will return an expression for only one scenario. This function will not generally be interfaced by PARODIS, but it is useful to use it in buildExpression
  • [horizon] = evaluateHorizon(x, u, d, params, slacks) This function should return a vector containing the cost evolution over the prediction horizon. In MPC terms it should return $[ l_i( x(0|k), u(0|k) ),\ \dots,\ l_i( x(N_{pred}-1|k), u(N_{pred}-1|k)) ]$. It will only ever be called with numerical values as arguments.
classdef CostFunction < handle
    % Abstract super class for implementation of cost functions
    
    methods (Abstract,Static)
        % Function returning an expression for the cost function
        [expr] = buildExpression(x, u, d, params, Ns, slacks, T_s)
    end
    
    methods (Static)
        % Function for introducing custom slacks and corresponding constraints
        function [slacks] = getSlacks(model, agent, params)
            slacks = struct;
        end
        
        function [constraints] = getConstraints(model, agent, slacks, params)
            constraints = [];
        end
        
        % Optional function for expression for a single scenario
        function [exprSingle] = buildExpressionSingleScen(x, u, d, params, slacks, T_s)
            exprSingle = [];
        end
        
        function [horizon] = evaluateHorizon(x, u, d, params, slacks, T_s)
            % horizon = evaluateHorizon(x, u, d, params, slacks, T_s)    
            %   optional function that calculates l_i for each point in the
            %   horizon and returns [l_i(0|k) ... l_i(N_pred-1|k)]
            %
            % x         cell array with scenarios of xPred
            % u         uPred
            % d         cell array with scenarios of dPred
            % params    parameters
            % slacks    slacks
            % T_s       horizon time vector
            
            horizon = NaN(1, size(u, 2));
        end
    end
end