Skip to content
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

pulp.lpDot update #572

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pulp/pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2293,5 +2293,9 @@ def lpDot(v1, v2):
return lpDot([v1] * len(v2), v2)
elif not const.isiterable(v2):
return lpDot(v1, [v2] * len(v1))
elif isinstance(v1, LpAffineExpression):
return lpSum([v1[e1] * lpDot(e1, e2) for e1, e2 in zip(v1, v2)])
elif isinstance(v2, LpAffineExpression):
return lpSum([v2[e2] * lpDot(e1, e2) for e1, e2 in zip(v1, v2)])
else:
return lpSum([lpDot(e1, e2) for e1, e2 in zip(v1, v2)])
15 changes: 14 additions & 1 deletion pulp/tests/test_pulp.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pulp.constants import PulpError
from pulp.apis import *
from pulp import LpVariable, LpProblem, lpSum, LpConstraintVar, LpFractionConstraint
from pulp import LpVariable, LpProblem, lpSum, lpDot, LpConstraintVar, LpFractionConstraint
from pulp import constants as const
from pulp.tests.bin_packing_problem import create_bin_packing_problem
from pulp.utilities import makeDict
Expand Down Expand Up @@ -75,6 +75,19 @@ def test_pulp_001(self):
print("\t Testing zero subtraction")
assert str(c2) # will raise an exception

def test_pulp_002(self):
"""
Test the lpDot operation
"""
x = LpVariable("x")
y = LpVariable("y")
z = LpVariable("z")
a = [1, 2, 3]
assert dict(lpDot([x, y, z], a)) == {x: 1, y: 2, z: 3}
assert dict(lpDot([2*x, 2*y, 2*z], a)) == {x: 2, y: 4, z: 6}
assert dict(lpDot([x+y, y+z, z], a)) == {x: 1, y: 3, z: 5}
assert dict(lpDot(a, [x+y, y+z, z])) == {x: 1, y: 3, z: 5}

def test_pulp_009(self):
# infeasible
prob = LpProblem("test09", const.LpMinimize)
Expand Down