This is a script file.
0001 classdef matRad_OptimizerFmincon < matRad_Optimizer
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 properties
0023 options
0024 wResult
0025 resultInfo
0026 end
0027
0028 methods
0029 function obj = matRad_OptimizerFmincon
0030
0031
0032 matRad_cfg = MatRad_Config.instance();
0033
0034 obj.wResult = [];
0035 obj.resultInfo = [];
0036
0037
0038
0039 obj.options = optimoptions('fmincon',...
0040 'Algorithm','interior-point',...
0041 'Display','iter-detailed',...
0042 'SpecifyObjectiveGradient',true,...
0043 'SpecifyConstraintGradient',true,...
0044 'AlwaysHonorConstraints', 'bounds',...
0045 'MaxIterations',matRad_cfg.propOpt.defaultMaxIter,...
0046 'MaxFunctionEvaluations',3000,...
0047 'CheckGradients',false,...
0048 'HessianApproximation',{'lbfgs',6},...
0049 'UseParallel',true,...
0050 'Diagnostics','on',...
0051 'ScaleProblem',true,...
0052 'PlotFcn',{@optimplotfval,@optimplotx,@optimplotfunccount,@optimplotconstrviolation,@optimplotstepsize,@optimplotfirstorderopt});
0053 end
0054
0055 function obj = optimize(obj,w0,optiProb,dij,cst)
0056
0057
0058
0059 lb = optiProb.lowerBounds(w0);
0060 ub = optiProb.upperBounds(w0);
0061
0062
0063
0064
0065
0066
0067 [obj.wResult,fVal,exitflag,info] = fmincon(@(x) obj.fmincon_objAndGradWrapper(x,optiProb,dij,cst),...
0068 w0,...
0069 [],[],...
0070 [],[],...
0071 lb,ub,...
0072 @(x) obj.fmincon_nonlconWrapper(x,optiProb,dij,cst),...
0073 obj.options);
0074
0075 obj.resultInfo = info;
0076 obj.resultInfo.fVal = fVal;
0077 obj.resultInfo.exitflag = exitflag;
0078 end
0079
0080 function [f, fGrad] = fmincon_objAndGradWrapper(obj,x,optiProb,dij,cst)
0081 f = optiProb.matRad_objectiveFunction(x,dij,cst);
0082 fGrad = optiProb.matRad_objectiveGradient(x,dij,cst);
0083 end
0084
0085 function [c,cEq,cJacob,cEqJacob] = fmincon_nonlconWrapper(obj,x,optiProb,dij,cst)
0086
0087 [cl,cu] = optiProb.matRad_getConstraintBounds(cst);
0088
0089
0090 clFinIx = isfinite(cl);
0091 cuFinIx = isfinite(cu);
0092
0093
0094 assert(isequal(size(cl),size(cu)));
0095 assert(all(cl <= cu));
0096
0097
0098
0099 isEqConstr = (cl == cu);
0100 eqIx = isEqConstr;
0101 ineqIx = ~isEqConstr;
0102
0103
0104 cVals = optiProb.matRad_constraintFunctions(x,dij,cst);
0105 cJacob = optiProb.matRad_constraintJacobian(x,dij,cst);
0106
0107
0108 cEq = cVals(eqIx & clFinIx);
0109 cEqJacob = cJacob(eqIx & clFinIx,:)';
0110
0111
0112
0113
0114 cL = cl(ineqIx & clFinIx) - cVals(ineqIx & clFinIx);
0115 cU = cVals(ineqIx & cuFinIx) - cu(ineqIx & cuFinIx);
0116 cJacobL = -cJacob(ineqIx & clFinIx,:);
0117 cJacobU = cJacob(ineqIx & cuFinIx,:);
0118
0119
0120 c = [cL; cU];
0121 cJacob = transpose([cJacobL; cJacobU]);
0122 end
0123
0124 function [statusmsg,statusflag] = GetStatus(obj)
0125 try
0126 statusmsg = obj.resultInfo.message;
0127 if obj.resultInfo.exitflag == 0
0128 statusflag = 0;
0129 elseif obj.resultInfo.exitflag > 0
0130 statusflag = 1;
0131 else
0132 statusflag = -1;
0133 end
0134 catch
0135 statusmsg = 'No Last Optimizer Status Available!';
0136 statusflag = -1;
0137 end
0138 end
0139 end
0140
0141 methods (Static)
0142 function available = IsAvailable()
0143
0144 available = exist('fmincon') == 6;
0145 end
0146 end
0147 end