# ================ moco = osim.MocoStudy() moco.setName('oscillator_spring_stiffness') # Define the optimal control problem. # =================================== problem = moco.updProblem() # Model (dynamics). # ----------------- problem.setModel(model) # Bounds. # ------- # Initial time must be 0, final time is finalTime. problem.setTimeBounds(osim.MocoInitialBounds(0.), osim.MocoFinalBounds(finalTime)) # Position must be within [-5, 5] throughout the motion. # Initial position must be -0.5, final position must be within [0.25, 0.75]. problem.setStateInfo('/slider/position/value', osim.MocoBounds(-5., 5.), osim.MocoInitialBounds(-0.5), osim.MocoFinalBounds(0.25, 0.75)) # Speed must be within [-20, 20] throughout the motion. # Initial and final speed must be 0. Use compact syntax. problem.setStateInfo('/slider/position/speed', [-20, 20], [0], [0]) # Add Parameter. The default initial guess for a parameter is the midpoint of # its bounds, *not* the value of a property in the model. problem.addParameter(
problem.setModel(simModel) #Navigate to guess directory os.chdir('..\\GuessFiles') #Set guess path guessPath = os.getcwd() #Set time bounds on the problem ##### NOTE: first iteration with end time bounds seemed to try and solve to the ##### the final end time bound (i.e. 1.0) rather than as fast as possible. Test ##### and see whether removing these fixes it. # Set to a percentage of the final guess time to end timeLB = 0.9 * osim.Storage(taskName + '_StartingGuess.sto').getLastTime() timeUB = 1.1 * osim.Storage(taskName + '_StartingGuess.sto').getLastTime() problem.setTimeBounds(osim.MocoInitialBounds(0.0), osim.MocoFinalBounds(timeLB, timeUB)) # problem.setTimeBounds(0,[]) #Define marker end point goals osimHelper.addMarkerEndPoints(taskName, problem, simModel) #Define kinematic task bounds osimHelper.addTaskBounds(taskName, problem, simModel, taskBoundsElv, taskBoundsRot, taskBoundsAng) #Add a control cost to the problem problem.addGoal(osim.MocoControlGoal('effort', 1)) #Add a final time goal to the problem problem.addGoal(osim.MocoFinalTimeGoal('time', 1))
# ================ study = osim.MocoStudy() study.setName('sliding_mass') # Define the optimal control problem. # =================================== problem = study.updProblem() # Model (dynamics). # ----------------- problem.setModel(model) # Bounds. # ------- # Initial time must be 0, final time can be within [0, 5]. problem.setTimeBounds(osim.MocoInitialBounds(0.), osim.MocoFinalBounds(0., 5.)) # Position must be within [-5, 5] throughout the motion. # Initial position must be 0, final position must be 1. problem.setStateInfo('/slider/position/value', osim.MocoBounds(-5, 5), osim.MocoInitialBounds(0), osim.MocoFinalBounds(1)) # Speed must be within [-50, 50] throughout the motion. # Initial and final speed must be 0. Use compact syntax. problem.setStateInfo('/slider/position/speed', [-50, 50], [0], [0]) # Applied force must be between -50 and 50. problem.setControlInfo('/actuator', osim.MocoBounds(-50, 50)) # Cost. # ----- problem.addGoal(osim.MocoFinalTimeGoal())
solver = moco.initCasADiSolver() solver.set_num_mesh_points(25) solver.set_dynamics_mode('implicit') solver.set_optim_convergence_tolerance(1e-4) solver.set_optim_constraint_tolerance(1e-4) solver.set_optim_solver('ipopt') solver.set_transcription_scheme('hermite-simpson') solver.set_enforce_constraint_derivatives(True) solver.set_optim_hessian_approximation('limited-memory') solver.set_optim_finite_difference_scheme('forward') #Set bounds on the problem. problem = moco.updProblem() #Set the time bounds (from kinematics file) problem.setTimeBounds(osim.MocoInitialBounds(3.572), osim.MocoFinalBounds(6.044)) #Set kinematic bounds (mins and maxes) #Shoulder elevation charValue = '/jointset/' + coordSet.get('shoulder_elv').getJoint().getName( ) + '/' + coordSet.get('shoulder_elv').getName() + '/value' charSpeed = '/jointset/' + coordSet.get('shoulder_elv').getJoint().getName( ) + '/' + coordSet.get('shoulder_elv').getName() + '/speed' mn = coordSet.get('shoulder_elv').getRangeMin() mx = coordSet.get('shoulder_elv').getRangeMax() problem.setStateInfo(charValue, osim.MocoBounds(mn, mx)) problem.setStateInfo(charSpeed, osim.MocoBounds(-50, 50)) del (mn, mx, charValue, charSpeed)
def test_bounds(self): model = osim.Model() model.setName('sliding_mass') model.set_gravity(osim.Vec3(0, 0, 0)) body = osim.Body('body', 2.0, osim.Vec3(0), osim.Inertia(0)) model.addComponent(body) joint = osim.SliderJoint('slider', model.getGround(), body) coord = joint.updCoordinate() coord.setName('position') model.addComponent(joint) actu = osim.CoordinateActuator() actu.setCoordinate(coord) actu.setName('actuator') actu.setOptimalForce(1) model.addComponent(actu) study = osim.MocoStudy() study.setName('sliding_mass') mp = study.updProblem() mp.setModel(model) ph0 = mp.getPhase() mp.setTimeBounds(osim.MocoInitialBounds(0.), osim.MocoFinalBounds(0.1, 5.)) assert ph0.getTimeInitialBounds().getLower() == 0 assert ph0.getTimeInitialBounds().getUpper() == 0 self.assertAlmostEqual(ph0.getTimeFinalBounds().getLower(), 0.1) self.assertAlmostEqual(ph0.getTimeFinalBounds().getUpper(), 5.0) mp.setTimeBounds([0.2, 0.3], [3.5]) self.assertAlmostEqual(ph0.getTimeInitialBounds().getLower(), 0.2) self.assertAlmostEqual(ph0.getTimeInitialBounds().getUpper(), 0.3) self.assertAlmostEqual(ph0.getTimeFinalBounds().getLower(), 3.5) self.assertAlmostEqual(ph0.getTimeFinalBounds().getUpper(), 3.5) # Use setter on MocoPhase. ph0.setTimeBounds([2.2, 2.3], [4.5]) self.assertAlmostEqual(ph0.getTimeInitialBounds().getLower(), 2.2) self.assertAlmostEqual(ph0.getTimeInitialBounds().getUpper(), 2.3) self.assertAlmostEqual(ph0.getTimeFinalBounds().getLower(), 4.5) self.assertAlmostEqual(ph0.getTimeFinalBounds().getUpper(), 4.5) mp.setStateInfo('slider/position/value', osim.MocoBounds(-5, 5), osim.MocoInitialBounds(0)) assert -5 == ph0.getStateInfo( 'slider/position/value').getBounds().getLower() assert 5 == ph0.getStateInfo( 'slider/position/value').getBounds().getUpper() assert isnan( ph0.getStateInfo( 'slider/position/value').getFinalBounds().getLower()) assert isnan( ph0.getStateInfo( 'slider/position/value').getFinalBounds().getUpper()) mp.setStateInfo('slider/position/speed', [-50, 50], [-3], 1.5) assert -50 == ph0.getStateInfo( 'slider/position/speed').getBounds().getLower() assert 50 == ph0.getStateInfo( 'slider/position/speed').getBounds().getUpper() assert -3 == ph0.getStateInfo( 'slider/position/speed').getInitialBounds().getLower() assert -3 == ph0.getStateInfo( 'slider/position/speed').getInitialBounds().getUpper() self.assertAlmostEqual( 1.5, ph0.getStateInfo( 'slider/position/speed').getFinalBounds().getLower()) self.assertAlmostEqual( 1.5, ph0.getStateInfo( 'slider/position/speed').getFinalBounds().getUpper()) # Use setter on MocoPhase. ph0.setStateInfo('slider/position/speed', [-6, 10], [-4, 3], [0]) assert -6 == ph0.getStateInfo( 'slider/position/speed').getBounds().getLower() assert 10 == ph0.getStateInfo( 'slider/position/speed').getBounds().getUpper() assert -4 == ph0.getStateInfo( 'slider/position/speed').getInitialBounds().getLower() assert 3 == ph0.getStateInfo( 'slider/position/speed').getInitialBounds().getUpper() assert 0 == ph0.getStateInfo( 'slider/position/speed').getFinalBounds().getLower() assert 0 == ph0.getStateInfo( 'slider/position/speed').getFinalBounds().getUpper() # Controls. mp.setControlInfo('actuator', osim.MocoBounds(-50, 50)) assert -50 == ph0.getControlInfo('actuator').getBounds().getLower() assert 50 == ph0.getControlInfo('actuator').getBounds().getUpper() mp.setControlInfo('actuator', [18]) assert 18 == ph0.getControlInfo('actuator').getBounds().getLower() assert 18 == ph0.getControlInfo('actuator').getBounds().getUpper()