1classdef BudgetConstraint < opt.Constraint
2 % BudgetConstraint Budget constraint: total cost <= budget.
5 costCoefficients % containers.Map name -> cost
8 function obj = BudgetConstraint(budget, costCoefficients, name)
9 if nargin < 3, name =
''; end
10 obj@opt.Constraint(name);
12 if nargin >= 2 && ~isempty(costCoefficients)
13 obj.costCoefficients = costCoefficients;
15 obj.costCoefficients = containers.Map(
'KeyType',
'char',
'ValueType',
'double');
18 function n = generateName(obj)
19 n = sprintf(
'Budget_le_%g', obj.budget);
21 function c = computeCost(obj, variableValues)
23 ks = keys(obj.costCoefficients);
25 if isKey(variableValues, ks{i})
26 c = c + obj.costCoefficients(ks{i}) * opt.Objective.numericValue(variableValues(ks{i}));
30 function v = evaluate(obj, ~, variableValues)
31 v = max(0.0, obj.computeCost(variableValues) - obj.budget);