Skip to content

Commit

Permalink
Merge pull request #215 from rcnl-org/candidate
Browse files Browse the repository at this point in the history
Candidate
  • Loading branch information
cvhammond committed Jun 15, 2023
2 parents f4695fb + 0d105f3 commit 5cdbbc4
Show file tree
Hide file tree
Showing 105 changed files with 1,537 additions and 952 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info Ref="src/core/TreatmentOptimization/costTerms" Type="Relative"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="cc1969ad-586b-4b5c-8496-892291913346" type="Reference"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="1" type="DIR_SIGNIFIER"/>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info>
<Category UUID="FileClassCategory">
<Label UUID="derived"/>
<Label UUID="design"/>
</Category>
</Info>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="isTrackingCostTerm.m" type="File"/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="costTerms" type="File"/>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info>
<Category UUID="FileClassCategory">
<Label UUID="derived"/>
<Label UUID="design"/>
</Category>
</Info>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="writeNeuralControlPersonalizationOsimxFile.m" type="File"/>
22 changes: 6 additions & 16 deletions src/DesignOptimization/DesignOptimization.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% This function is part of the NMSM Pipeline, see file for full license.
%
% () -> ()
%
%

% ----------------------------------------------------------------------- %
% The NMSM Pipeline is a toolkit for model personalization and treatment %
Expand All @@ -11,7 +11,7 @@
% National Institutes of Health (R01 EB030520). %
% %
% Copyright (c) 2021 Rice University and the Authors %
% Author(s): Marleny Vega %
% Author(s): Marleny Vega, Claire V. Hammond %
% %
% Licensed under the Apache License, Version 2.0 (the "License"); %
% you may not use this file except in compliance with the License. %
Expand All @@ -26,19 +26,9 @@
% ----------------------------------------------------------------------- %

