Skip to content

Commit

Permalink
Merge pull request #294 from Amedeo123/master
Browse files Browse the repository at this point in the history
Fix from initial guess for each frame interpolation + add in example
  • Loading branch information
pariterre authored Feb 24, 2021
2 parents 60b890b + 52e1489 commit 3cd45ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
5 changes: 3 additions & 2 deletions bioptim/optimization/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from matplotlib import pyplot as plt

from ..limits.path_conditions import InitialGuess, InitialGuessList
from ..misc.enums import ControlType, CostType, Shooting
from ..misc.enums import ControlType, CostType, Shooting, InterpolationType
from ..misc.utils import check_version
from ..limits.phase_transition import PhaseTransitionFunctions
from ..optimization.non_linear_program import NonLinearProgram
Expand Down Expand Up @@ -262,7 +262,8 @@ def init_from_initial_guess(sol: list):
self.vector = np.ndarray((0, 1))
sol_states, sol_controls = sol[0], sol[1]
for p, s in enumerate(sol_states):
s.init.check_and_adjust_dimensions(self.ocp.nlp[p].nx, self.ocp.nlp[p].ns + 1, "states")
ns = self.ocp.nlp[p].ns + 1 if s.init.type != InterpolationType.EACH_FRAME else self.ocp.nlp[p].ns
s.init.check_and_adjust_dimensions(self.ocp.nlp[p].nx, ns, "states")
for i in range(self.ns[p] + 1):
self.vector = np.concatenate((self.vector, s.init.evaluate_at(i)[:, np.newaxis]))
for p, s in enumerate(sol_controls):
Expand Down
20 changes: 16 additions & 4 deletions examples/getting_started/example_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
can be held in the solution. Another goal would be to reload fast a previously saved optimized solution
"""

from bioptim import InitialGuess, Solution, Shooting

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


Expand All @@ -23,6 +23,7 @@
)

# Simulation the Initial Guess
# Interpolation: Constant
X = InitialGuess([0, 0, 0, 0])
U = InitialGuess([-1, 1])

Expand All @@ -32,17 +33,28 @@
# 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, 11)
U = InitialGuess(U, interpolation=InterpolationType.EACH_FRAME)

sol_from_initial_guess = Solution(ocp, [X, U])
s = sol_from_initial_guess.integrate(shooting_type=Shooting.SINGLE_CONTINUOUS)
print(f"Final position of q from single shooting of initial guess = {s.states['q'][:, -1]}")
# Uncomment the next line to animate the integration
# s.animate()

# Uncomment the following lines to graph the solution from initial guesses
# sol_from_initial_guess.graphs(shooting_type=Shooting.SINGLE_CONTINUOUS)
# sol_from_initial_guess.graphs(shooting_type=Shooting.MULTIPLE)

# Simulation of the solution. It is not the graph of the solution, it is the graph of a Runge Kutta from the solution
# Simulation of the solution. It is not the graph of the solution,
# it is the graph of a Runge Kutta from the solution
sol = ocp.solve()
s_single = sol.integrate(shooting_type=Shooting.SINGLE_CONTINUOUS)
# 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)
s_multiple = sol.integrate(shooting_type=Shooting.MULTIPLE, keepdims=False)
print(f"Final position of q from multiple shooting of the solution = {s_multiple.states['q'][:, -1]}")

# Uncomment the following lines to graph the solution from the actual solution
Expand Down

0 comments on commit 3cd45ba

Please sign in to comment.