How to implement refining conflict in constraint programming - python

I use Docplex with python 3.7 to implement constraints programming. when it was infeasible, how can i proceed to list constraints those was to source of the conflict?
mdl.export_as_cpo(out="/home/..../MCP3.lp")
msol = mdl.solve(FailLimit=700000, TimeLimit=1600)
DInfos= msol.get_solver_infos()
mconflict=msol.CpoRefineConflictResult()
mconflict.get_all_member_constraints()
Error message:
mconflict=msol.CpoRefineConflictResult()
AttributeError: 'CpoSolveResult' object has no attribute 'CpoRefineConflictResult'

solve returns a SolveResult, and CpoRefineConflictResult is a class in docplex.cp.solution. So, the error message is correct: a SolveResult does not have an attribute CpoRefineConflictResult. You'd expect the CpoRefineConflictResult as the result of the conflict refiner.
You should probably read through the documentation a bit more http://ibmdecisionoptimization.github.io/docplex-doc/cp/docplex.cp.solution.py.html
You can call the .refine_conflict() method on the CpoSolver object to obtain a CpoRefineConflictResult, as documented here http://ibmdecisionoptimization.github.io/docplex-doc/cp/docplex.cp.solver.solver.py.html#detailed-description
Perhaps you can provide a minimal, reproducible example, if you need a more specific solution to your problem. https://stackoverflow.com/help/minimal-reproducible-example

I have add:
from docplex.cp.solver.solver import CpoSolver
After, i have add those lines if the model is infeasible:
mconfl= CpoSolver(model)
mconf = mconfl.refine_conflict()

Related

How to get pair to swap two AttributeError: 'Contract' object has no attribute 'methods' in Web3.py

I am using python. I am going to get a pair to swap two tokens. How to get it?
I am going to get the reverse value of two tokens. But I can't get this value, I am using method like this.
`
uniswap_pair = uniswap_factory.methods.getPair(tokenIn, tokenOut).call()
uniswap_reserves = uniswap_pair.methods.getReserve().call()
reserve0 = uniswap_reserves[0]
reserve1 = uniswap_reserves[1]`
But I have errors.
uniswap_pair = uniswap_factory.methods.getPair(tokenIn, tokenOut).call() AttributeError: 'Contract' object has no attribute 'methods'
Web3.py documentation how to call smart contracts is here. What you are writing does not make sense with Python and looks like JavaScript and Web3.js code.
I suggest you start with Web3.py tutorials and basics before trying to tackle more complex problems, because this knowledge is prerequisite for you to program anything useful.

Keeping alias types simple in Python documentation?

I'm trying to use the typing module to document my Python package, and I have a number of situations where several different types are allowable for a function parameter. For instance, you can either pass a number, an Envelope object (one of the classes in my package), or a list of numbers from which an Envelope is constructed, or a list of lists of numbers from which an envelope is constructed. So I make an alias type as follows:
NumberOrEnvelope = Union[Sequence[Real], Sequence[Sequence[Real]], Real, Envelope]
Then I write the function:
def example_function(parameter: NumberOrEnvelope):
...
And that looks great to me. However, when I create the documentation using Sphinx, I end up with this horrifically unreadable function signature:
example_function(parameter: Union[Sequence[numbers.Real], Sequence[Sequence[numbers.Real]], numbers.Real, expenvelope.envelope.Envelope])
Same thing also with the hints that pop up when I start to try to use the function in PyCharm.
Is there some way I can have it just leave it as "NumberOrEnvelope". Ideally that would also link in the documentation to a clarification of what "NumberOrEnvelope" is, though even if it didn't it would be way better than what's appearing now.
I had the same issue and used https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_type_aliases, introduced in version 3.3.
In your sphinx conf.py, insert this section. It does not seem to make much sense at the first sight, but does the trick:
autodoc_type_aliases = dict(NumberOrEnvelope='NumberOrEnvelope')
Warning: It only works in modules that start with from __future__ import annotation
Note: If there is a target in the documentation, type references even have a hyperlink to the definition. I have classes, documented elsewhere with autoclass, which are used as types of function parameters, and the docs show the nice names of the types with links.
Support for this appears to be in the works.
See Issue #6518.
That issue can be closed by the recent updates to Pull Request #8007 (under review).
If you want the fix ASAP, you can perhaps try using that build.
EDIT: This doesn't quite work, sadly.
Turns out after a little more searching, I found what I was looking for. Instead of:
NumberOrEnvelope = Union[Sequence[Real], Sequence[Sequence[Real]], Real, Envelope]
I found that you can create your own compound type that does the same thing:
NumberOrEnvelope = TypeVar("NumberOrEnvelope", Sequence[Real], Sequence[Sequence[Real]], Real, Envelope)
This displays in documentation as "NumberOrEnvelope", just as I wanted.