function [output, inputs] = DesignOptimization(inputs, params)
pointKinematics(inputs.mexModel);
inverseDynamics(inputs.mexModel);
inputs = getStateDerivatives(inputs);
inputs = setupGroundContact(inputs);
inputs = getSplines(inputs);
inputs = checkStateGuess(inputs);
inputs = checkControlGuess(inputs);
inputs = checkParameterGuess(inputs);
inputs = getIntegralBounds(inputs);
inputs = getPathConstraintBounds(inputs);
inputs = getTerminalConstraintBounds(inputs);
inputs = getDesignVariableInputBounds(inputs);
if strcmp(inputs.controllerType, 'synergy_driven')
inputs = makeTreatmentOptimizationInputs(inputs, params);
initializeMexOrMatlabParallelFunctions(inputs.mexModel);
if strcmp(inputs.controllerType, 'synergy_driven')
inputs = setupMuscleSynergies(inputs);
end
output = computeDesignOptimizationMainFunction(inputs, params);
Expand All @@ -47,4 +37,4 @@
inputs.splineSynergyActivations = spaps(inputs.initialGuess.time, ...
inputs.initialGuess.control(:, inputs.numCoordinates + 1:end)', 0.0000001);
inputs.synergyLabels = inputs.initialGuess.controlLabels(:, inputs.numCoordinates + 1:end);
end
end
4 changes: 2 additions & 2 deletions src/DesignOptimization/DesignOptimizationTool.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ function DesignOptimizationTool(settingsFileName)
settingsTree = xml2struct(settingsFileName);
[inputs, params] = parseDesignOptimizationSettingsTree(settingsTree);
[outputs, inputs] = DesignOptimization(inputs, params);
reportDesignOptimizationResults(outputs, inputs);
reportTreatmentOptimizationResults(outputs, inputs);
saveDesignOptimizationResults(outputs, inputs);
end
end
35 changes: 6 additions & 29 deletions src/DesignOptimization/calcDesignOptimizationDiscreteObjective.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,11 @@
% permissions and limitations under the License. %
% ----------------------------------------------------------------------- %

function discrete = calcDesignOptimizationDiscreteObjective(values, params)
function discrete = calcDesignOptimizationDiscreteObjective(values, ...
modeledValues, auxdata)

discrete = [];
if isfield(params, 'discrete')
for i = 1:length(params.discrete.tracking)
costTerm = params.discrete.tracking{i};
if costTerm.isEnabled
switch costTerm.type
case "synergy_vectors"
discrete = cat(2, discrete, ...
calcTrackingCoordinateIntegrand(params, ...
values.time, values.statePositions, ...
costTerm.coordinate));
otherwise
throw(MException('', ['Cost term type ' costTerm.type ...
' does not exist for this tool.']))
end
end
end
for i = 1:length(params.discrete.minimizing)
costTerm = params.discrete.minimizing{i};
if costTerm.isEnabled
switch costTerm.type
otherwise
throw(MException('', ['Cost term type ' costTerm.type ...
' does not exist for this tool.']))
end
end
end
end
[costTermCalculations, allowedTypes] = ...
generateCostTermStruct("discrete", "DesignOptimization");
discrete = calcTreatmentOptimizationCost( ...
costTermCalculations, allowedTypes, values, modeledValues, auxdata);
end
48 changes: 9 additions & 39 deletions src/DesignOptimization/calcDesignOptimizationIntegrand.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% This function is part of the NMSM Pipeline, see file for full license.
%
% () -> ()
%
%

% ----------------------------------------------------------------------- %
% The NMSM Pipeline is a toolkit for model personalization and treatment %
Expand All @@ -11,7 +11,7 @@
% National Institutes of Health (R01 EB030520). %
% %
% Copyright (c) 2021 Rice University and the Authors %
% Author(s): Marleny Vega %
% Author(s): Marleny Vega, Claire V. Hammond %
% %
% Licensed under the Apache License, Version 2.0 (the "License"); %
% you may not use this file except in compliance with the License. %
Expand All @@ -25,42 +25,12 @@
% permissions and limitations under the License. %
% ----------------------------------------------------------------------- %

function integrand = calcDesignOptimizationIntegrand(values, params, ...
phaseout)
integrand = [];
for i = 1:length(params.integral.tracking)
costTerm = params.integral.tracking{i};
if costTerm.isEnabled
switch costTerm.type
case "coordinate"
integrand = cat(2, integrand, ...
calcTrackingCoordinateIntegrand(params, ...
values.time, values.statePositions, ...
costTerm.coordinate));
case "controller"
integrand = cat(2, integrand, ...
calcTrackingControllerIntegrand(params, values, ...
costTerm.controller));
otherwise
throw(MException('', ['Cost term type ' costTerm.type ...
' does not exist for this tool.']))
end
end
end
for i = 1:length(params.integral.minimizing)
costTerm = params.integral.minimizing{i};
if costTerm.isEnabled
switch costTerm.type
case "joint_jerk"
integrand = cat(2, integrand, ...
calcMinimizingJointJerkIntegrand(values.controlJerks, ...
params, costTerm.coordinate));
otherwise
throw(MException('', ['Cost term type ' costTerm.type ...
' does not exist for this tool.']))
end
end
end
integrand = scaleToBounds(integrand, params.maxIntegral, params.minIntegral);
function integrand = calcDesignOptimizationIntegrand(values, ...
modeledValues, auxdata)
[costTermCalculations, allowedTypes] = ...
generateCostTermStruct("continuous", "DesignOptimization");
integrand = calcTreatmentOptimizationCost( ...
costTermCalculations, allowedTypes, values, modeledValues, auxdata);
integrand = integrand ./ (auxdata.maxIntegral - auxdata.minIntegral);
integrand = integrand .^ 2;
end
6 changes: 5 additions & 1 deletion src/DesignOptimization/calcDesignOptimizationObjective.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
% permissions and limitations under the License. %
% ----------------------------------------------------------------------- %

function objective = calcDesignOptimizationObjective(discrete, continuous)
function objective = calcDesignOptimizationObjective(discrete, ...
continuous, finalTime, inputs)
continuousObjective = sum(continuous) / length(continuous);
if isfield(inputs, "finalTimeRange")
continuousObjective = continuousObjective / finalTime;
end
discreteObjective = sum(discrete) / length(discrete);
if isnan(discreteObjective); discreteObjective = 0; end
objective = continuousObjective + discreteObjective;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% This function is part of the NMSM Pipeline, see file for full license.
%
% () -> ()
%
%

% ----------------------------------------------------------------------- %
% The NMSM Pipeline is a toolkit for model personalization and treatment %
Expand All @@ -25,13 +25,20 @@
% permissions and limitations under the License. %
% ----------------------------------------------------------------------- %

function phaseout = computeDesignOptimizationContinuousFunction(inputs)
function modeledValues = computeDesignOptimizationContinuousFunction(inputs)

values = getDesignOptimizationValueStruct(inputs.phase, inputs.auxdata);
phaseout = calcTorqueBasedModeledValues(values, inputs.auxdata);
phaseout = calcSynergyBasedModeledValues(values, inputs.auxdata, phaseout);
phaseout.dynamics = calcDesignOptimizationDynamicsConstraint(values, inputs.auxdata);
phaseout.path = calcDesignOptimizationPathConstraint(values, phaseout, inputs.auxdata);
phaseout.integrand = calcDesignOptimizationIntegrand(values, inputs.auxdata, ...
phaseout);
end
inputs = updateSystemFromUserDefinedFunctions(inputs, values);
modeledValues = calcTorqueBasedModeledValues(values, inputs.auxdata);
modeledValues = calcSynergyBasedModeledValues(values, inputs.auxdata, ...
modeledValues);
modeledValues.dynamics = calcDesignOptimizationDynamicsConstraint(values, ...
inputs.auxdata);
if ~isempty(inputs.auxdata.path)
modeledValues.path = calcDesignOptimizationPathConstraint(values, ...
modeledValues, inputs.auxdata);
end
modeledValues.integrand = calcDesignOptimizationIntegrand(values, ...
modeledValues, inputs.auxdata);
end

35 changes: 28 additions & 7 deletions src/DesignOptimization/computeDesignOptimizationEndpointFunction.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% This function is part of the NMSM Pipeline, see file for full license.
%
% () -> ()
%
%

% ----------------------------------------------------------------------- %
% The NMSM Pipeline is a toolkit for model personalization and treatment %
Expand All @@ -11,7 +11,7 @@
% National Institutes of Health (R01 EB030520). %
% %
% Copyright (c) 2021 Rice University and the Authors %
% Author(s): Marleny Vega %
% Author(s): Marleny Vega, Claire V. Hammond %
% %
% Licensed under the Apache License, Version 2.0 (the "License"); %
% you may not use this file except in compliance with the License. %
Expand All @@ -31,14 +31,35 @@
inputs.phase.time = [inputs.phase.initialtime; inputs.phase.finaltime];
inputs.phase.control = ones(size(inputs.phase.time,1), ...
length(inputs.auxdata.minControl));
values = getDesignOptimizationValueStruct(inputs.phase, inputs.auxdata);
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);
output.eventgroup.event = calcDesignOptimizationTerminalConstraint( ...
values, modeledValues, inputs.auxdata);
end
discrete = calcDesignOptimizationDiscreteObjective(values, inputs.auxdata);
discrete = calcDesignOptimizationDiscreteObjective(values, ...
modeledValues, inputs.auxdata);
% discrete = computeStaticParameterCost(inputs);
output.objective = calcDesignOptimizationObjective(discrete, ...
inputs.phase.integral);
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

Loading

0 comments on commit 5cdbbc4

Please sign in to comment.