Skip to content
This repository was archived by the owner on Dec 7, 2021. It is now read-only.

Add feasibility checks to optimizers/converters #1199

Merged
merged 45 commits into from
Sep 11, 2020

Conversation

KahanMajmudar
Copy link
Contributor

Summary

draft of #1134
Added is_feasible() function to check whether the returned result of the optimizers and to check whether they are feasible or not

Details and comments

Please check for the same and provide valuable feedback. Thank you 😃

@CLAassistant
Copy link

CLAassistant commented Aug 13, 2020

CLA assistant check
All committers have signed the CLA.

@KahanMajmudar KahanMajmudar changed the title Draft for issue#1134 [WIP] Draft for issue#1134 Aug 13, 2020
@t-imamichi
Copy link
Contributor

Please change the title to be more informative to denote what this PR adds and merge master to your branch. There are some conflicts.

@KahanMajmudar KahanMajmudar changed the title [WIP] Draft for issue#1134 [WIP] Draft for issue#1134. Adds a is_feasible() function to check for feasibility of a solution returned by the solve method Aug 13, 2020
@KahanMajmudar
Copy link
Contributor Author

Please change the title to be more informative to denote what this PR adds and merge master to your branch. There are some conflicts.

Done and Done. 😄

@KahanMajmudar
Copy link
Contributor Author

@t-imamichi I am confused by this PR. It already incorporates the changes of is_feasible(x) function in optimizers. So after this(draft added by me) PR addes the function, does your PR makes the necessary changes or do I have to work on adding the function to every optimzier by following your PR.

@adekusar-drl adekusar-drl linked an issue Aug 13, 2020 that may be closed by this pull request
@adekusar-drl adekusar-drl changed the title [WIP] Draft for issue#1134. Adds a is_feasible() function to check for feasibility of a solution returned by the solve method [WIP] Add feasibility checks to optimizers/converters Aug 13, 2020
Copy link
Contributor

@adekusar-drl adekusar-drl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping going! All good as an intermediate version!
Please think of adding unit tests. Also, pay attention at the pipeline output, stuff like pylint, code style, spelling, etc.

Comment on lines 1133 to 1138
if(detailed == True):

for i in range(len(satisfied_variables)):
(print(f'{self.get_variable(i)._name} is within the bounds') if satisfied_variables[i]
else logger.warning(f'{self.get_variable(i)._name} is outside the bounds'))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial idea was to return a list of violations, not just printing them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial idea was to return a list of violations, not just printing them.

This is for debug purposes only. I was not sure about how the return value was required. I will change it into a list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good question how to return them back. May be we should postpone this feature, I mean returning a list of violations and focus on the boolean result.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect to see a list of something, but what is float here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So currently I will print the violations and just return the bool result. One way would be to change the return type from ->bool to ->Tuple[bool, List[float]] and return empty list whenever detailed = False.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list can contain float values as well. Like x = [1.0, 2.0, 1.0, 1.99999982]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but what do they mean, these float values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry I didn't get your question. I would try to explain from what I understood.
So if we define a quadratic problem with variables x, y and z, then we get results.x = [1.0, 2.0, 1.0] would mean x = 1.0, y = 2.0 and z = 1.0.

@adekusar-drl
Copy link
Contributor

@t-imamichi I am confused by this PR. It already incorporates the changes of is_feasible(x) function in optimizers. So after this(draft added by me) PR addes the function, does your PR makes the necessary changes or do I have to work on adding the function to every optimzier by following your PR.

As I understood from the description of that PR, you don't have to follow that PR. Main purpose of #1196 is to change the interpret method in the converters. So it should may use of this PR.

@t-imamichi
Copy link
Contributor

Yes. You can ignore is_feasible(x) in #1196. It is a tentative implementation of feasibility check to develop my PoC. Your implementation will be more intuitive and efficient.

Comment on lines 143 to 145
fval=sol.get_objective_value(),
variables=problem.variables,
raw_results=sol)
raw_results=problem.variables,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is raw_results=problem.variables ? originally it was raw_results=sol.

Comment on lines 96 to 114
FAILURE = 1
"""the optimization algorithm ended in a failure."""
Raises:
None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no exception is raised, section "Raises" should be omitted.

Comment on lines 33 to 35
samples: List[Tuple[str, float, float]],
min_eigen_solver_result: Optional[MinimumEigensolverResult] = None) -> None:
min_eigen_solver_result: Optional[MinimumEigensolverResult] = None,
status: OptimizationResultStatus = OptimizationResultStatus.SUCCESS) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make status parameter required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think there is something like Required inside the typing provided by python
https://github.com/python/typing/blob/5d2f3b1992b8b66d41fe001df148693b45d30e31/src/typing.py#L23-L98

