How to solve a facility location allocation (IP) problem in CVXPY










1















I am learning to solve optimization problems using CVXPY, so I started with the following simple facility location allocation problem.



enter image description here



The Code in CVXPY is given as:



Fi = np.array([1,1,1]) # Fixed cost of each facility
Ci = np.array([15, 10, 10]) # Capacity of each facility
Dj = np.array([5, 5, 5, 3, 3, 4]) # Demand of each facility
Cij = np.ones(m,n)


n = len(Dj)
m = len(Fi)


# Decision Variables
Xij = cvx.Bool(m,n) # (m,n) vector
Yi = cvx.Bool(m) # column vector of length (m,1)

# Objective
fixed_cost = cvx.sum_entries(Fi*Yi)
var_cost = cvx.sum_entries(Cij.T * Dj *Xij)
total_cost = fixed_cost + var_cost
objective = cvx.Minimize(total_cost)

# Maximum facility locations to be selected?
constraints.append(cvx.sum_entries(Yi)==2)

# Sum of demands allocated to a facility shall be <= facility capacity -
# Capacity Fixed Cost
constraints.append(cvx.sum_entries(Dj * Xij.T, axis=0) <= Ci*Yi)

# Every demand point shall be supplied by only one facility.
constraints.append(cvx.sum_entries(Xij, axis=1) == 1)


# Solve the problem
prob = cvx.Problem(objective, constraints)
prob.solve(solver=cvx.GLPK_MI)

# Print the values
#print("status:", prob.status)
print("optimal value", prob.value)
print("Selected Facility Locations", Yi.value)
print("Assigned Nodes", Xij.value, )


As per the last constraint, a demand location should be supplied by only one facility, however the output of Xij.value shows wrong results.



Using CVXPY version: 0.4.10
status: optimal
optimal value 91.0
Selected Facility Locations [[1.]
[0.]
[1.]]
Assigned Nodes to Facility 1) [[1. 0. 0. 0. 0. 0.]]
Assigned Nodes to Facility 2) [[1. 0. 0. 0. 0. 0.]]
Assigned Nodes to Facility 3) [[1. 0. 0. 0. 0. 0.]]


The Xij.value should be something like this:



Using CVXPY version: 0.4.10
status: optimal
optimal value 91.0
Selected Facility Locations [[1.]
[1.]
[0.]]
Assigned Nodes to Facility 1) [[1. 1. 1. 0. 0. 0.]]
Assigned Nodes to Facility 2) [[0. 0. 0. 1. 1. 1.]]
Assigned Nodes to Facility 3) [[0. 0. 0. 0. 0. 0.]]


Which means, facility 1 and 2 are selected.
The first three points are allocated to facility 1 and the next three to facility 2.










share|improve this question
























  • X == 1 is telling it that all values are 1

    – juvian
    Nov 16 '18 at 16:14











  • No, even when I remove the constraint, the X.value is still all one matrix.

    – Muhammad Asif Khan
    Nov 16 '18 at 17:56











  • Your code doesn't work, for instance m,n are defined after they are first used. If you can provide reproducible code compatiible with cvxpy 1.0 then I'm happy to help.

    – Michal Adamaszek
    Nov 30 '18 at 13:45















1















I am learning to solve optimization problems using CVXPY, so I started with the following simple facility location allocation problem.



enter image description here



The Code in CVXPY is given as:



Fi = np.array([1,1,1]) # Fixed cost of each facility
Ci = np.array([15, 10, 10]) # Capacity of each facility
Dj = np.array([5, 5, 5, 3, 3, 4]) # Demand of each facility
Cij = np.ones(m,n)


n = len(Dj)
m = len(Fi)


# Decision Variables
Xij = cvx.Bool(m,n) # (m,n) vector
Yi = cvx.Bool(m) # column vector of length (m,1)

# Objective
fixed_cost = cvx.sum_entries(Fi*Yi)
var_cost = cvx.sum_entries(Cij.T * Dj *Xij)
total_cost = fixed_cost + var_cost
objective = cvx.Minimize(total_cost)

# Maximum facility locations to be selected?
constraints.append(cvx.sum_entries(Yi)==2)

# Sum of demands allocated to a facility shall be <= facility capacity -
# Capacity Fixed Cost
constraints.append(cvx.sum_entries(Dj * Xij.T, axis=0) <= Ci*Yi)

# Every demand point shall be supplied by only one facility.
constraints.append(cvx.sum_entries(Xij, axis=1) == 1)


# Solve the problem
prob = cvx.Problem(objective, constraints)
prob.solve(solver=cvx.GLPK_MI)

