Best COSSAN Code

From COSSAN Wiki
Jump to: navigation, search

PLEASE DO NOT CODE COSSAN components like this

Best Obfuscated code

Xfun    = Function('Sexpression',...
   [' subsref(subsref(apply(<&Xrs&>,rmfield(getStructure(<&Xin&>),{' Sremovefields '})),'...
   'struct(type,.,subs,Tvalues)),struct(type,.,subs,logPf)) - '   num2str(log(targetProb),'%e')],...
   'CUseObject',{'Xrs',Xrs});

source [1] (Revision 5171)

Best Misuse of the object

  Xrbop   = add(Xrbop,'Xfunction_objectivefunction',...
           Function('Sexpression','<&XFunObjFunctionDeterministic&>+<&XFunExpectedCost&>',...
           'CuseObject',{'XFunObjFunctionDeterministic',XFunObjFunctionDeterministic},...
           'CuseObject',{'XFunExpectedCost',XFunExpectedCost}));


Useless duplication of the variables

Nmax        = Xsqp.Nmax;    %Maximum number of function evaluations that is allowed
fdp         = Xsqp.FiniteDifferencePerturbation;    %Perturbation for performing finite differences (required for gradient estimation)
Nmaxiter    = Xsqp.NMaximumIterations;              %Maximum number of iterations that is allowed.
tolfobj     = Xsqp.ToleranceObjectiveFunction;      %Termination tolerance on the value of the objective function.
tolcon      = Xsqp.ToleranceConstraint;             %Termination tolerance on the constraint violation.
tolx        = Xsqp.ToleranceDV;                      %Termination tolerance on the design variable vector
NEvaluationsPerBatch    = Xsqp.NEvaluationsPerBatch;    %number of evaluations per batch
LKeepFiles              = Xsqp.LKeepFiles;       

Why the functions are more user-friendly than the object

function [fobj,Vdfobj] = evaluate_objective_function(x,Xfun,Cdesvar,...
   Xin,fdp,Sxinname,Xpm,NEvaluationsPerBatch,LKeepFiles)
%
%%  1.   Definition of variables
global MGLOBAL_SQP_designs_OF NGLOBAL_SQP_Niter_OF SCALINGCONSTANT_SQP XSIMULATION_OUTPUT_SQP %declare global variables
Ndesvar = numel(x); %number of design variables
Lfobj   = false;    %logical value indicating whether or not value of objective function is available; default value is false
LVdfobj = false;    %logical value indicating whether or not value of gradient of objective function is available; default value is false

See Also