-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add structure to solve optimization problems #4
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, some suggestions on the API but non blocking
Another suggestion would be to add named arguments to have a scaling when passing a costraint and cast it into a cost. |
Done in 109859c |
Friendly ping @traversaro @diegoferigo |
Great, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice @S-Dafarra 🚀 I like the direction. I left (too many) comments on this PR, feel free to include here what you think useful and -hopefully- address the nits/suggestions/python syntax enhancements in next ones. Approving regardless so you can move forward.
if isinstance(input_structure, OptimizationObject): | ||
return self._generate_objects_from_instance(input_structure=input_structure) | ||
return self._generate_objects_from_list(input_structure=input_structure) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit
if isinstance(input_structure, OptimizationObject): | |
return self._generate_objects_from_instance(input_structure=input_structure) | |
return self._generate_objects_from_list(input_structure=input_structure) | |
return ( | |
self._generate_objects_from_instance if isinstance(input_structure, OptimizationObject) | |
else self._generate_objects_from_instance | |
)(input_structure=input_structure) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried, but it does not work because it returns a function, not an object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first () return a function selecting one among the two options, the second () passes to it the argument, this should work fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I apply the suggestion, this test fails with the error. Any suggestion?
test/test_optimization_problem.py:129 (test_opti_solver_with_parameters_and_lists)
class_or_instance = [MyTestVarAndPar(composite=MyTestVar(variable=array([0., 0., 0.])), parameter=array([0., 0., 0.])), MyTestVarAndPar(co...rray([0., 0., 0.])), MyTestVarAndPar(composite=MyTestVar(variable=array([0., 0., 0.])), parameter=array([0., 0., 0.]))]
def fields(class_or_instance):
"""Return a tuple describing the fields of this dataclass.
Accepts a dataclass or an instance of one. Tuple elements are of
type Field.
"""
# Might it be worth caching this, per class?
try:
> fields = getattr(class_or_instance, _FIELDS)
E AttributeError: 'list' object has no attribute '__dataclass_fields__'
../../mambaforge/envs/hippopt/lib/python3.10/dataclasses.py:1196: AttributeError
During handling of the above exception, another exception occurred:
def test_opti_solver_with_parameters_and_lists():
problem = OptimizationProblem()
initial_guess = []
for _ in range(3):
initial_guess.append(MyTestVarAndPar())
> var = problem.generate_optimization_objects(input_structure=initial_guess)
test_optimization_problem.py:136:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../src/hippopt/base/optimization_problem.py:37: in generate_optimization_objects
return self._solver.generate_optimization_objects(
../src/hippopt/base/opti_solver.py:346: in generate_optimization_objects
return (
../src/hippopt/base/opti_solver.py:67: in _generate_objects_from_instance
for field in dataclasses.fields(output):
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
class_or_instance = [MyTestVarAndPar(composite=MyTestVar(variable=array([0., 0., 0.])), parameter=array([0., 0., 0.])), MyTestVarAndPar(co...rray([0., 0., 0.])), MyTestVarAndPar(composite=MyTestVar(variable=array([0., 0., 0.])), parameter=array([0., 0., 0.]))]
def fields(class_or_instance):
"""Return a tuple describing the fields of this dataclass.
Accepts a dataclass or an instance of one. Tuple elements are of
type Field.
"""
# Might it be worth caching this, per class?
try:
fields = getattr(class_or_instance, _FIELDS)
except AttributeError:
> raise TypeError('must be called with a dataclass type or instance')
E TypeError: must be called with a dataclass type or instance
../../mambaforge/envs/hippopt/lib/python3.10/dataclasses.py:1198: TypeError
Co-authored-by: Diego Ferigo <[email protected]>
aee7685
to
fd41fc5
Compare
Avoid to use None for the output solution and cost.
@diegoferigo I should have addressed your comments (except #4 (comment) where I am not sure how to continue). I set the comments to resolved quite arbitrarily, so feel free to "Unresolve" the points that need some further discussion. Let me know how you would like to proceed 😉 |
I will proceed in merging this. Other open points will be addressed in future PRs. |
An optimization problem can be set as follows
Note the line
Equalities can be cast into a cost. This might simplify the setting of tracking tasks.