I'm writing a code for comparing multiple numerical optimization algorithms using multiple combination of parameter choices.
The traditional way is to insert tons of "if ... else ..." or "switch ... case ..." statements inside the iterations, but I want my code to generate a new standalone function for each combination of parameter & options.
The traditional way may look like this
option.secondOrder = 'BFGS';
option.tolerance = 1e-8;
result = optimization_algorithm(problem, option);
function r = optimization_algorithm(prob, opt)
I want to implement something like this
codeType = 'MATLAB';
fnfilename = optimization_algorithm.generate_function(option);
result = feval(fnfilename, problem);
For me, I can think of ways of doing this:
- construct a cell array containing all lines of codes, and using frprintf to generate a code file (MPT Toolbox use this method)
- have template code files containing part of code pieces, use string template to replace some of the variable names.
These methods are feasible, however, I want to know if there is a more systematic/organized way of doing this? The thing I want may be keyworded as "string template" or something?
I had some experiences in web development, and I am looking for something PHP-like in MATLAB ecosystem.
Is there anything like this?