1classdef MinimizeCost < opt.Objective
2 % MinimizeCost Minimize infrastructure cost subject to SLA constraints.
3 % Cost = server costs (
'name_servers') + rate costs (variable names
4 % containing
the station name and
'rate') + replica costs
5 % (
'name_replicas'). Mirrors native-Python MinimizeCost.
7 % serverCost/rateCost/replicaCost are containers.Map keyed by station name
8 % (
char) -> cost, or []. subjectTo
is a cell array of opt.Constraint.
17 function obj = MinimizeCost(serverCost, rateCost, replicaCost, subjectTo)
18 obj.serverCost = opt.MinimizeCost.suffixMap(serverCost,
'_servers');
19 obj.rateCost = opt.MinimizeCost.suffixMap(rateCost,
'');
20 obj.replicaCost = opt.MinimizeCost.suffixMap(replicaCost,
'_replicas');
21 if nargin >= 4 && ~isempty(subjectTo)
22 obj.constraints = subjectTo;
26 function tf = isMinimization(~), tf =
true; end
28 function total = evaluate(obj, ~, variableValues)
31 ks = keys(obj.serverCost);
33 if isKey(variableValues, ks{i})
34 val = variableValues(ks{i});
35 if opt.Objective.isScalarNumeric(val)
36 total = total + obj.serverCost(ks{i}) * val;
40 % rate costs (pattern match)
41 rks = keys(obj.rateCost);
42 vks = keys(variableValues);
47 if ~isempty(strfind(vname, pattern)) && ~isempty(strfind(lower(vname),
'rate')) %#ok<STREMP>
48 val = variableValues(vname);
49 if opt.Objective.isScalarNumeric(val)
50 total = total + obj.rateCost(pattern) * val;
56 pks = keys(obj.replicaCost);
58 if isKey(variableValues, pks{i})
59 val = variableValues(pks{i});
60 if opt.Objective.isScalarNumeric(val)
61 total = total + obj.replicaCost(pks{i}) * val;
69 function out = suffixMap(inMap, suffix)
70 out = containers.Map(
'KeyType',
'char',
'ValueType',
'double');
71 if nargin < 1 || isempty(inMap)
76 out([ks{i} suffix]) = inMap(ks{i});