Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore example simulation in getting started #823

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions bioptim/examples/getting_started/example_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
can be held in the solution.
"""

from bioptim import InitialGuess, Solution, Shooting, InterpolationType, SolutionIntegrator
import numpy as np

import pendulum
from bioptim import InitialGuessList, Solution, Shooting, InterpolationType, SolutionIntegrator


def main():
Expand All @@ -24,22 +25,31 @@ def main():

# Simulation the Initial Guess
# Interpolation: Constant
X = InitialGuess([0, 0, 0, 0])
U = InitialGuess([-1, 1])
X = InitialGuessList()
X["q"] = [0] * 2
X["qdot"] = [0] * 2

U = InitialGuessList()
U["tau"] = [-1, 1]

sol_from_initial_guess = Solution.from_initial_guess(ocp, [X, U])
sol_from_initial_guess = Solution.from_initial_guess(
ocp, [np.array([1 / 30]), X, U, InitialGuessList(), InitialGuessList()]
)
s = sol_from_initial_guess.integrate(shooting_type=Shooting.SINGLE, integrator=SolutionIntegrator.OCP)
print(f"Final position of q from single shooting of initial guess = {s.states['q'][:, -1]}")
print(f"Final position of q from single shooting of initial guess = {s['q'][-1]}")
# Uncomment the next line to animate the integration
# s.animate()

# Interpolation: Each frame (for instance, values from a previous optimization or from measured data)
U = np.random.rand(2, 31)
U = InitialGuess(U, interpolation=InterpolationType.EACH_FRAME)
random_u = np.random.rand(2, 30)
U = InitialGuessList()
U.add("tau", random_u, interpolation=InterpolationType.EACH_FRAME)

sol_from_initial_guess = Solution.from_initial_guess(ocp, [X, U])
sol_from_initial_guess = Solution.from_initial_guess(
ocp, [np.array([1 / 30]), X, U, InitialGuessList(), InitialGuessList()]
)
s = sol_from_initial_guess.integrate(shooting_type=Shooting.SINGLE, integrator=SolutionIntegrator.OCP)
print(f"Final position of q from single shooting of initial guess = {s.states['q'][:, -1]}")
print(f"Final position of q from single shooting of initial guess = {s['q'][-1]}")
# Uncomment the next line to animate the integration
# s.animate()

Expand All @@ -53,16 +63,16 @@ def main():
s_single = sol.integrate(shooting_type=Shooting.SINGLE, integrator=SolutionIntegrator.OCP)
# Uncomment the next line to animate the integration
# s_single.animate()
print(f"Final position of q from single shooting of the solution = {s_single.states['q'][:, -1]}")
s_multiple = sol.integrate(
shooting_type=Shooting.MULTIPLE, keep_intermediate_points=True, integrator=SolutionIntegrator.OCP
)
print(f"Final position of q from multiple shooting of the solution = {s_multiple.states['q'][:, -1]}")
print(f"Final position of q from single shooting of the solution = {s_single['q'][-1]}")
s_multiple = sol.integrate(shooting_type=Shooting.MULTIPLE, integrator=SolutionIntegrator.OCP)
print(f"Final position of q from multiple shooting of the solution = {s_multiple['q'][-1]}")

# Uncomment the following lines to graph the solution from the actual solution
# sol.graphs(shooting_type=Shooting.SINGLE)
# sol.graphs(shooting_type=Shooting.MULTIPLE)

return s, s_single, s_multiple


if __name__ == "__main__":
main()
Loading