How to implement a cost minimization objective function correctly in Gurobi?Is there a built-in function to print all the current properties and values of an object?How to flush output of print function?How to return multiple values from a function?How to know if an object has an attribute in PythonHow to make a chain of function decorators?In Python, how do I determine if an object is iterable?“Large data” work flows using pandasGurobi Irreducible Subset ISS contains no conflict?Gurobi, Robust Optimization, Demand UncertaintyAdd model information to the Gurobi log

What is the color associated with lukewarm?

How could I create a situation in which a PC has to make a saving throw or be forced to pet a dog?

Catching a robber on one line

Is there a term for someone whose preferred policies are a mix of Left and Right?

My student in one course asks for paid tutoring in another course. Appropriate?

On George Box, Galit Shmueli and the scientific method?

Cut power on a remote Raspberry Pi 3 via another raspi

How can Caller ID be faked?

How do I become a better writer when I hate reading?

How can I ping multiple IP addresses at the same time?

Lead the way to this Literary Knight to its final “DESTINATION”

Digital signature that is only verifiable by one specific person

What are the mechanical differences between Adapt and Monstrosity?

What do I put on my resume to make the company i'm applying to think i'm mature enough to handle a job?

Co-worker is now managing my team. Does this mean that I'm being demoted?

Fill the maze with a wall-following Snake until it gets stuck

How to avoid offending original culture when making conculture inspired from original

I have found ports on my Samsung smart tv running a display service. What can I do with it?

What does a/.b[c][[1]] mean?

How can I detect if I'm in a subshell?

Do my partner and son need an SSN to be dependents on my taxes?

Why do you need to heat the pan before heating the olive oil?

Can a non-invertible function be inverted by returning a set of all possible solutions?

At what temperature should the earth be cooked to prevent human infection?



How to implement a cost minimization objective function correctly in Gurobi?


Is there a built-in function to print all the current properties and values of an object?How to flush output of print function?How to return multiple values from a function?How to know if an object has an attribute in PythonHow to make a chain of function decorators?In Python, how do I determine if an object is iterable?“Large data” work flows using pandasGurobi Irreducible Subset ISS contains no conflict?Gurobi, Robust Optimization, Demand UncertaintyAdd model information to the Gurobi log






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















Given transport costs, per single unit of delivery, for a supermarket from three distribution centers to ten separate stores.



Note: Please look in the #data section of my code to see the data that I'm not allowed to post in photo form. ALSO note while my costs are a vector with 30 entries. Each distribution centre can only access 10 costs each. So DC1 costs = entries 1-10, DC2 costs = entries 11-20 etc..




I want to minimize the transport cost subject to each of the ten stores demand (in units of delivery).



This can be done by inspection. The the minimum cost being $150313. The problem being implementing the solution with Python and Gurobi and producing the same result.




What I've tried is a somewhat sloppy model of the problem in Gurobi so far. I'm not sure how to correctly index and iterate through my sets that are required to produce a result.



This is my main problem: The objective function I define to minimize transport costs is not correct as I produce a non-answer.



The code "runs" though. If I change to maximization I just get an unbounded problem. So I feel like I am definitely not calling the correct data/iterations through sets into play.



My solution so far is quite small, so I feel like I can format it into the question and comment along the way.



from gurobipy import *

