Mixed Integer to connect islands together or to a terminal












0















I am trying to use MIP using pulp package to connect islands together or to a terminal. The desired solution is to find the minimum distances in the system.



The results of the MIP code below shows the minimum distance is to connect all islands to the terminal although the distances between the islands to the terminal is clearly high. The code should has connected some islands together.



What am I doing wrong ? Appreciate your support.



import itertools
import pulp

islands = {0: [(0, 0)], 1: [(0, 7)], 2: [(3, 3)]} ## islands id = 0,1,2 and their locations
terminal = {3: [(1,1)]} ## the terminal id = 3 and its location
distances = {(0,1): 1.0, (0,2): 2.0, (1,2): 3.0, (0,3): 33.0, (1,3): 34.0, (2,3): 23.0} ## distances of connecting islands together and islands to terminal

islandsPair = [(m, n) for m in islands for n in islands if m < n] ## islands pair
terminalPair = [(b, c) for b in islands for c in terminal] ## island-terminal pair

x = pulp.LpVariable.dicts("x", distances.keys(), lowBound=0, upBound=1)
p = pulp.LpVariable.dicts("p", islandsPair, lowBound=0, upBound=1)
l = pulp.LpVariable.dicts("l", terminalPair, lowBound=0, upBound=1)

mod = pulp.LpProblem("Islands", pulp.LpMinimize)

# Objective
mod += sum([distances[k] * x[k] for k in distances])


## constraint that there has to be at least 3 connections in the system:
for island in range(len(islands)):
mod += sum(p[(m, n)] for m in islands for n in islands if m < n) + sum(l[(b, c)] for b in islands for c in terminal) >= 3



# Solve and output
mod.solve()

## printing the solutions:

print pulp.LpStatus[mod.status]

print '0,3',l[(0, 3)].value()
print '1,3',l[(1, 3)].value()
print '2,3',l[(2, 3)].value()

print '0,1',p[(0, 1)].value()
print '0,2',p[(0, 2)].value()
print '1,2',p[(1, 2)].value()









share|improve this question


















  • 1





    Could you more precisely describe your objective value and constraints? Is your question the same as stackoverflow.com/q/30555606/3093387 ?

    – josliber
    Nov 25 '18 at 5:39











  • Hello, I saw your solution to the other island problem. I have a similar problem to that one posted in this link stackoverflow.com/questions/53440953/… The connection is very similar with a few different assumptions. I need help to formulate proper constraints to make it work. I would appreciate if you can take a look given that you had a really great solution to the problem you shared.

    – M.Khaled
    Nov 27 '18 at 0:36


















0















I am trying to use MIP using pulp package to connect islands together or to a terminal. The desired solution is to find the minimum distances in the system.



The results of the MIP code below shows the minimum distance is to connect all islands to the terminal although the distances between the islands to the terminal is clearly high. The code should has connected some islands together.



What am I doing wrong ? Appreciate your support.



import itertools
import pulp

islands = {0: [(0, 0)], 1: [(0, 7)], 2: [(3, 3)]} ## islands id = 0,1,2 and their locations
terminal = {3: [(1,1)]} ## the terminal id = 3 and its location
distances = {(0,1): 1.0, (0,2): 2.0, (1,2): 3.0, (0,3): 33.0, (1,3): 34.0, (2,3): 23.0} ## distances of connecting islands together and islands to terminal

islandsPair = [(m, n) for m in islands for n in islands if m < n] ## islands pair
terminalPair = [(b, c) for b in islands for c in terminal] ## island-terminal pair

x = pulp.LpVariable.dicts("x", distances.keys(), lowBound=0, upBound=1)
p = pulp.LpVariable.dicts("p", islandsPair, lowBound=0, upBound=1)
l = pulp.LpVariable.dicts("l", terminalPair, lowBound=0, upBound=1)

mod = pulp.LpProblem("Islands", pulp.LpMinimize)

# Objective
mod += sum([distances[k] * x[k] for k in distances])


## constraint that there has to be at least 3 connections in the system:
for island in range(len(islands)):
mod += sum(p[(m, n)] for m in islands for n in islands if m < n) + sum(l[(b, c)] for b in islands for c in terminal) >= 3



# Solve and output
mod.solve()

## printing the solutions:

print pulp.LpStatus[mod.status]

print '0,3',l[(0, 3)].value()
print '1,3',l[(1, 3)].value()
print '2,3',l[(2, 3)].value()

print '0,1',p[(0, 1)].value()
print '0,2',p[(0, 2)].value()
print '1,2',p[(1, 2)].value()









