0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 classdef softMetaConstraint < softConstraint
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 properties
0051 const1
0052 const2
0053 const2costScale
0054 comparison
0055 end
0056
0057
0058 methods
0059
0060 function obj = softMetaConstraint(varargin)
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075 p = inputParser;
0076
0077 p.addParamValue('sosObj','null', ...
0078 @(sosObj)strcmp(class(sosObj),'sos'));
0079 p.addParamValue('constraintType', 'null', ...
0080 @(constraintType)strcmp('meta',constraintType));
0081 p.addParamValue('fnc','null', ...
0082 @(fnc)any(strcmp({'matchCost','matchCostNotMin'},fnc)));
0083 p.addParamValue('constraint1','null', ...
0084 @(softCost1)any(strcmp(superclasses(softCost1),...
0085 'genericConstraint')));
0086 p.addParamValue('constraint2','null', ...
0087 @(softCost2)any(strcmp(superclasses(softCost2),...
0088 'genericConstraint')));
0089 p.addParamValue('constraint2costScale', 1, ...
0090 @(constraint2costScale)isnumeric(constraint2costScale));
0091 p.addParamValue('exponent',2,@(exponent)isnumeric(exponent));
0092 p.addParamValue('weight',1,@(weight)isnumeric(weight));
0093 p.addParamValue('name','noname',@(name)ischar(name));
0094
0095 p.parse(varargin{:});
0096
0097
0098
0099 if(p.Results.sosObj.containsConstraint(p.Results.constraint1) == 0)
0100 error('Constraint2 is not part of the specified sosObj');
0101 end
0102
0103 if(p.Results.sosObj.containsConstraint(p.Results.constraint2) == 0)
0104 error('Constraint2 is not part of the specified sosObj');
0105 end
0106
0107
0108
0109 obj.sosObj = p.Results.sosObj;
0110 obj.constraintType = p.Results.constraintType;
0111 obj.fnc = p.Results.fnc;
0112
0113 obj.weight = p.Results.weight;
0114 obj.exp = p.Results.exponent;
0115
0116 obj.const1 = p.Results.constraint1;
0117 obj.const2 = p.Results.constraint2;
0118 obj.const2costScale = p.Results.constraint2costScale;
0119
0120 if strcmp(obj.fnc,'matchCost')
0121 obj.comparison = @obj.minDiff;
0122 elseif strcmp(obj.fnc,'matchCostNotMin')
0123 obj.comparison = @obj.minDiffCond;
0124 else
0125 error(['fnc: "' obj.fnc, '" not yet supported']);
0126 end
0127
0128 obj.cost = NaN;
0129 obj.swCost = NaN;
0130
0131
0132 obj.label = [obj.constraintType,'_',obj.fnc,'_[',...
0133 obj.const1.name,']_[',...
0134 obj.const2.name,']_',...
0135 'x',num2str(obj.const2costScale),'_w',...
0136 num2str(obj.weight),'_e',num2str(obj.exp)];
0137 if any(strcmp(p.UsingDefaults,'name'))
0138 obj.name = obj.label;
0139 else
0140 obj.name = p.Results.name;
0141 end
0142
0143 verbosePrint('Soft META Constraint has been created', ...
0144 'softMetaConstraint_Constructor_endObjCreation');
0145
0146 end
0147
0148
0149 function cost = initCost(obj)
0150
0151 cost = obj.comparison(obj.const1.cost,obj.const2.cost);
0152 obj.cost = cost;
0153 end
0154
0155 function swCost = swapCost(obj)
0156
0157 if isnan(obj.const1.swCost)
0158 cost1 = obj.const1.cost;
0159 else
0160 cost1 = obj.const1.swCost;
0161 end
0162
0163 if isnan(obj.const2.swCost)
0164 cost2 = obj.const2.cost;
0165 else
0166 cost2 = obj.const2.swCost;
0167 end
0168
0169 swCost = obj.comparison(cost1,cost2);
0170 obj.swCost = swCost;
0171 end
0172
0173
0174 function cost = minDiff(obj,x1,x2)
0175
0176 cost = (abs((obj.const2costScale*x2) - x1)^obj.exp)*obj.weight*100;
0177 end
0178
0179
0180 function cost = minDiffCond(obj,x1,x2)
0181
0182 if x2 <= 0
0183 cost = ( ((abs(obj.const2costScale*x2 - x1)*x1)^0.5 )^obj.exp)*obj.weight*100;
0184 else
0185 cost = 0;
0186 end
0187 end
0188
0189 function cost = acceptSwap(obj)
0190
0191 cost = acceptSwap@genericConstraint(obj);
0192 end
0193
0194 function cost = rejectSwap(obj)
0195
0196 cost = rejectSwap@genericConstraint(obj);
0197 end
0198
0199 end
0200
0201 end
0202