Skip to main content

computeDesignOptimizationEndpointFunction.m


% This function is part of the NMSM Pipeline, see file for full license.
%
% This function computes the terminal constraint (if any), discrete
% objective (if any), and total cost function objective for design
% optimization.
%
% (struct) -> (struct)
%


function output = computeDesignOptimizationEndpointFunction(inputs)

inputs.phase.state = [inputs.phase.initialstate; inputs.phase.finalstate];
inputs.phase.time = [inputs.phase.initialtime; inputs.phase.finaltime];
inputs.phase.control = ones(size(inputs.phase.time,1), ...
length(inputs.auxdata.minControl));
phase = inputs.phase;
if isfield(inputs, "parameter")
phase.parameter = inputs.parameter;
end
values = getDesignOptimizationValueStruct(phase, inputs.auxdata);
inputs = updateSystemFromUserDefinedFunctions(inputs, values);
modeledValues = calcTorqueBasedModeledValues(values, inputs.auxdata);

if ~isempty(inputs.auxdata.terminal)
output.eventgroup.event = calcDesignOptimizationTerminalConstraint( ...
values, modeledValues, inputs.auxdata);
end
discrete = calcDesignOptimizationDiscreteObjective(values, ...
modeledValues, inputs.auxdata);
% discrete = computeStaticParameterCost(inputs);
output.objective = calcDesignOptimizationObjective(discrete, ...
inputs.phase.integral, values.time(end), inputs.auxdata);
end

function cost = computeStaticParameterCost(inputs)
costTerms = inputs.auxdata.costTerms;
cost = 0;
for i = 1:length(costTerms)
costTerm = costTerms{i};
if strcmp(costTerm.type, "user_defined") && ...
strcmp(costTerm.cost_term_type, "discrete")
func = str2func(costTerm.function_name);
cost = cost + func(inputs);
end
end
end