share|improve this question


















  • 1





    Could you more precisely describe your objective value and constraints? Is your question the same as stackoverflow.com/q/30555606/3093387 ?

    – josliber
    Nov 25 '18 at 5:39











  • Hello, I saw your solution to the other island problem. I have a similar problem to that one posted in this link stackoverflow.com/questions/53440953/… The connection is very similar with a few different assumptions. I need help to formulate proper constraints to make it work. I would appreciate if you can take a look given that you had a really great solution to the problem you shared.

    – M.Khaled
    Nov 27 '18 at 0:36
















0












0








0








I am trying to use MIP using pulp package to connect islands together or to a terminal. The desired solution is to find the minimum distances in the system.



The results of the MIP code below shows the minimum distance is to connect all islands to the terminal although the distances between the islands to the terminal is clearly high. The code should has connected some islands together.



What am I doing wrong ? Appreciate your support.



import itertools
import pulp

islands = {0: [(0, 0)], 1: [(0, 7)], 2: [(3, 3)]} ## islands id = 0,1,2 and their locations
terminal = {3: [(1,1)]} ## the terminal id = 3 and its location
distances = {(0,1): 1.0, (0,2): 2.0, (1,2): 3.0, (0,3): 33.0, (1,3): 34.0, (2,3): 23.0} ## distances of connecting islands together and islands to terminal

islandsPair = [(m, n) for m in islands for n in islands if m < n] ## islands pair
terminalPair = [(b, c) for b in islands for c in terminal] ## island-terminal pair

x = pulp.LpVariable.dicts("x", distances.keys(), lowBound=0, upBound=1)
p = pulp.LpVariable.dicts("p", islandsPair, lowBound=0, upBound=1)
l = pulp.LpVariable.dicts("l", terminalPair, lowBound=0, upBound=1)

mod = pulp.LpProblem("Islands", pulp.LpMinimize)

# Objective
mod += sum([distances[k] * x[k] for k in distances])


## constraint that there has to be at least 3 connections in the system:
for island in range(len(islands)):
mod += sum(p[(m, n)] for m in islands for n in islands if m < n) + sum(l[(b, c)] for b in islands for c in terminal) >= 3



# Solve and output
mod.solve()

## printing the solutions:

print pulp.LpStatus[mod.status]

print '0,3',l[(0, 3)].value()
print '1,3',l[(1, 3)].value()
print '2,3',l[(2, 3)].value()

print '0,1',p[(0, 1)].value()
print '0,2',p[(0, 2)].value()
print '1,2',p[(1, 2)].value()









share|improve this question














I am trying to use MIP using pulp package to connect islands together or to a terminal. The desired solution is to find the minimum distances in the system.



The results of the MIP code below shows the minimum distance is to connect all islands to the terminal although the distances between the islands to the terminal is clearly high. The code should has connected some islands together.



What am I doing wrong ? Appreciate your support.



import itertools
import pulp

islands = {0: [(0, 0)], 1: [(0, 7)], 2: [(3, 3)]} ## islands id = 0,1,2 and their locations
terminal = {3: [(1,1)]} ## the terminal id = 3 and its location
distances = {(0,1): 1.0, (0,2): 2.0, (1,2): 3.0, (0,3): 33.0, (1,3): 34.0, (2,3): 23.0} ## distances of connecting islands together and islands to terminal

islandsPair = [(m, n) for m in islands for n in islands if m < n] ## islands pair
terminalPair = [(b, c) for b in islands for c in terminal] ## island-terminal pair

x = pulp.LpVariable.dicts("x", distances.keys(), lowBound=0, upBound=1)
p = pulp.LpVariable.dicts("p", islandsPair, lowBound=0, upBound=1)
l = pulp.LpVariable.dicts("l", terminalPair, lowBound=0, upBound=1)

mod = pulp.LpProblem("Islands", pulp.LpMinimize)

# Objective
mod += sum([distances[k] * x[k] for k in distances])


## constraint that there has to be at least 3 connections in the system:
for island in range(len(islands)):
mod += sum(p[(m, n)] for m in islands for n in islands if m < n) + sum(l[(b, c)] for b in islands for c in terminal) >= 3



# Solve and output
mod.solve()

## printing the solutions:

print pulp.LpStatus[mod.status]

print '0,3',l[(0, 3)].value()
print '1,3',l[(1, 3)].value()
print '2,3',l[(2, 3)].value()

print '0,1',p[(0, 1)].value()
print '0,2',p[(0, 2)].value()
print '1,2',p[(1, 2)].value()






python mathematical-optimization linear-programming pulp mixed-integer-programming






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 4:24









M.Khaled M.Khaled

184