#Sets
Distro = ["DC0","DC1","DC2"]
Stores = ["S0", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9"]
D = range(len(Distro))
S = range(len(Stores))


Here I define my sets of distribution centres and set of stores. I am not sure where or how to exactly define the D and S iteration variables to get a correct answer.




#Data
Demand = [10,16,11,8,8,18,11,20,13,12]
Costs = [1992,2666,977,1761,2933,1387,2307,1814,706,1162,
2471,2023,3096,2103,712,2304,1440,2180,2925,2432,
1642,2058,1533,1102,1970,908,1372,1317,1341,776]


Just a block of my relevant data. I am not sure if my cost data should be 3 separate sets considering each distribution centre only has access to 10 costs and not 30. Or if there is a way to keep my costs as one set but make sure each centre can only access the costs relevant to itself I would not know.



m = Model("WonderMarket")
#Variables
X =
for d in D:
for s in S:
X[d,s] = m.addVar()


Declaring my objective variable. Again, I'm blindly iterating at this point to produce something that works. I've never programmed before. But I'm learning and putting as much thought into this question as possible.



#set objective
m.setObjective(quicksum(Costs[s] * X[d, s] * Demand[s] for d in D for s in S), GRB.MINIMIZE)


My objective function is attempting to multiply the cost of each delivery from a centre to a store, subject to a stores demand, then make that the smallest value possible. I do not have a non zero constraint yet. I will need one eventually?! But right now I have bigger fish to fry.



m.optimize()


I produce a 0 row, 30 column with 0 nonzero entries model that gives me a solution of 0. I need to set up my program so that I get the value that can be calculated easily by hand. I believe the issue is my general declaring of variables and low knowledge of iteration and general "what goes where" issues. A lot of thinking for just a study exercise!



Appreciate anyone who has read all the way through. Thank you for any tips or help in advance.










share|improve this question






























    0















    Given transport costs, per single unit of delivery, for a supermarket from three distribution centers to ten separate stores.



    Note: Please look in the #data section of my code to see the data that I'm not allowed to post in photo form. ALSO note while my costs are a vector with 30 entries. Each distribution centre can only access 10 costs each. So DC1 costs = entries 1-10, DC2 costs = entries 11-20 etc..




    I want to minimize the transport cost subject to each of the ten stores demand (in units of delivery).



    This can be done by inspection. The the minimum cost being $150313. The problem being implementing the solution with Python and Gurobi and producing the same result.




    What I've tried is a somewhat sloppy model of the problem in Gurobi so far. I'm not sure how to correctly index and iterate through my sets that are required to produce a result.



    This is my main problem: The objective function I define to minimize transport costs is not correct as I produce a non-answer.



    The code "runs" though. If I change to maximization I just get an unbounded problem. So I feel like I am definitely not calling the correct data/iterations through sets into play.



    My solution so far is quite small, so I feel like I can format it into the question and comment along the way.



    from gurobipy import *

    #Sets
    Distro = ["DC0","DC1","DC2"]
    Stores = ["S0", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9"]
    D = range(len(Distro))
    S = range(len(Stores))


    Here I define my sets of distribution centres and set of stores. I am not sure where or how to exactly define the D and S iteration variables to get a correct answer.




    #Data
    Demand = [10,16,11,8,8,18,11,20,13,12]
    Costs = [1992,2666,977,1761,2933,1387,2307,1814,706,1162,
    2471,2023,3096,2103,712,2304,1440,2180,2925,2432,
    1642,2058,1533,1102,1970,908,1372,1317,1341,776]


    Just a block of my relevant data. I am not sure if my cost data should be 3 separate sets considering each distribution centre only has access to 10 costs and not 30. Or if there is a way to keep my costs as one set but make sure each centre can only access the costs relevant to itself I would not know.



    m = Model("WonderMarket")
    #Variables
    X =
    for d in D:
    for s in S:
    X[d,s] = m.addVar()


    Declaring my objective variable. Again, I'm blindly iterating at this point to produce something that works. I've never programmed before. But I'm learning and putting as much thought into this question as possible.



    #set objective
    m.setObjective(quicksum(Costs[s] * X[d, s] * Demand[s] for d in D for s in S), GRB.MINIMIZE)


    My objective function is attempting to multiply the cost of each delivery from a centre to a store, subject to a stores demand, then make that the smallest value possible. I do not have a non zero constraint yet. I will need one eventually?! But right now I have bigger fish to fry.



    m.optimize()


    I produce a 0 row, 30 column with 0 nonzero entries model that gives me a solution of 0. I need to set up my program so that I get the value that can be calculated easily by hand. I believe the issue is my general declaring of variables and low knowledge of iteration and general "what goes where" issues. A lot of thinking for just a study exercise!



    Appreciate anyone who has read all the way through. Thank you for any tips or help in advance.










    share|improve this question


























      0












      0








      0








      Given transport costs, per single unit of delivery, for a supermarket from three distribution centers to ten separate stores.



      Note: Please look in the #data section of my code to see the data that I'm not allowed to post in photo form. ALSO note while my costs are a vector with 30 entries. Each distribution centre can only access 10 costs each. So DC1 costs = entries 1-10, DC2 costs = entries 11-20 etc..




      I want to minimize the transport cost subject to each of the ten stores demand (in units of delivery).



      This can be done by inspection. The the minimum cost being $150313. The problem being implementing the solution with Python and Gurobi and producing the same result.




      What I've tried is a somewhat sloppy model of the problem in Gurobi so far. I'm not sure how to correctly index and iterate through my sets that are required to produce a result.



      This is my main problem: The objective function I define to minimize transport costs is not correct as I produce a non-answer.



      The code "runs" though. If I change to maximization I just get an unbounded problem. So I feel like I am definitely not calling the correct data/iterations through sets into play.



      My solution so far is quite small, so I feel like I can format it into the question and comment along the way.



      from gurobipy import *

      #Sets
      Distro = ["DC0","DC1","DC2"]
      Stores = ["S0", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9"]
      D = range(len(Distro))
      S = range(len(Stores))


      Here I define my sets of distribution centres and set of stores. I am not sure where or how to exactly define the D and S iteration variables to get a correct answer.




      #Data
      Demand = [10,16,11,8,8,18,11,20,13,12]
      Costs = [1992,2666,977,1761,2933,1387,2307,1814,706,1162,
      2471,2023,3096,2103,712,2304,1440,2180,2925,2432,
      1642,2058,1533,1102,1970,908,1372,1317,1341,776]


      Just a block of my relevant data. I am not sure if my cost data should be 3 separate sets considering each distribution centre only has access to 10 costs and not 30. Or if there is a way to keep my costs as one set but make sure each centre can only access the costs relevant to itself I would not know.



      m = Model("WonderMarket")
      #Variables
      X =
      for d in D:
      for s in S:
      X[d,s] = m.addVar()


      Declaring my objective variable. Again, I'm blindly iterating at this point to produce something that works. I've never programmed before. But I'm learning and putting as much thought into this question as possible.



      #set objective
      m.setObjective(quicksum(Costs[s] * X[d, s] * Demand[s] for d in D for s in S), GRB.MINIMIZE)


      My objective function is attempting to multiply the cost of each delivery from a centre to a store, subject to a stores demand, then make that the smallest value possible. I do not have a non zero constraint yet. I will need one eventually?! But right now I have bigger fish to fry.



      m.optimize()


      I produce a 0 row, 30 column with 0 nonzero entries model that gives me a solution of 0. I need to set up my program so that I get the value that can be calculated easily by hand. I believe the issue is my general declaring of variables and low knowledge of iteration and general "what goes where" issues. A lot of thinking for just a study exercise!



      Appreciate anyone who has read all the way through. Thank you for any tips or help in advance.










      share|improve this question
















      Given transport costs, per single unit of delivery, for a supermarket from three distribution centers to ten separate stores.



      Note: Please look in the #data section of my code to see the data that I'm not allowed to post in photo form. ALSO note while my costs are a vector with 30 entries. Each distribution centre can only access 10 costs each. So DC1 costs = entries 1-10, DC2 costs = entries 11-20 etc..




      I want to minimize the transport cost subject to each of the ten stores demand (in units of delivery).



      This can be done by inspection. The the minimum cost being $150313. The problem being implementing the solution with Python and Gurobi and producing the same result.




      What I've tried is a somewhat sloppy model of the problem in Gurobi so far. I'm not sure how to correctly index and iterate through my sets that are required to produce a result.



      This is my main problem: The objective function I define to minimize transport costs is not correct as I produce a non-answer.



      The code "runs" though. If I change to maximization I just get an unbounded problem. So I feel like I am definitely not calling the correct data/iterations through sets into play.



      My solution so far is quite small, so I feel like I can format it into the question and comment along the way.



      from gurobipy import *

      #Sets
      Distro = ["DC0","DC1","DC2"]
      Stores = ["S0", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9"]
      D = range(len(Distro))
      S = range(len(Stores))


      Here I define my sets of distribution centres and set of stores. I am not sure where or how to exactly define the D and S iteration variables to get a correct answer.




      #Data
      Demand = [10,16,11,8,8,18,11,20,13,12]
      Costs = [1992,2666,977,1761,2933,1387,2307,1814,706,1162,
      2471,2023,3096,2103,712,2304,1440,2180,2925,2432,
      1642,2058,1533,1102,1970,908,1372,1317,1341,776]


      Just a block of my relevant data. I am not sure if my cost data should be 3 separate sets considering each distribution centre only has access to 10 costs and not 30. Or if there is a way to keep my costs as one set but make sure each centre can only access the costs relevant to itself I would not know.



      m = Model("WonderMarket")
      #Variables
      X =
      for d in D:
      for s in S:
      X[d,s] = m.addVar()


      Declaring my objective variable. Again, I'm blindly iterating at this point to produce something that works. I've never programmed before. But I'm learning and putting as much thought into this question as possible.



      #set objective
      m.setObjective(quicksum(Costs[s] * X[d, s] * Demand[s] for d in D for s in S), GRB.MINIMIZE)


      My objective function is attempting to multiply the cost of each delivery from a centre to a store, subject to a stores demand, then make that the smallest value possible. I do not have a non zero constraint yet. I will need one eventually?! But right now I have bigger fish to fry.



      m.optimize()


      I produce a 0 row, 30 column with 0 nonzero entries model that gives me a solution of 0. I need to set up my program so that I get the value that can be calculated easily by hand. I believe the issue is my general declaring of variables and low knowledge of iteration and general "what goes where" issues. A lot of thinking for just a study exercise!



      Appreciate anyone who has read all the way through. Thank you for any tips or help in advance.







      python linear-programming gurobi






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 26 at 5:38









      Silke Horn

      17016




      17016










      asked Mar 25 at 4:09









      99 Fishing99 Fishing

      51




      51






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Your objective is 0 because you do not have defined any constraints. By default all variables have a lower bound of 0 and hence minizing an unconstrained problem puts all variables to this lower bound.



          A few comments:



          Unless you need the names for the distribution centers and stores, you could define them as follows:



          D = 3
          S = 10
          Distro = range(D)
          Stores = range(S)


          You could define the costs as a 2-dimensional array, e.g.



          Costs = [[1992,2666,977,1761,2933,1387,2307,1814,706,1162],
          [2471,2023,3096,2103,712,2304,1440,2180,2925,2432],
          [1642,2058,1533,1102,1970,908,1372,1317,1341,776]]


          Then the cost of transportation from distribution center d to store s are stored in Costs[d][s].



          You can add all variables at once and I assume you want them to be binary:



          X = m.addVars(D, S, vtype=GRB.BINARY)


          (or use Distro and Stores instead of D and S if you need to use the names).



          Your definition of the objective function then becomes:



          m.setObjective(quicksum(Costs[d][s] * X[d, s] * Demand[s] for d in Distro for s in Stores), GRB.MINIMIZE)


          (This is all assuming that each store can only be delivered from one distribution center, but since your distribution centers do not have a maximal capacity this seems to be a fair assumption.)



          You need constraints ensuring that the stores' demands are actually satisfied. For this it suffices to ensure that each store is being delivered from one distribution center, i.e., that for each s one X[d, s] is 1.



          m.addConstrs(quicksum(X[d, s] for d in Distro) == 1 for s in Stores)


          When I optimize this, I indeed get an optimal solution with value 150313.






          share|improve this answer























          • Wow, thank you for your response! I've been reading through this for a while and I am looking forward to implementing this when I get home. Thank you. You make a point about something more complicated though, what WOULD I do if my distribution centres had capacity requirements? That seems more like real-life. Would I go about defining a DistroMax = [73,70,25] (any combination that sums the demand really) then adding a constraint like m.addConstr(quicksum((DistroMax[d] - Demand[d])*X[d,s] for d in Distro) >= 0)? Actually I'm not sure if that'd work or change the model at all

            – 99 Fishing
            Mar 26 at 2:03












          • If you ever come back to this thread would love to hear your thoughts on what you proposed in your answer. I might be on the right track but I think I would still get the same answer.

            – 99 Fishing
            Mar 26 at 2:04












          • If there were capacity constraints on the distribution centers, I would use integer variables instead of binaries (if the goods can only be delivered in discrete quantities, otherwise continuous ones). Then you need to make sure that the capacity constraints are satisfied in the distribution centers (quicksum(X[d, s] for s in Stores) <= DistroMax[d]) and that the requirements are met in the stores (quicksum(X[d, s] for d in Distro) = Demand[s]). The objective would become quicksum(Costs[d][s] * X[d, s] for d in Distro for s in Stores).

            – Silke Horn
            Mar 28 at 17:55












          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%2f55331146%2fhow-to-implement-a-cost-minimization-objective-function-correctly-in-gurobi%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














          Your objective is 0 because you do not have defined any constraints. By default all variables have a lower bound of 0 and hence minizing an unconstrained problem puts all variables to this lower bound.



          A few comments:



          Unless you need the names for the distribution centers and stores, you could define them as follows:



          D = 3
          S = 10
          Distro = range(D)
          Stores = range(S)


          You could define the costs as a 2-dimensional array, e.g.



          Costs = [[1992,2666,977,1761,2933,1387,2307,1814,706,1162],
          [2471,2023,3096,2103,712,2304,1440,2180,2925,2432],
          [1642,2058,1533,1102,1970,908,1372,1317,1341,776]]


          Then the cost of transportation from distribution center d to store s are stored in Costs[d][s].



          You can add all variables at once and I assume you want them to be binary:



          X = m.addVars(D, S, vtype=GRB.BINARY)


          (or use Distro and Stores instead of D and S if you need to use the names).



          Your definition of the objective function then becomes:



          m.setObjective(quicksum(Costs[d][s] * X[d, s] * Demand[s] for d in Distro for s in Stores), GRB.MINIMIZE)


          (This is all assuming that each store can only be delivered from one distribution center, but since your distribution centers do not have a maximal capacity this seems to be a fair assumption.)



          You need constraints ensuring that the stores' demands are actually satisfied. For this it suffices to ensure that each store is being delivered from one distribution center, i.e., that for each s one X[d, s] is 1.



          m.addConstrs(quicksum(X[d, s] for d in Distro) == 1 for s in Stores)


          When I optimize this, I indeed get an optimal solution with value 150313.






          share|improve this answer























          • Wow, thank you for your response! I've been reading through this for a while and I am looking forward to implementing this when I get home. Thank you. You make a point about something more complicated though, what WOULD I do if my distribution centres had capacity requirements? That seems more like real-life. Would I go about defining a DistroMax = [73,70,25] (any combination that sums the demand really) then adding a constraint like m.addConstr(quicksum((DistroMax[d] - Demand[d])*X[d,s] for d in Distro) >= 0)? Actually I'm not sure if that'd work or change the model at all

            – 99 Fishing
            Mar 26 at 2:03












          • If you ever come back to this thread would love to hear your thoughts on what you proposed in your answer. I might be on the right track but I think I would still get the same answer.

            – 99 Fishing
            Mar 26 at 2:04












          • If there were capacity constraints on the distribution centers, I would use integer variables instead of binaries (if the goods can only be delivered in discrete quantities, otherwise continuous ones). Then you need to make sure that the capacity constraints are satisfied in the distribution centers (quicksum(X[d, s] for s in Stores) <= DistroMax[d]) and that the requirements are met in the stores (quicksum(X[d, s] for d in Distro) = Demand[s]). The objective would become quicksum(Costs[d][s] * X[d, s] for d in Distro for s in Stores).

            – Silke Horn
            Mar 28 at 17:55
















          1














          Your objective is 0 because you do not have defined any constraints. By default all variables have a lower bound of 0 and hence minizing an unconstrained problem puts all variables to this lower bound.



          A few comments:



          Unless you need the names for the distribution centers and stores, you could define them as follows:



          D = 3
          S = 10
          Distro = range(D)
          Stores = range(S)


          You could define the costs as a 2-dimensional array, e.g.



          Costs = [[1992,2666,977,1761,2933,1387,2307,1814,706,1162],
          [2471,2023,3096,2103,712,2304,1440,2180,2925,2432],
          [1642,2058,1533,1102,1970,908,1372,1317,1341,776]]


          Then the cost of transportation from distribution center d to store s are stored in Costs[d][s].



          You can add all variables at once and I assume you want them to be binary:



          X = m.addVars(D, S, vtype=GRB.BINARY)


          (or use Distro and Stores instead of D and S if you need to use the names).



          Your definition of the objective function then becomes:



          m.setObjective(quicksum(Costs[d][s] * X[d, s] * Demand[s] for d in Distro for s in Stores), GRB.MINIMIZE)


          (This is all assuming that each store can only be delivered from one distribution center, but since your distribution centers do not have a maximal capacity this seems to be a fair assumption.)



          You need constraints ensuring that the stores' demands are actually satisfied. For this it suffices to ensure that each store is being delivered from one distribution center, i.e., that for each s one X[d, s] is 1.



          m.addConstrs(quicksum(X[d, s] for d in Distro) == 1 for s in Stores)


          When I optimize this, I indeed get an optimal solution with value 150313.






          share|improve this answer























          • Wow, thank you for your response! I've been reading through this for a while and I am looking forward to implementing this when I get home. Thank you. You make a point about something more complicated though, what WOULD I do if my distribution centres had capacity requirements? That seems more like real-life. Would I go about defining a DistroMax = [73,70,25] (any combination that sums the demand really) then adding a constraint like m.addConstr(quicksum((DistroMax[d] - Demand[d])*X[d,s] for d in Distro) >= 0)? Actually I'm not sure if that'd work or change the model at all

            – 99 Fishing
            Mar 26 at 2:03












          • If you ever come back to this thread would love to hear your thoughts on what you proposed in your answer. I might be on the right track but I think I would still get the same answer.

            – 99 Fishing
            Mar 26 at 2:04












          • If there were capacity constraints on the distribution centers, I would use integer variables instead of binaries (if the goods can only be delivered in discrete quantities, otherwise continuous ones). Then you need to make sure that the capacity constraints are satisfied in the distribution centers (quicksum(X[d, s] for s in Stores) <= DistroMax[d]) and that the requirements are met in the stores (quicksum(X[d, s] for d in Distro) = Demand[s]). The objective would become quicksum(Costs[d][s] * X[d, s] for d in Distro for s in Stores).

            – Silke Horn
            Mar 28 at 17:55














          1












          1








          1







          Your objective is 0 because you do not have defined any constraints. By default all variables have a lower bound of 0 and hence minizing an unconstrained problem puts all variables to this lower bound.



          A few comments:



          Unless you need the names for the distribution centers and stores, you could define them as follows:



          D = 3
          S = 10
          Distro = range(D)
          Stores = range(S)


          You could define the costs as a 2-dimensional array, e.g.



          Costs = [[1992,2666,977,1761,2933,1387,2307,1814,706,1162],
          [2471,2023,3096,2103,712,2304,1440,2180,2925,2432],
          [1642,2058,1533,1102,1970,908,1372,1317,1341,776]]


          Then the cost of transportation from distribution center d to store s are stored in Costs[d][s].



          You can add all variables at once and I assume you want them to be binary:



          X = m.addVars(D, S, vtype=GRB.BINARY)


          (or use Distro and Stores instead of D and S if you need to use the names).



          Your definition of the objective function then becomes:



          m.setObjective(quicksum(Costs[d][s] * X[d, s] * Demand[s] for d in Distro for s in Stores), GRB.MINIMIZE)


          (This is all assuming that each store can only be delivered from one distribution center, but since your distribution centers do not have a maximal capacity this seems to be a fair assumption.)



          You need constraints ensuring that the stores' demands are actually satisfied. For this it suffices to ensure that each store is being delivered from one distribution center, i.e., that for each s one X[d, s] is 1.



          m.addConstrs(quicksum(X[d, s] for d in Distro) == 1 for s in Stores)


          When I optimize this, I indeed get an optimal solution with value 150313.






          share|improve this answer













          Your objective is 0 because you do not have defined any constraints. By default all variables have a lower bound of 0 and hence minizing an unconstrained problem puts all variables to this lower bound.



          A few comments:



          Unless you need the names for the distribution centers and stores, you could define them as follows:



          D = 3
          S = 10
          Distro = range(D)
          Stores = range(S)


          You could define the costs as a 2-dimensional array, e.g.



          Costs = [[1992,2666,977,1761,2933,1387,2307,1814,706,1162],
          [2471,2023,3096,2103,712,2304,1440,2180,2925,2432],
          [1642,2058,1533,1102,1970,908,1372,1317,1341,776]]


          Then the cost of transportation from distribution center d to store s are stored in Costs[d][s].



          You can add all variables at once and I assume you want them to be binary:



          X = m.addVars(D, S, vtype=GRB.BINARY)


          (or use Distro and Stores instead of D and S if you need to use the names).



          Your definition of the objective function then becomes:



          m.setObjective(quicksum(Costs[d][s] * X[d, s] * Demand[s] for d in Distro for s in Stores), GRB.MINIMIZE)


          (This is all assuming that each store can only be delivered from one distribution center, but since your distribution centers do not have a maximal capacity this seems to be a fair assumption.)



          You need constraints ensuring that the stores' demands are actually satisfied. For this it suffices to ensure that each store is being delivered from one distribution center, i.e., that for each s one X[d, s] is 1.



          m.addConstrs(quicksum(X[d, s] for d in Distro) == 1 for s in Stores)


          When I optimize this, I indeed get an optimal solution with value 150313.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 21:52









          Silke HornSilke Horn

          17016




          17016












          • Wow, thank you for your response! I've been reading through this for a while and I am looking forward to implementing this when I get home. Thank you. You make a point about something more complicated though, what WOULD I do if my distribution centres had capacity requirements? That seems more like real-life. Would I go about defining a DistroMax = [73,70,25] (any combination that sums the demand really) then adding a constraint like m.addConstr(quicksum((DistroMax[d] - Demand[d])*X[d,s] for d in Distro) >= 0)? Actually I'm not sure if that'd work or change the model at all

            – 99 Fishing
            Mar 26 at 2:03












          • If you ever come back to this thread would love to hear your thoughts on what you proposed in your answer. I might be on the right track but I think I would still get the same answer.

            – 99 Fishing
            Mar 26 at 2:04












          • If there were capacity constraints on the distribution centers, I would use integer variables instead of binaries (if the goods can only be delivered in discrete quantities, otherwise continuous ones). Then you need to make sure that the capacity constraints are satisfied in the distribution centers (quicksum(X[d, s] for s in Stores) <= DistroMax[d]) and that the requirements are met in the stores (quicksum(X[d, s] for d in Distro) = Demand[s]). The objective would become quicksum(Costs[d][s] * X[d, s] for d in Distro for s in Stores).

            – Silke Horn
            Mar 28 at 17:55


















          • Wow, thank you for your response! I've been reading through this for a while and I am looking forward to implementing this when I get home. Thank you. You make a point about something more complicated though, what WOULD I do if my distribution centres had capacity requirements? That seems more like real-life. Would I go about defining a DistroMax = [73,70,25] (any combination that sums the demand really) then adding a constraint like m.addConstr(quicksum((DistroMax[d] - Demand[d])*X[d,s] for d in Distro) >= 0)? Actually I'm not sure if that'd work or change the model at all

            – 99 Fishing
            Mar 26 at 2:03












          • If you ever come back to this thread would love to hear your thoughts on what you proposed in your answer. I might be on the right track but I think I would still get the same answer.

            – 99 Fishing
            Mar 26 at 2:04












          • If there were capacity constraints on the distribution centers, I would use integer variables instead of binaries (if the goods can only be delivered in discrete quantities, otherwise continuous ones). Then you need to make sure that the capacity constraints are satisfied in the distribution centers (quicksum(X[d, s] for s in Stores) <= DistroMax[d]) and that the requirements are met in the stores (quicksum(X[d, s] for d in Distro) = Demand[s]). The objective would become quicksum(Costs[d][s] * X[d, s] for d in Distro for s in Stores).

            – Silke Horn
            Mar 28 at 17:55

















          Wow, thank you for your response! I've been reading through this for a while and I am looking forward to implementing this when I get home. Thank you. You make a point about something more complicated though, what WOULD I do if my distribution centres had capacity requirements? That seems more like real-life. Would I go about defining a DistroMax = [73,70,25] (any combination that sums the demand really) then adding a constraint like m.addConstr(quicksum((DistroMax[d] - Demand[d])*X[d,s] for d in Distro) >= 0)? Actually I'm not sure if that'd work or change the model at all

          – 99 Fishing
          Mar 26 at 2:03






          Wow, thank you for your response! I've been reading through this for a while and I am looking forward to implementing this when I get home. Thank you. You make a point about something more complicated though, what WOULD I do if my distribution centres had capacity requirements? That seems more like real-life. Would I go about defining a DistroMax = [73,70,25] (any combination that sums the demand really) then adding a constraint like m.addConstr(quicksum((DistroMax[d] - Demand[d])*X[d,s] for d in Distro) >= 0)? Actually I'm not sure if that'd work or change the model at all

          – 99 Fishing
          Mar 26 at 2:03














          If you ever come back to this thread would love to hear your thoughts on what you proposed in your answer. I might be on the right track but I think I would still get the same answer.

          – 99 Fishing
          Mar 26 at 2:04






          If you ever come back to this thread would love to hear your thoughts on what you proposed in your answer. I might be on the right track but I think I would still get the same answer.

          – 99 Fishing
          Mar 26 at 2:04














          If there were capacity constraints on the distribution centers, I would use integer variables instead of binaries (if the goods can only be delivered in discrete quantities, otherwise continuous ones). Then you need to make sure that the capacity constraints are satisfied in the distribution centers (quicksum(X[d, s] for s in Stores) <= DistroMax[d]) and that the requirements are met in the stores (quicksum(X[d, s] for d in Distro) = Demand[s]). The objective would become quicksum(Costs[d][s] * X[d, s] for d in Distro for s in Stores).

          – Silke Horn
          Mar 28 at 17:55






          If there were capacity constraints on the distribution centers, I would use integer variables instead of binaries (if the goods can only be delivered in discrete quantities, otherwise continuous ones). Then you need to make sure that the capacity constraints are satisfied in the distribution centers (quicksum(X[d, s] for s in Stores) <= DistroMax[d]) and that the requirements are met in the stores (quicksum(X[d, s] for d in Distro) = Demand[s]). The objective would become quicksum(Costs[d][s] * X[d, s] for d in Distro for s in Stores).

          – Silke Horn
          Mar 28 at 17:55




















          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%2f55331146%2fhow-to-implement-a-cost-minimization-objective-function-correctly-in-gurobi%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript