LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
MinimizeCost.m
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.
6 %
7 % serverCost/rateCost/replicaCost are containers.Map keyed by station name
8 % (char) -> cost, or []. subjectTo is a cell array of opt.Constraint.
9
10 properties
11 serverCost
12 rateCost
13 replicaCost
14 end
15
16 methods
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;
23 end
24 end
25
26 function tf = isMinimization(~), tf = true; end
27
28 function total = evaluate(obj, ~, variableValues)
29 total = 0.0;
30 % server costs
31 ks = keys(obj.serverCost);
32 for i = 1:numel(ks)
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;
37 end
38 end
39 end
40 % rate costs (pattern match)
41 rks = keys(obj.rateCost);
42 vks = keys(variableValues);
43 for i = 1:numel(rks)
44 pattern = rks{i};
45 for j = 1:numel(vks)
46 vname = vks{j};
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;
51 end
52 end
53 end
54 end
55 % replica costs
56 pks = keys(obj.replicaCost);
57 for i = 1:numel(pks)
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;
62 end
63 end
64 end
65 end
66 end
67
68 methods (Static)
69 function out = suffixMap(inMap, suffix)
70 out = containers.Map('KeyType', 'char', 'ValueType', 'double');
71 if nargin < 1 || isempty(inMap)
72 return;
73 end
74 ks = keys(inMap);
75 for i = 1:numel(ks)
76 out([ks{i} suffix]) = inMap(ks{i});
77 end
78 end
79 end
80end
Definition Station.m:245