Skip to content
Draft
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion bioptim/limits/path_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ def evaluate_at(self, shooting_point: Int, repeat: Int = 1):
return self[:, shooting_point]
elif self.type == InterpolationType.SPLINE:
spline = interp1d(self.t, self)
return spline(shooting_point / self.n_shooting * (self.t[-1] - self.t[0]))
time = self.t[0] + shooting_point / (self.n_shooting * repeat) * (self.t[-1] - self.t[0])
return spline(time)
elif self.type == InterpolationType.CUSTOM:
if self.slice_list is not None:
slice_list = self.slice_list
Expand Down
2 changes: 1 addition & 1 deletion bioptim/optimization/vector_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _compute_value_for_node(
real_keys = [key for key in defined_values.keys() if key != "None"]
for key in real_keys:

if defined_values[key].type == InterpolationType.ALL_POINTS:
if defined_values[key].type in (InterpolationType.ALL_POINTS, InterpolationType.SPLINE):
point = node * repeat + sub_node
else:
point = _get_interpolation_point(node, sub_node)
Expand Down
20 changes: 20 additions & 0 deletions tests/shard5/test_initial_condition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from types import SimpleNamespace

import numpy as np
import numpy.testing as npt
Expand All @@ -19,6 +20,7 @@
SolutionMerge,
)
from bioptim.limits.path_conditions import InitialGuess
from bioptim.optimization.vector_utils import _compute_value_for_node
from ..utils import TestUtils

# TODO: Add negative test for sizes
Expand Down Expand Up @@ -137,6 +139,24 @@ def test_initial_guess_spline():
npt.assert_almost_equal(init.init.evaluate_at(t), expected_val)


def test_initial_guess_spline_at_collocation_points():
class VariableContainer(dict):
shape = 1

initial_guesses = InitialGuessList()
initial_guesses.add("x", [[0.0, 6.0]], t=[2.0, 8.0], interpolation=InterpolationType.SPLINE)
initial_guesses["x"].check_and_adjust_dimensions(1, 2)
variables = VariableContainer(x=SimpleNamespace(index=[0]))
scaling = {"x": SimpleNamespace(scaling=np.ones((1, 1)))}

values = [
_compute_value_for_node(node, sub_node, 0, 3, variables, initial_guesses, scaling).item()
for node in range(2)
for sub_node in range(3)
]
npt.assert_allclose(values, [0, 1, 2, 3, 4, 5])


@pytest.mark.parametrize("phase_dynamics", [PhaseDynamics.SHARED_DURING_THE_PHASE, PhaseDynamics.ONE_PER_NODE])
def test_initial_guess_update(phase_dynamics):
# Load pendulum
Expand Down
Loading