API 4.4.1-2022-10-19-2c4045e59
For MATLAB, Python, Java, and C++ users
exampleOptimizeMass.py

A simple parameter optimization example.

1# -------------------------------------------------------------------------- #
2# OpenSim Moco: exampleOptimizeMass.py #
3# -------------------------------------------------------------------------- #
4# Copyright (c) 2019 Stanford University and the Authors #
5# #
6# Author(s): Noah Gordon, Jennifer Yong #
7# #
8# Licensed under the Apache License, Version 2.0 (the "License"); you may #
9# not use this file except in compliance with the License. You may obtain a #
10# copy of the License at http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17# -------------------------------------------------------------------------- #
18
19# Optimize the mass of a simple harmonic oscillator such that it follows the
20# correct trajectory specified by the state bounds and the MocoMarkerFinalGoal.
21
22import os
23import opensim as osim
24import numpy as np
25
26# Setting variables
27stiffness = 100.
28mass = 5.
29finalTime = np.pi * np.sqrt(mass / stiffness)
30
31# Defining the model
32model = osim.Model()
33model.setName('oscillator')
34model.set_gravity(osim.Vec3(0, 0, 0))
35body = osim.Body('body', np.multiply(0.5, mass), osim.Vec3(0), osim.Inertia(0))
36model.addComponent(body)
37
38# Adding a marker to body in the model
39marker = osim.Marker('marker', body, osim.Vec3(0))
40model.addMarker(marker)
41
42# Allows translation along x.
43joint = osim.SliderJoint('slider', model.getGround(), body)
44coord = joint.updCoordinate()
45coord.setName('position')
46model.addComponent(joint)
47
48# Adds the spring component
49spring = osim.SpringGeneralizedForce()
50spring.set_coordinate('position')
51spring.setRestLength(0.)
52spring.setStiffness(stiffness)
53spring.setViscosity(0.)
54model.addComponent(spring)
55
56
57# Create MocoStudy.
58# ================
59moco = osim.MocoStudy()
60moco.setName('oscillator_spring_stiffness')
61
62
63# Define the optimal control problem.
64# ===================================
65problem = moco.updProblem()
66
67# Model (dynamics).
68# -----------------
69problem.setModel(model)
70
71# Bounds.
72# -------
73# Initial time must be 0, final time is finalTime.
74problem.setTimeBounds(osim.MocoInitialBounds(0.),
75 osim.MocoFinalBounds(finalTime))
76
77# Position must be within [-5, 5] throughout the motion.
78# Initial position must be -0.5, final position must be within [0.25, 0.75].
79problem.setStateInfo('/slider/position/value', osim.MocoBounds(-5., 5.),
80 osim.MocoInitialBounds(-0.5),
81 osim.MocoFinalBounds(0.25, 0.75))
82
83# Speed must be within [-20, 20] throughout the motion.
84# Initial and final speed must be 0. Use compact syntax.
85problem.setStateInfo('/slider/position/speed', [-20, 20], [0], [0])
86
87# Add Parameter. The default initial guess for a parameter is the midpoint of
88# its bounds, *not* the value of a property in the model.
89problem.addParameter(osim.MocoParameter('oscillator_mass', 'body', 'mass',
90 osim.MocoBounds(0, 10)))
91
92# Cost.
93# -----
94endpointCost = osim.MocoMarkerFinalGoal()
95endpointCost.setPointName('/markerset/marker')
96endpointCost.setReferenceLocation(osim.Vec3(0.5, 0, 0))
97problem.addGoal(endpointCost)
98
99
100# Configure the solver.
101# =====================
102solver = moco.initTropterSolver()
103
104# Now that we've finished setting up the study, print it to a file.
105moco.printToXML('optimize_mass.omoco')
106
107# Solve the problem.
108# ==================
109solution = moco.solve()
110solution.write('optimize_mass_solution.sto')