LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
BudgetConstraint.m
1classdef BudgetConstraint < opt.Constraint
2 % BudgetConstraint Budget constraint: total cost <= budget.
3 properties
4 budget
5 costCoefficients % containers.Map name -> cost
6 end
7 methods
8 function obj = BudgetConstraint(budget, costCoefficients, name)
9 if nargin < 3, name = ''; end
10 obj@opt.Constraint(name);
11 obj.budget = budget;
12 if nargin >= 2 && ~isempty(costCoefficients)
13 obj.costCoefficients = costCoefficients;
14 else
15 obj.costCoefficients = containers.Map('KeyType', 'char', 'ValueType', 'double');
16 end
17 end
18 function n = generateName(obj)
19 n = sprintf('Budget_le_%g', obj.budget);
20 end
21 function c = computeCost(obj, variableValues)
22 c = 0.0;
23 ks = keys(obj.costCoefficients);
24 for i = 1:numel(ks)
25 if isKey(variableValues, ks{i})
26 c = c + obj.costCoefficients(ks{i}) * opt.Objective.numericValue(variableValues(ks{i}));
27 end
28 end
29 end
30 function v = evaluate(obj, ~, variableValues)
31 v = max(0.0, obj.computeCost(variableValues) - obj.budget);
32 end
33 end
34end