184








  • 1





    Could you more precisely describe your objective value and constraints? Is your question the same as stackoverflow.com/q/30555606/3093387 ?

    – josliber
    Nov 25 '18 at 5:39











  • Hello, I saw your solution to the other island problem. I have a similar problem to that one posted in this link stackoverflow.com/questions/53440953/… The connection is very similar with a few different assumptions. I need help to formulate proper constraints to make it work. I would appreciate if you can take a look given that you had a really great solution to the problem you shared.

    – M.Khaled
    Nov 27 '18 at 0:36
















  • 1





    Could you more precisely describe your objective value and constraints? Is your question the same as stackoverflow.com/q/30555606/3093387 ?

    – josliber
    Nov 25 '18 at 5:39











  • Hello, I saw your solution to the other island problem. I have a similar problem to that one posted in this link stackoverflow.com/questions/53440953/… The connection is very similar with a few different assumptions. I need help to formulate proper constraints to make it work. I would appreciate if you can take a look given that you had a really great solution to the problem you shared.

    – M.Khaled
    Nov 27 '18 at 0:36










1




1





Could you more precisely describe your objective value and constraints? Is your question the same as stackoverflow.com/q/30555606/3093387 ?

– josliber
Nov 25 '18 at 5:39





Could you more precisely describe your objective value and constraints? Is your question the same as stackoverflow.com/q/30555606/3093387 ?

– josliber
Nov 25 '18 at 5:39













Hello, I saw your solution to the other island problem. I have a similar problem to that one posted in this link stackoverflow.com/questions/53440953/… The connection is very similar with a few different assumptions. I need help to formulate proper constraints to make it work. I would appreciate if you can take a look given that you had a really great solution to the problem you shared.

– M.Khaled
Nov 27 '18 at 0:36







Hello, I saw your solution to the other island problem. I have a similar problem to that one posted in this link stackoverflow.com/questions/53440953/… The connection is very similar with a few different assumptions. I need help to formulate proper constraints to make it work. I would appreciate if you can take a look given that you had a really great solution to the problem you shared.

– M.Khaled
Nov 27 '18 at 0:36














1 Answer
1






active

oldest

votes


















1














There are several issues with your code:




  • you use for island in range(len(islands)) to add a constraint of 3 edges used, but never use island so not sure why you are doing that loop instead of just adding the constraint.

  • You have no relation between x and p, l so as you are only minimizing relating to x, x values are all 0 because there are no constraints on it.

  • I don´t see the need at all for p and l when you already have x to model if the edges are used or not.


All in all, here is what I would do:



import itertools
import pulp

distances = {(0,1): 1.0, (0,2): 2.0, (1,2): 3.0, (0,3): 33.0, (1,3): 34.0, (2,3): 23.0} ## distances of connecting islands together and islands to terminal

x = pulp.LpVariable.dicts("x", distances.keys(), lowBound=0, upBound=1)

mod = pulp.LpProblem("Islands", pulp.LpMinimize)

y = pulp.LpVariable("y", lowBound=0, upBound = sum(distances.values()))
y = sum([distances[k] * x[k] for k in distances])
# Objective
mod += y

## constraint that there has to be at least 3 connections in the system:
mod += sum(x[edge] for edge in distances) >= 3

# Solve and output
mod.solve()

## printing the solutions:

print (pulp.LpStatus[mod.status], y.value())

for edge in distances:
print (edge, x[edge].value())





share|improve this answer
























  • Thank you for your answer

    – M.Khaled
    Nov 26 '18 at 23:45











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%2f53464637%2fmixed-integer-to-connect-islands-together-or-to-a-terminal%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














There are several issues with your code:




  • you use for island in range(len(islands)) to add a constraint of 3 edges used, but never use island so not sure why you are doing that loop instead of just adding the constraint.

  • You have no relation between x and p, l so as you are only minimizing relating to x, x values are all 0 because there are no constraints on it.

  • I don´t see the need at all for p and l when you already have x to model if the edges are used or not.


All in all, here is what I would do:



import itertools
import pulp

distances = {(0,1): 1.0, (0,2): 2.0, (1,2): 3.0, (0,3): 33.0, (1,3): 34.0, (2,3): 23.0} ## distances of connecting islands together and islands to terminal

x = pulp.LpVariable.dicts("x", distances.keys(), lowBound=0, upBound=1)

mod = pulp.LpProblem("Islands", pulp.LpMinimize)

y = pulp.LpVariable("y", lowBound=0, upBound = sum(distances.values()))
y = sum([distances[k] * x[k] for k in distances])
# Objective
mod += y

## constraint that there has to be at least 3 connections in the system:
mod += sum(x[edge] for edge in distances) >= 3

# Solve and output
mod.solve()

