-
-
Notifications
You must be signed in to change notification settings - Fork 480
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
Added method to find constraints value for MILP #38055
base: develop
Are you sure you want to change the base?
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.
In addition, please add a test for a single integer input.
What happens when an index is outside of the valid input range?
src/sage/numerical/mip.pyx
Outdated
sum = 0 | ||
cons = self.constraints(i)[1] | ||
for index, coeff in zip(cons[0], cons[1]): | ||
sum += var_val[index] * coeff | ||
return sum |
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.
sum = 0 | |
cons = self.constraints(i)[1] | |
for index, coeff in zip(cons[0], cons[1]): | |
sum += var_val[index] * coeff | |
return sum | |
s = 0 | |
cons = self.constraints(i)[1] | |
for index, coeff in zip(cons[0], cons[1]): | |
s += var_val[index] * coeff | |
return s |
Please avoid using builtin names like sum
.
src/sage/numerical/mip.pyx
Outdated
sum = 0 | ||
cons = self.constraints(i)[1] | ||
for index, coeff in zip(cons[0], cons[1]): | ||
sum += var_val[index] * coeff | ||
return sum |
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.
sum = 0 | |
cons = self.constraints(i)[1] | |
for index, coeff in zip(cons[0], cons[1]): | |
sum += var_val[index] * coeff | |
return sum | |
s = 0 | |
cons = self.constraints(i)[1] | |
for index, coeff in zip(cons[0], cons[1]): | |
s += var_val[index] * coeff | |
return s |
Please avoid using builtin names like sum
.
src/sage/numerical/mip.pyx
Outdated
var_val = self.get_values([n for n in var]) | ||
|
||
if indices is None: | ||
indices = [n for n in range(0, self.number_of_constraints())] |
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.
indices = [n for n in range(0, self.number_of_constraints())] | |
indices = [n for n in range(self.number_of_constraints())] |
The 0
is unneeded in Python.
src/sage/numerical/mip.pyx
Outdated
EXAMPLES: | ||
|
||
sage: p = MixedIntegerLinearProgram(maximization=True, solver='GLPK') |
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.
EXAMPLES: | |
sage: p = MixedIntegerLinearProgram(maximization=True, solver='GLPK') | |
EXAMPLES:: | |
sage: p = MixedIntegerLinearProgram(maximization=True, solver='GLPK') |
and indent the rest accordingly.
src/sage/numerical/mip.pyx
Outdated
[24.0, 6.0, -1.5, 1.5] | ||
|
||
""" |
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.
[24.0, 6.0, -1.5, 1.5] | |
""" | |
[24.0, 6.0, -1.5, 1.5] | |
""" |
src/sage/numerical/mip.pyx
Outdated
|
||
if isinstance(indices, (int, Integer)): | ||
result.append(calculate_value(self, indices)) | ||
return result | ||
elif isinstance(indices, list): | ||
for i in indices: | ||
result.append(calculate_value(self, i)) | ||
return result | ||
else: | ||
raise ValueError("constraints_values() requires a list of integers, though it will accommodate None or an integer.") |
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 isinstance(indices, (int, Integer)): | |
result.append(calculate_value(self, indices)) | |
return result | |
elif isinstance(indices, list): | |
for i in indices: | |
result.append(calculate_value(self, i)) | |
return result | |
else: | |
raise ValueError("constraints_values() requires a list of integers, though it will accommodate None or an integer.") | |
elif indices in ZZ: | |
indices = [ZZ(indices)] | |
return [calculate_value(self, i) for i in indices] |
src/sage/numerical/mip.pyx
Outdated
[24.0, 6.0, -1.5, 1.5] | ||
|
||
""" | ||
from sage.rings.integer import Integer |
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.
from sage.rings.integer import Integer | |
from sage.rings.integer_ring import ZZ |
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 think you were too greedy with a find-replace sum
-> result
. Please revert almost all of those.
src/sage/numerical/mip.pyx
Outdated
sage: p.constraints_values() | ||
[24.0, 6.0, -1.5, 1.5] | ||
|
||
TESTS:: |
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.
TESTS:: | |
TESTS: |
src/sage/numerical/mip.pyx
Outdated
ValueError: invalid row index 2 | ||
|
||
""" | ||
from sage.rings.integer_ring import ZZ |
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.
ValueError: invalid row index 2 | |
""" | |
from sage.rings.integer_ring import ZZ | |
ValueError: invalid row index 2 | |
""" | |
from sage.rings.integer_ring import ZZ |
src/sage/numerical/mip.pyx
Outdated
- ``indices`` -- select which constraint(s) values to return | ||
|
||
- If ``indices = None``, the method returns the list of all the | ||
constraints values. | ||
|
||
- If ``indices`` is an integer `i`, the method returns value of constraint | ||
`i`. | ||
|
||
- If ``indices`` is a list of integers, the method returns the list | ||
of the corresponding value of constraints. | ||
|
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.
- ``indices`` -- select which constraint(s) values to return | |
- If ``indices = None``, the method returns the list of all the | |
constraints values. | |
- If ``indices`` is an integer `i`, the method returns value of constraint | |
`i`. | |
- If ``indices`` is a list of integers, the method returns the list | |
of the corresponding value of constraints. | |
- ``indices`` -- select which constraint(s) values to return: | |
* ``None`` - return the list of all the constraints values | |
* an integer `i` - return the value of constraint `i` | |
* a list of integers - return the list of the | |
corresponding value of constraints | |
Documentation preview for this PR (built with commit b114d7a; changes) is ready! 🎉 |
Impelement new method to show the values of constraints of MILP according to the current variable's values. Based on #7300.
📝 Checklist