cmower / optas

OpTaS: An optimization-based task specification library for trajectory optimization and model predictive control.
https://cmower.github.io/optas/
Other
101 stars 14 forks source link

Suggested feature: solve() should not return dictionary, rather it should return an object called Solution #76

Open cmower opened 1 year ago

cmower commented 1 year ago

Currently, the solve() method returns a dictionary containing the solution. I'm proposing here that we should implement a solution class that has the solution data packed inside but also comes with some useful methods.

For example, it could look something like the following.

import casadi as cs
from dataclasses import dataclass
from typing import Dict, List
from .models import Model

@dataclass
class Solution:
    solution_dict: Dict[str, cs.DM]
    success: bool

    def get_model_solution_state(self, name, t):
         return self.solution_dict[f{name}/q'][:, t]

    def interpolate(self, name, duration):
        pass  # todo

This would be a little re-work but I think it would help optas become more extensible. I think this would be worthwhile. @joaomoura24, if this means a lot of hassle you're end. We could still incorporate the feature but put a flag in the Solver class interface. E.g. we put a flag saying solution_return_type='dict' by default (and the return type is what it is currently), and if the user wants to get the solution as the Solution class (something like above) perhaps they put solution_return_type='soln_cls' or something. I prefer not to do this but happy to if it means a lot of work your end.

I've not been a fan of having to do solution[f'{self.name}/q'] all the time, and also solver.interpolate doesn't make so much sense so would be good to put that functionality in a place that makes more sense.

joaomoura24 commented 1 year ago

Yeah, i agree. This month i am already packed so i would propose that we start a branch in March to add this.

If this has time and you are happy with it, I can start the branch directly in the repo (not sure about permission).

Additionally, I would suggest also to include the parameters used to solve the problem in the returned solution class, and the respective methods to access them by name. (whether they are model parameters or parameters defined by the user)

cmower commented 1 year ago

Cool, will keep in mind.

I can do the main implementation. Then you can check out once it is done when ever you have time.