# Print the values
#print("status:", prob.status)
print("optimal value", prob.value)
print("Selected Facility Locations", Yi.value)
print("Assigned Nodes", Xij.value, )


As per the last constraint, a demand location should be supplied by only one facility, however the output of Xij.value shows wrong results.



Using CVXPY version: 0.4.10
status: optimal
optimal value 91.0
Selected Facility Locations [[1.]
[0.]
[1.]]
Assigned Nodes to Facility 1) [[1. 0. 0. 0. 0. 0.]]
Assigned Nodes to Facility 2) [[1. 0. 0. 0. 0. 0.]]
Assigned Nodes to Facility 3) [[1. 0. 0. 0. 0. 0.]]


The Xij.value should be something like this:



Using CVXPY version: 0.4.10
status: optimal
optimal value 91.0
Selected Facility Locations [[1.]
[1.]
[0.]]
Assigned Nodes to Facility 1) [[1. 1. 1. 0. 0. 0.]]
Assigned Nodes to Facility 2) [[0. 0. 0. 1. 1. 1.]]
Assigned Nodes to Facility 3) [[0. 0. 0. 0. 0. 0.]]


Which means, facility 1 and 2 are selected.
The first three points are allocated to facility 1 and the next three to facility 2.










share|improve this question
























  • X == 1 is telling it that all values are 1

    – juvian
    Nov 16 '18 at 16:14











  • No, even when I remove the constraint, the X.value is still all one matrix.

    – Muhammad Asif Khan
    Nov 16 '18 at 17:56











  • Your code doesn't work, for instance m,n are defined after they are first used. If you can provide reproducible code compatiible with cvxpy 1.0 then I'm happy to help.

    – Michal Adamaszek
    Nov 30 '18 at 13:45













1












1








1








I am learning to solve optimization problems using CVXPY, so I started with the following simple facility location allocation problem.



enter image description here



The Code in CVXPY is given as:



Fi = np.array([1,1,1]) # Fixed cost of each facility
Ci = np.array([15, 10, 10]) # Capacity of each facility
Dj = np.array([5, 5, 5, 3, 3, 4]) # Demand of each facility
Cij = np.ones(m,n)


n = len(Dj)
m = len(Fi)


# Decision Variables
Xij = cvx.Bool(m,n) # (m,n) vector
Yi = cvx.Bool(m) # column vector of length (m,1)

# Objective
fixed_cost = cvx.sum_entries(Fi*Yi)
var_cost = cvx.sum_entries(Cij.T * Dj *Xij)
total_cost = fixed_cost + var_cost
objective = cvx.Minimize(total_cost)

# Maximum facility locations to be selected?
constraints.append(cvx.sum_entries(Yi)==2)

# Sum of demands allocated to a facility shall be <= facility capacity -
# Capacity Fixed Cost
constraints.append(cvx.sum_entries(Dj * Xij.T, axis=0) <= Ci*Yi)

# Every demand point shall be supplied by only one facility.
constraints.append(cvx.sum_entries(Xij, axis=1) == 1)


# Solve the problem
prob = cvx.Problem(objective, constraints)
prob.solve(solver=cvx.GLPK_MI)

# Print the values
#print("status:", prob.status)
print("optimal value", prob.value)
print("Selected Facility Locations", Yi.value)
print("Assigned Nodes", Xij.value, )


As per the last constraint, a demand location should be supplied by only one facility, however the output of Xij.value shows wrong results.



Using CVXPY version: 0.4.10
status: optimal
optimal value 91.0
Selected Facility Locations [[1.]
[0.]
[1.]]
Assigned Nodes to Facility 1) [[1. 0. 0. 0. 0. 0.]]
Assigned Nodes to Facility 2) [[1. 0. 0. 0. 0. 0.]]
Assigned Nodes to Facility 3) [[1. 0. 0. 0. 0. 0.]]


The Xij.value should be something like this:



Using CVXPY version: 0.4.10
status: optimal
optimal value 91.0
Selected Facility Locations [[1.]
[1.]
[0.]]
Assigned Nodes to Facility 1) [[1. 1. 1. 0. 0. 0.]]
Assigned Nodes to Facility 2) [[0. 0. 0. 1. 1. 1.]]
Assigned Nodes to Facility 3) [[0. 0. 0. 0. 0. 0.]]


Which means, facility 1 and 2 are selected.
The first three points are allocated to facility 1 and the next three to facility 2.










share|improve this question
















I am learning to solve optimization problems using CVXPY, so I started with the following simple facility location allocation problem.



enter image description here



The Code in CVXPY is given as:



