API  4.3
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 
22 import os
23 import opensim as osim
24 import numpy as np
25 
26 # Setting variables
27 stiffness = 100.
28 mass = 5.
29 finalTime = np.pi * np.sqrt(mass / stiffness)
30 
31 # Defining the model
32 model = osim.Model()
33 model.setName('oscillator')
34 model.set_gravity(osim.Vec3(0, 0, 0))
35 body = osim.Body('body', np.multiply(0.5, mass), osim.Vec3(0), osim.Inertia(0))
36 model.addComponent(body)
37 
38 # Adding a marker to body in the model
39 marker = osim.Marker('marker', body, osim.Vec3(0))
40 model.addMarker(marker)
41 
42 # Allows translation along x.
43 joint = osim.SliderJoint('slider', model.getGround(), body)
44 coord = joint.updCoordinate()
45 coord.setName('position')
46 model.addComponent(joint)
47 
48 # Adds the spring component
49 spring = osim.SpringGeneralizedForce()
50 spring.set_coordinate('position')
51 spring.setRestLength(0.)
52 spring.setStiffness(stiffness)
53 spring.setViscosity(0.)
54 model.addComponent(spring)
55 
56 
57 # Create MocoStudy.
58 # ================
59 moco = osim.MocoStudy()
60 moco.setName('oscillator_spring_stiffness')
61 
62 
63 # Define the optimal control problem.
64 # ===================================
65 problem = moco.updProblem()
66 
67 # Model (dynamics).
68 # -----------------
69 problem.setModel(model)
70 
71 # Bounds.
72 # -------
73 # Initial time must be 0, final time is finalTime.
74 problem.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].
79 problem.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.
85 problem.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.
89 problem.addParameter(osim.MocoParameter('oscillator_mass', 'body', 'mass',
90  osim.MocoBounds(0, 10)))
91 
92 # Cost.
93 # -----
94 endpointCost = osim.MocoMarkerFinalGoal()
95 endpointCost.setPointName('/markerset/marker')
96 endpointCost.setReferenceLocation(osim.Vec3(0.5, 0, 0))
97 problem.addGoal(endpointCost)
98 
99 
100 # Configure the solver.
101 # =====================
102 solver = moco.initTropterSolver()
103 
104 # Now that we've finished setting up the study, print it to a file.
105 moco.printToXML('optimize_mass.omoco')
106 
107 # Solve the problem.
108 # ==================
109 solution = moco.solve()
110 solution.write('optimize_mass_solution.sto')