How to get description on Python Method?

I know that to get help for a function, we use help(func) or ?func, but what about a method? simply using help(method) or ?method won't do anything.
I tried using a preloaded object name before a method, it worked. But are there any other way?
I found this: https://www.programiz.com/python-programming/methods/built-in/help
Try these on Python shell.
help('random thing')
help('print')
help('def')
from math import *
help('math.pow')
Each one will show different results - but the last couple should help you understand how it works for what you are asking.
This format is inappropriate ?method and will produce an error. If you're having trouble understanding some Python object then help() will do.
help(func)
help(method)
help(Class.method)

How to get rid of delays on double_click_input() actions?

Is there a way I can get rid of delays on double_click_input() actions?
What I'm trying to do is double click the edit box and then type keys here. Maybe both of these actions have some delay, so the whole process performing looks very slow.
Code:
myApp = Desktop(backend='uia').window(title_re='myTitle_re')
myApp.window(auto_id='myAutoId').window(title='myTitle').double_click_input()
myApp.descendants(title='myTitle', control_type='Edit')[1].type_keys('myKeys')
And an additional question: I tried to use double_click() here, but it always throws an exception:
AttributeError: WindowSpecification class has no 'double_click'
method.
Then I tried myApp.window(auto_id='myAutoId').window(title='myTitle').wrapper_object().double_click()
And got:
AttributeError: 'ListItemWrapper' object has no attribute
'double_click'
What should I change to get this work?
I'm using pywinauto 0.6.3.
Answering your first question, you can set some timings to null using global settings. For double_click_input:
from pywinauto.timings import Timings
Timings.after_clickinput_wait = 0.0
Timings.after_setcursorpos_wait = 0.0
For real user input (*_input methods) changing timings may cause modified sequence not to work. But you may experiment for your own risk. Sometimes it's better to use silent methods using window messages like WM_CLICK (for "win32" backend) or UIAutomation Patterns like Invoke Pattern (for "uia" backend).
double_click is not implemented for "uia" because it's unclear which UIAutomation Pattern should be interpreted as double click action. We have method .invoke() and ButtonWrapper.click = invoke alias. But for non-buttons InvokePattern may have different meaning. That's why we left it as .invoke().
P.S. Regarding legacy propery text... It can be obtained by .legacy_properties()[u'Value'] for your case (or other value from returned dict). There are methods set_window_text/set_edit_text using ValuePattern so the text can be set silently without any tricks.

Python "property object has no attribute" Exception

confirmation = property(_get_confirmation, _set_confirmation)
confirmation.short_description = "Confirmation"
When I try the above I get an Exception I don't quite understand:
AttributeError: 'property' object has no attribute 'short_description'
This was an answer to another question on here but I couldn't comment on it as I don't have enough points or something. :-(
In other tests I've also got this error under similar circumstances:
TypeError: 'property' object has only read-only attributes (assign to .short_description)
Any ideas anybody?
The result of property() is an object where you can't add new fields or methods. It's immutable which is why you get the error.
One way to achieve what you want is with using four arguments to property():
confirmation = property(_get_confirmation, _set_confirmation, None, "Confirmation.")
or put the explanation into the docstring of _get_confirmation.
(see docs here, also supported in Python 2)
[EDIT] As for the answer, you refer to: I think the indentation of the example was completely wrong when you looked at it. This has been fixed, now.

Categories