Copy link
Contributor

@adekusar-drl adekusar-drl Sep 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant, delete default assignment to status, this will cause the parameter to be required, you have to pass a value for this parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but I do need to define a default value for it. Should I go with None?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, no default value, that's the thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it throws an error: default value must be specified here Python(parser-16)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non default(required) parameters go first. So, move it up on the list of parameters, we still can do this.

Comment on lines 58 to 60
replacements: Dict[str, Tuple[str, int]],
history: Tuple[List[MinimumEigenOptimizationResult], OptimizationResult]) -> None:
history: Tuple[List[MinimumEigenOptimizationResult], OptimizationResult],
status: OptimizationResultStatus = OptimizationResultStatus.SUCCESS) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, status is required.

Comment on lines 37 to 39
fx: Optional[np.ndarray] = None, its: Optional[int] = None,
imode: Optional[int] = None, smode: Optional[str] = None) -> None:
imode: Optional[int] = None, smode: Optional[str] = None,
status: OptimizationResultStatus = OptimizationResultStatus.SUCCESS) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, please make status required.

Comment on lines 223 to 225
# check for feasibility
status = OptimizationResultStatus.SUCCESS if problem.is_feasible(result.x) \
else OptimizationResultStatus.INFEASIBLE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are not required any more

Comment on lines 1137 to 1142
# create a dict containing only unsatisfied variable(s)/constraint(s)
final_dict = {k: v for k, v in {**satisfied_variables, **satisfied_constraints}
.items() if not v}

# debug
logger.debug("Violations: %s", final_dict)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final_dict is created for debug purposes only. Please create it only when debug is enabled or remove this.

@adekusar-drl
Copy link
Contributor

@woodsp-ibm, @t-imamichi please take a look, should be fine now.

@t-imamichi t-imamichi added the Changelog: API Change Include in the Changed section of the changelog label Sep 10, 2020
@t-imamichi
Copy link
Contributor

Thank you! It looks good. We need just a reno text to explain the new APIs.

@adekusar-drl adekusar-drl merged commit 99394e5 into qiskit-community:master Sep 11, 2020
@KahanMajmudar
Copy link
Contributor Author

Thank you everyone 😄. I am glad that my first ever PR has been accepted and merged successfully. Thank you @adekusar-drl and @t-imamichi for giving me the opportunity to work on this 🎉.

@t-imamichi
Copy link
Contributor

Congrats! Enjoy Qiskit hacking.

pbark pushed a commit to pbark/qiskit-aqua that referenced this pull request Sep 16, 2020
* Draft for issue#1134

* resolved issue in quadratic_problem.py

* incorporated changes as suggested by @adekusar-drl

* fix linting issues

* added is_feasible method in all optimizers, added changes suggested by @ woodsp-ibm

* removed cast import

* made changes as per points 1 to 3

* made suggested changes by @adekusar-drl

* some cleaning

* added reno

* fixed reno

* Update releasenotes/notes/feasibility-check-b99605f771e745b7.yaml

Co-authored-by: Steve Wood <[email protected]>

* Update releasenotes/notes/feasibility-check-b99605f771e745b7.yaml

Co-authored-by: Steve Wood <[email protected]>

* code review

Co-authored-by: Manoel Marques <[email protected]>
Co-authored-by: Takashi Imamichi <[email protected]>
Co-authored-by: Anton Dekusar <[email protected]>
Co-authored-by: Anton Dekusar <[email protected]>
Co-authored-by: Steve Wood <[email protected]>
manoelmarques added a commit to qiskit-community/qiskit-optimization that referenced this pull request Jan 14, 2021
…kit-aqua#1199)

* Draft for issue#1134

* resolved issue in quadratic_problem.py

* incorporated changes as suggested by @adekusar-drl

* fix linting issues

* added is_feasible method in all optimizers, added changes suggested by @ woodsp-ibm

* removed cast import

* made changes as per points 1 to 3

* made suggested changes by @adekusar-drl

* some cleaning

* added reno

* fixed reno

* Update releasenotes/notes/feasibility-check-b99605f771e745b7.yaml

Co-authored-by: Steve Wood <[email protected]>

* Update releasenotes/notes/feasibility-check-b99605f771e745b7.yaml

Co-authored-by: Steve Wood <[email protected]>

* code review

Co-authored-by: Manoel Marques <[email protected]>
Co-authored-by: Takashi Imamichi <[email protected]>
Co-authored-by: Anton Dekusar <[email protected]>
Co-authored-by: Anton Dekusar <[email protected]>
Co-authored-by: Steve Wood <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Changelog: API Change Include in the Changed section of the changelog Optimization type: enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add feasibility checks to optimizers/converters
6 participants