function [... NewRing,... penalty,... dmin... ]=atmatch(... Ring,... Variables,... Constraints,... Tolerance,... Calls,... verbose,... minimizer,... twissin) this functions modifies the Variables (parameters in THERING) to obtain a new THERING with the Constraints verified Ring : at lattice structure Variables : a structure array of parameters to vary with step size. Constraints : a structure array Tolerance : square sum of distance to wished constraints at which the minimizer stops Calls : number of calls verbose : verbosity 0-3 (see later) minimizer : @fminsearch (default) or @lsqnonlin twissin : open line matching initial parameters Variables struct('Indx',{[indx],... @(ring,varval)fun(ring,varval,...),... },... 'Parameter',{{'paramname',{M,N,...},...},... [initialvarval],... },... 'LowLim',{[val],[val],...},... 'HighLim',{[val],[val],...},... ) Constraints: structure array struct(... 'Fun',@functname(ring,lindata,globaldata), 'Min',min, 'Max',max, 'Weight',w, 'RefPoints',refpts); lindata is the output of atlinopt at the requested locations globaldata.fractune=tune from atlinopt globaldata.chromaticity=chrom from atlinopt functname must return a row vector of values to be optimized min, max and weight must have the same size as the return value of functname verbose to print out results. 0 (no output) 1 (initial values) 2 (iterations) 3 (result) Variables are changed within the range min<res<max with a Tolerance Given by Tolerance See also ATLINCONSTRAINT ATVARIABLEBUILDER
0001 function [NewRing,penalty,dmin]=atmatch(... 0002 Ring,Variables,Constraints,Tolerance,Calls,verbose,varargin) 0003 %function [... 0004 % NewRing,... 0005 % penalty,... 0006 % dmin... 0007 % ]=atmatch(... 0008 % Ring,... 0009 % Variables,... 0010 % Constraints,... 0011 % Tolerance,... 0012 % Calls,... 0013 % verbose,... 0014 % minimizer,... 0015 % twissin) 0016 % 0017 % this functions modifies the Variables (parameters in THERING) to obtain 0018 % a new THERING with the Constraints verified 0019 % 0020 % Ring : at lattice structure 0021 % Variables : a structure array of parameters to vary with step size. 0022 % Constraints : a structure array 0023 % Tolerance : square sum of distance to wished constraints at which the minimizer stops 0024 % Calls : number of calls 0025 % verbose : verbosity 0-3 (see later) 0026 % minimizer : @fminsearch (default) or @lsqnonlin 0027 % twissin : open line matching initial parameters 0028 % 0029 % Variables struct('Indx',{[indx],... 0030 % @(ring,varval)fun(ring,varval,...),... 0031 % },... 0032 % 'Parameter',{{'paramname',{M,N,...},...},... 0033 % [initialvarval],... 0034 % },... 0035 % 'LowLim',{[val],[val],...},... 0036 % 'HighLim',{[val],[val],...},... 0037 % ) 0038 % 0039 % 0040 % Constraints: structure array struct(... 0041 % 'Fun',@functname(ring,lindata,globaldata), 0042 % 'Min',min, 0043 % 'Max',max, 0044 % 'Weight',w, 0045 % 'RefPoints',refpts); 0046 % 0047 % lindata is the output of atlinopt at the requested locations 0048 % globaldata.fractune=tune from atlinopt 0049 % globaldata.chromaticity=chrom from atlinopt 0050 % 0051 % functname must return a row vector of values to be optimized 0052 % 0053 % min, max and weight must have the same size as the return value of 0054 % functname 0055 % 0056 % verbose to print out results. 0057 % 0 (no output) 0058 % 1 (initial values) 0059 % 2 (iterations) 0060 % 3 (result) 0061 % 0062 % Variables are changed within the range min<res<max with a Tolerance Given 0063 % by Tolerance 0064 % 0065 % See also ATLINCONSTRAINT ATVARIABLEBUILDER 0066 0067 0068 % 0069 % using least square. 0070 % 0071 % 0072 % History of changes 0073 % created : 27-8-2012 0074 % updated : 28-8-2012 constraints 'Fun' may output vectors 0075 % updated : 17-9-2012 output dmin 0076 % updated : 6-11-2012 added simulated annealing (annealing) 0077 % and simplex-simulated-annealing (simpsa) 0078 % for global minimum search. 0079 % updated : 21-02-2013 TipicalX included in 0080 % updated : 11-03-2013 (major) 0081 % function named 'atmatch'. 0082 % anonimous functions constraints and variable 0083 % Variable limits in variable. 0084 % update : 23-03-2013 0085 % atGetVaraiblesNumber added. 0086 % fixed Low High lim bug. 0087 % fixed function variables treat input values as 0088 % independent parameters to match. 0089 % introduced verbose flag. 0090 % updated 25-3-2013 varibles as absolute values and not variations. 0091 % Indx and Parmaeter switched in case of function. 0092 % setfield(...Parameter{:}) instead of Parameter{1} or{2} 0093 % reshaped initialization of tipx for lsqnonlin 0094 % atlinopt call optimized in the constraint evaluation call 0095 % changed constraint structure 0096 0097 %% optional arguments 0098 minimizer=@fminsearch; 0099 twissin=[]; 0100 0101 if length(varargin)==1 0102 if ~isstruct(varargin{1}) 0103 minimizer=varargin{1}; 0104 twissin=[]; 0105 else 0106 twissin=varargin{1}; 0107 minimizer=@fminsearch; 0108 end 0109 elseif length(varargin)==2 0110 minimizer=varargin{1}; 0111 twissin=varargin{2}; 0112 end 0113 0114 options=optimset(minimizer); 0115 0116 IniVals=atGetVariableValue(Ring,Variables); 0117 splitvar=@(varvec) reshape(mat2cell(varvec,cellfun(@length,IniVals),1),size(Variables)); 0118 0119 initval=cat(1,IniVals{:}); 0120 Blow=cat(1,Variables.LowLim); 0121 Bhigh=cat(1,Variables.HighLim); 0122 0123 % tolfun is the precisin of the minimum value tolx the accuracy of the 0124 % parameters (delta_0) 0125 % tipicalx is the value of tipical change of a variable 0126 % (useful if varibles have different ranges) 0127 tipx=ones(size(initval)); 0128 notzero=initval~=0; 0129 tipx(notzero)=initval(notzero); 0130 0131 [posarray,~,ic]=unique(cat(2,Constraints.RefPoints)); 0132 if posarray(1)<=0 || posarray(end) > length(Ring)+1 0133 error('atmatch:WrongInput','RefPoint out of range'); 0134 end 0135 indinposarray=mat2cell(ic(:)',1,arrayfun(@(s) length(s.RefPoints), Constraints)); 0136 evalfunc={Constraints.Fun}; 0137 0138 options=optimset(options,... 0139 'MaxFunEvals',Calls*100,... 0140 'MaxIter',Calls,... 0141 'TypicalX',tipx,... 0142 'TolFun',Tolerance,... 0143 'TolX',Tolerance); 0144 0145 if verbose == 0 0146 options = optimset(options,... 0147 'Display','off'); 0148 end 0149 0150 if verbose > 1 0151 options = optimset(options,... 0152 'Display','iter'); 0153 end 0154 0155 switch func2str(minimizer) 0156 case 'lsqnonlin' 0157 0158 f = @(d) evalvector(Ring,Variables,Constraints,splitvar(d),... 0159 evalfunc,posarray,indinposarray,twissin); % vector 0160 args={initval,Blow,Bhigh}; 0161 case 'fminsearch' 0162 0163 f = @(d)evalsum(Ring,Variables,Constraints,... 0164 splitvar(d),evalfunc,posarray,indinposarray,twissin); % scalar (sum of squares of f) 0165 args={initval}; 0166 case 'fmincon' 0167 0168 f = @(d)evalsum(Ring,Variables,Constraints,... 0169 splitvar(d),evalfunc,posarray,indinposarray,twissin); % scalar (sum of squares of f) 0170 args={initval,[],[],[],[],Blow,Bhigh,[]}; 0171 end 0172 0173 cstr1=atEvaluateConstraints(Ring,evalfunc,posarray,indinposarray,twissin); 0174 penalty0=atGetPenalty(cstr1,Constraints); 0175 0176 if verbose>0 0177 0178 disp('f2: '); 0179 disp(num2str(penalty0.^2)); 0180 disp('Sum of f2: '); 0181 disp(num2str(sum(penalty0.^2))); 0182 0183 end 0184 0185 %% Least Squares 0186 if sum(penalty0.*penalty0)>Tolerance 0187 dmin=minimizer(f,args{:},options); % wants a scalar 0188 else 0189 dmin=initval; 0190 end 0191 %% 0192 0193 NewRing=atApplyVariation(Ring,Variables,splitvar(dmin)); 0194 0195 cstr2=atEvaluateConstraints(NewRing,evalfunc,posarray,indinposarray,twissin); 0196 penalty=atGetPenalty(cstr2,Constraints); 0197 0198 if verbose>1 0199 0200 disp('-----oooooo----oooooo----oooooo----') 0201 disp(' ') 0202 disp('f2: '); 0203 disp(num2str(penalty.^2)); 0204 disp('Sum of f2: '); 0205 disp(num2str(sum(penalty.^2))); 0206 disp(' ') 0207 disp('-----oooooo----oooooo----oooooo----') 0208 0209 end 0210 if verbose>2 0211 splitpen=@(pen) reshape(mat2cell(pen,1,cellfun(@length,cstr1)),size(Constraints)); 0212 results=struct(... 0213 'val1',cstr1,... 0214 'val2',cstr2,... 0215 'penalty1',splitpen(penalty0),... 0216 'penalty2',splitpen(penalty)); 0217 atDisplayConstraintsChange(Constraints,results); 0218 atDisplayVariableChange(Ring,NewRing,Variables); 0219 end 0220 0221 function Val=evalvector(R,v,c,d,e,posarray,indinposarray,twissin) 0222 R=atApplyVariation(R,v,d); 0223 cstr=atEvaluateConstraints(R,e,posarray,indinposarray,twissin); 0224 Val=atGetPenalty(cstr,c); 0225 end 0226 0227 function sVal=evalsum(R,v,c,d,e,posarray,indinposarray,twissin) 0228 Val=evalvector(R,v,c,d,e,posarray,indinposarray,twissin); 0229 sVal=sum(Val.^2); 0230 end 0231 0232 end