Fi = np.array([1,1,1]) # Fixed cost of each facility
Ci = np.array([15, 10, 10]) # Capacity of each facility
Dj = np.array([5, 5, 5, 3, 3, 4]) # Demand of each facility
Cij = np.ones(m,n)


n = len(Dj)
m = len(Fi)


# Decision Variables
Xij = cvx.Bool(m,n) # (m,n) vector
Yi = cvx.Bool(m) # column vector of length (m,1)

# Objective
fixed_cost = cvx.sum_entries(Fi*Yi)
var_cost = cvx.sum_entries(Cij.T * Dj *Xij)
total_cost = fixed_cost + var_cost
objective = cvx.Minimize(total_cost)

# Maximum facility locations to be selected?
constraints.append(cvx.sum_entries(Yi)==2)

# Sum of demands allocated to a facility shall be <= facility capacity -
# Capacity Fixed Cost
constraints.append(cvx.sum_entries(Dj * Xij.T, axis=0) <= Ci*Yi)

# Every demand point shall be supplied by only one facility.
constraints.append(cvx.sum_entries(Xij, axis=1) == 1)


# Solve the problem
prob = cvx.Problem(objective, constraints)
prob.solve(solver=cvx.GLPK_MI)

# Print the values
#print("status:", prob.status)
print("optimal value", prob.value)
print("Selected Facility Locations", Yi.value)
print("Assigned Nodes", Xij.value, )


As per the last constraint, a demand location should be supplied by only one facility, however the output of Xij.value shows wrong results.



Using CVXPY version: 0.4.10
status: optimal
optimal value 91.0
Selected Facility Locations [[1.]
[0.]
[1.]]
Assigned Nodes to Facility 1) [[1. 0. 0. 0. 0. 0.]]
Assigned Nodes to Facility 2) [[1. 0. 0. 0. 0. 0.]]
Assigned Nodes to Facility 3) [[1. 0. 0. 0. 0. 0.]]


The Xij.value should be something like this:



Using CVXPY version: 0.4.10
status: optimal
optimal value 91.0
Selected Facility Locations [[1.]
[1.]
[0.]]
Assigned Nodes to Facility 1) [[1. 1. 1. 0. 0. 0.]]
Assigned Nodes to Facility 2) [[0. 0. 0. 1. 1. 1.]]
Assigned Nodes to Facility 3) [[0. 0. 0. 0. 0. 0.]]


Which means, facility 1 and 2 are selected.
The first three points are allocated to facility 1 and the next three to facility 2.







optimization integer-programming cvxpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 10:09







Muhammad Asif Khan

















asked Nov 16 '18 at 7:28









Muhammad Asif KhanMuhammad Asif Khan

7918




7918












  • X == 1 is telling it that all values are 1

    – juvian
    Nov 16 '18 at 16:14











  • No, even when I remove the constraint, the X.value is still all one matrix.

    – Muhammad Asif Khan
    Nov 16 '18 at 17:56











  • Your code doesn't work, for instance m,n are defined after they are first used. If you can provide reproducible code compatiible with cvxpy 1.0 then I'm happy to help.

    – Michal Adamaszek
    Nov 30 '18 at 13:45

















  • X == 1 is telling it that all values are 1

    – juvian
    Nov 16 '18 at 16:14











  • No, even when I remove the constraint, the X.value is still all one matrix.

    – Muhammad Asif Khan
    Nov 16 '18 at 17:56











  • Your code doesn't work, for instance m,n are defined after they are first used. If you can provide reproducible code compatiible with cvxpy 1.0 then I'm happy to help.

    – Michal Adamaszek
    Nov 30 '18 at 13:45
















X == 1 is telling it that all values are 1

– juvian
Nov 16 '18 at 16:14





X == 1 is telling it that all values are 1

– juvian
Nov 16 '18 at 16:14













No, even when I remove the constraint, the X.value is still all one matrix.

– Muhammad Asif Khan
Nov 16 '18 at 17:56





No, even when I remove the constraint, the X.value is still all one matrix.

– Muhammad Asif Khan
Nov 16 '18 at 17:56













Your code doesn't work, for instance m,n are defined after they are first used. If you can provide reproducible code compatiible with cvxpy 1.0 then I'm happy to help.

– Michal Adamaszek
Nov 30 '18 at 13:45





Your code doesn't work, for instance m,n are defined after they are first used. If you can provide reproducible code compatiible with cvxpy 1.0 then I'm happy to help.

– Michal Adamaszek
Nov 30 '18 at 13:45












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333253%2fhow-to-solve-a-facility-location-allocation-ip-problem-in-cvxpy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333253%2fhow-to-solve-a-facility-location-allocation-ip-problem-in-cvxpy%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric