Skip to main content

computeDesignOptimizationMainFunction.m


% This function is part of the NMSM Pipeline, see file for full license.
%
% This function sets up GPOPS-II to run Design Optimization.
%
% (struct) -> (struct, struct)
% Assigns optimal control settings and runs Design Optimization


function output = computeDesignOptimizationMainFunction(inputs, params)
guess = setupCommonOptimalControlInitialGuess(inputs);
bounds = setupProblemBounds(inputs, params, guess);
guess = addUserDefinedTermsToGuess(guess, inputs);
setup = setupCommonOptimalControlSolverSettings(inputs, ...
bounds, guess, params, ...
@computeDesignOptimizationContinuousFunction, ...
@computeDesignOptimizationEndpointFunction);
checkInitialGuess(guess, inputs, ...
@computeDesignOptimizationContinuousFunction);
solution = gpops2(setup);
solution = solution.result.solution;
solution.auxdata = inputs;
if isfield(solution, 'parameter')
solution.phase.parameter = [solution.parameter];
end
output = computeDesignOptimizationContinuousFunction(solution);
output.solution = solution;
end

function bounds = setupProblemBounds(inputs, params, guess)
bounds = setupCommonOptimalControlBounds(inputs, params);
% setup parameter bounds
if strcmp(inputs.controllerType, 'synergy_driven')
if inputs.optimizeSynergyVectors
bounds.parameter.lower = -0.5 * ones(1, length(inputs.minParameter));
bounds.parameter.upper = 0.5 * ones(1, length(inputs.minParameter));
end
end
for i = 1:length(inputs.userDefinedVariables)
variable = inputs.userDefinedVariables{i};
if ~isfield(bounds, "parameter") || ...
~isfield(bounds.parameter, "lower")
bounds.parameter.lower = [-0.5];
bounds.parameter.upper = [0.5];
else
bounds.parameter.lower = [bounds.parameter.lower, ...
-0.5];
bounds.parameter.upper = [bounds.parameter.upper, ...
0.5];
end
end
if isfield(inputs, "finalTimeRange")
bounds.phase.finaltime.lower = guess.phase.time(end) - (0.5 - guess.phase.time(end));
bounds.phase.finaltime.upper = 0.5;
end
end
function guess = addUserDefinedTermsToGuess(guess, inputs)
for i = 1:length(inputs.userDefinedVariables)
variable = inputs.userDefinedVariables{i};
if ~isfield(guess, "parameter")
guess.parameter = [];
end
guess.parameter = [guess.parameter, ...
scaleToBounds( ...
variable.initial_values, ...
variable.upper_bounds, ...
variable.lower_bounds)];
end
end