A simple parameter optimization example.
29finalTime = np.pi * np.sqrt(mass / stiffness)
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)
39marker = osim.Marker(
'marker', body, osim.Vec3(0))
40model.addMarker(marker)
43joint = osim.SliderJoint(
'slider', model.getGround(), body)
44coord = joint.updCoordinate()
45coord.setName(
'position')
46model.addComponent(joint)
49spring = osim.SpringGeneralizedForce()
50spring.set_coordinate(
'position')
51spring.setRestLength(0.)
52spring.setStiffness(stiffness)
53spring.setViscosity(0.)
54model.addComponent(spring)
59moco = osim.MocoStudy()
60moco.setName(
'oscillator_spring_stiffness')
65problem = moco.updProblem()
69problem.setModel(model)
74problem.setTimeBounds(osim.MocoInitialBounds(0.),
75 osim.MocoFinalBounds(finalTime))
79problem.setStateInfo(
'/slider/position/value', osim.MocoBounds(-5., 5.),
80 osim.MocoInitialBounds(-0.5),
81 osim.MocoFinalBounds(0.25, 0.75))
85problem.setStateInfo(
'/slider/position/speed', [-20, 20], [0], [0])
89problem.addParameter(osim.MocoParameter(
'oscillator_mass',
'body',
'mass',
90 osim.MocoBounds(0, 10)))
94endpointCost = osim.MocoMarkerFinalGoal()
95endpointCost.setPointName(
'/markerset/marker')
96endpointCost.setReferenceLocation(osim.Vec3(0.5, 0, 0))
97problem.addGoal(endpointCost)
102solver = moco.initTropterSolver()
105moco.printToXML(
'optimize_mass.omoco')
109solution = moco.solve()
110solution.write(
'optimize_mass_solution.sto')