## printing the solutions:

print (pulp.LpStatus[mod.status], y.value())

for edge in distances:
print (edge, x[edge].value())





share|improve this answer
























  • Thank you for your answer

    – M.Khaled
    Nov 26 '18 at 23:45
















1














There are several issues with your code:




  • you use for island in range(len(islands)) to add a constraint of 3 edges used, but never use island so not sure why you are doing that loop instead of just adding the constraint.

  • You have no relation between x and p, l so as you are only minimizing relating to x, x values are all 0 because there are no constraints on it.

  • I don´t see the need at all for p and l when you already have x to model if the edges are used or not.


All in all, here is what I would do:



import itertools
import pulp

distances = {(0,1): 1.0, (0,2): 2.0, (1,2): 3.0, (0,3): 33.0, (1,3): 34.0, (2,3): 23.0} ## distances of connecting islands together and islands to terminal

x = pulp.LpVariable.dicts("x", distances.keys(), lowBound=0, upBound=1)

mod = pulp.LpProblem("Islands", pulp.LpMinimize)

y = pulp.LpVariable("y", lowBound=0, upBound = sum(distances.values()))
y = sum([distances[k] * x[k] for k in distances])
# Objective
mod += y

## constraint that there has to be at least 3 connections in the system:
mod += sum(x[edge] for edge in distances) >= 3

# Solve and output
mod.solve()

## printing the solutions:

print (pulp.LpStatus[mod.status], y.value())

for edge in distances:
print (edge, x[edge].value())





share|improve this answer
























  • Thank you for your answer

    – M.Khaled
    Nov 26 '18 at 23:45














1












1








1







There are several issues with your code:




  • you use for island in range(len(islands)) to add a constraint of 3 edges used, but never use island so not sure why you are doing that loop instead of just adding the constraint.

  • You have no relation between x and p, l so as you are only minimizing relating to x, x values are all 0 because there are no constraints on it.

  • I don´t see the need at all for p and l when you already have x to model if the edges are used or not.


All in all, here is what I would do:



import itertools
import pulp

distances = {(0,1): 1.0, (0,2): 2.0, (1,2): 3.0, (0,3): 33.0, (1,3): 34.0, (2,3): 23.0} ## distances of connecting islands together and islands to terminal

x = pulp.LpVariable.dicts("x", distances.keys(), lowBound=0, upBound=1)

mod = pulp.LpProblem("Islands", pulp.LpMinimize)

y = pulp.LpVariable("y", lowBound=0, upBound = sum(distances.values()))
y = sum([distances[k] * x[k] for k in distances])
# Objective
mod += y

## constraint that there has to be at least 3 connections in the system:
mod += sum(x[edge] for edge in distances) >= 3

# Solve and output
mod.solve()

## printing the solutions:

print (pulp.LpStatus[mod.status], y.value())

for edge in distances:
print (edge, x[edge].value())





share|improve this answer













There are several issues with your code:




  • you use for island in range(len(islands)) to add a constraint of 3 edges used, but never use island so not sure why you are doing that loop instead of just adding the constraint.

  • You have no relation between x and p, l so as you are only minimizing relating to x, x values are all 0 because there are no constraints on it.

  • I don´t see the need at all for p and l when you already have x to model if the edges are used or not.


All in all, here is what I would do:



import itertools
import pulp

distances = {(0,1): 1.0, (0,2): 2.0, (1,2): 3.0, (0,3): 33.0, (1,3): 34.0, (2,3): 23.0} ## distances of connecting islands together and islands to terminal

x = pulp.LpVariable.dicts("x", distances.keys(), lowBound=0, upBound=1)

mod = pulp.LpProblem("Islands", pulp.LpMinimize)

y = pulp.LpVariable("y", lowBound=0, upBound = sum(distances.values()))
y = sum([distances[k] * x[k] for k in distances])
# Objective
mod += y

## constraint that there has to be at least 3 connections in the system:
mod += sum(x[edge] for edge in distances) >= 3

# Solve and output
mod.solve()

## printing the solutions:

print (pulp.LpStatus[mod.status], y.value())

for edge in distances:
print (edge, x[edge].value())






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 15:19









juvianjuvian

13.3k22026




13.3k22026













  • Thank you for your answer

    – M.Khaled
    Nov 26 '18 at 23:45



















  • Thank you for your answer

    – M.Khaled
    Nov 26 '18 at 23:45

















Thank you for your answer

– M.Khaled
Nov 26 '18 at 23:45





Thank you for your answer

– M.Khaled
Nov 26 '18 at 23:45


















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%2f53464637%2fmixed-integer-to-connect-islands-together-or-to-a-terminal%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)