-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made the most minimal version of the non-working l4casadi example
- Loading branch information
Showing
2 changed files
with
40 additions
and
65 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
bioptim/examples/getting_started/temporary_non_working_example.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from casadi import Opti, MX, Function | ||
import l4casadi as l4c | ||
import torch | ||
|
||
|
||
class NeuralNetworkModel(torch.nn.Module): | ||
def __init__(self, layer_node_count: tuple[int]): | ||
super(NeuralNetworkModel, self).__init__() | ||
layers = torch.nn.ModuleList() | ||
layers.append(torch.nn.Linear(layer_node_count[0], layer_node_count[-1])) | ||
self._forward_model = torch.nn.Sequential(*layers) | ||
self.eval() | ||
|
||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
return self._forward_model(x) | ||
|
||
|
||
def main(): | ||
opti = Opti() | ||
nx = nu = 1 | ||
torch_model = NeuralNetworkModel(layer_node_count=(nu, nx)) | ||
|
||
# ---- decision variables --------- | ||
x = opti.variable(nx, 1) # state | ||
u = opti.variable(nu, 1) # control | ||
|
||
# ---- dynamic constraints -------- | ||
x_sym = MX.sym("x", nx, 1) | ||
u_sym = MX.sym("u", nu, 1) | ||
forward_model = l4c.L4CasADi(torch_model, device="cpu") | ||
f = Function("xdot", [x_sym, u_sym], [x_sym - forward_model(u_sym)]) | ||
opti.subject_to(f(x, u) == 0) # Adding this line yields the error : jac_adj_i0_adj_o0 is not provided by L4CasADi. | ||
|
||
# ---- solve NLP ------ | ||
opti.solver("ipopt") | ||
opti.solve() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |