Writing an optimization constraint in Python












0















I am new to doing optimization with Python and Gurobi. I have tried to code the constraint



f_{s}^{E}=x_{i}sum_{1}^{i-1}ALW_{js},forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


Where A_{s}^{E} and x_{i} are variables.
To calculate ALW_{js} we need to read the upper triangle of a distance matrix and then sort the distances, d_{ij} for i,jinC and j>i, descending. The sorted result can be represented as follows:



d_{1} geqslant d_{2} geqslant ... geqslant d_{frac{c(c-1)}{2}}


Where d_{1}=max⁡{d_{ij}} and d_{frac{c(c-1)}{2}=min{d_{ij}}.
Each sorted distance above, meaning d_{1}, d_{2}, ..., d_{frac{c(c-1)}{2}}, has a corresponding value in scenario s



ALN_{ijs}=(1-0.05)(1-0.72) max left { AN_{is},AN{js} right }, forall sin S,forall i,jin C and j>i


Where AN_{is} is read from a file. ALN_{ijs} can be written



ALW_{js}, forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


My codes for this are below. The Gurobi provides the solution, but it says the constraint



f_{s}^{E}=x_{i}sum_{1}^{i-1}ALW_{js},forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


Is quadratic, which is not really. I would appreciate if anybody can guide me.



x={}
fE={}
for s in range (S):
fE[s]=Z.addVar(lb=0, vtype=GRB.CONTINUOUS, name='fE%s'%(s))

DtN={}
with open('Distances.csv', 'rU') as file:
table = [row for row in csv.reader(file)]
for i in range (C):
for j in range (C):
if j>i:
DtN[i,j]=round(-1*float(table[i][j]),2)
SortDis=
SortDisKey=
for key, value in sorted(DtN.iteritems(), key=lambda (k,v): (v,k)):
SortDis.append(abs(value))
SortDisKey.append(key)
for i in range (len(DtN)):
x[i]=Z.addVar(lb=0, vtype=GRB.BINARY, name='x%s'%(i))

with open('Feed1.txt', 'r') as Fee:
for i in range(C):
Feed= round(float(Fee.readline()),3)
for s in L11:
AN[i,s]=round(Feed/10**6,9)
for s in L12:
AN[i,s] = round(Feed*1.28/10**6,9)
for s in L13:
AN[i,s] = round(Feed*0.95/10**6,9)
ALN={}
for s in range (S):
ALN[s]={}

for s in range(S):
for i in range(C):
for j in range(C):
if j>i:
ALN[s][i,j]=max((1-0.05)*(1-0.72)*AN[i,s],(1-0.05)*(1-0.72)*AN[j,s])
ALW={}
for s in range (S):
ALW[s]=

for s in range (S):
for j in SortDisKey:
ALW[s].append(ALN[s][j])
for s in range (S):
for i in range (len(DtN)):
Z.addConstr(fE[s]>=(x[i]*(quicksum(ALW[s][j] for j in range (0,i-1)))), name='N3%s%s'%(s,i))









share|improve this question



























    0















    I am new to doing optimization with Python and Gurobi. I have tried to code the constraint



    f_{s}^{E}=x_{i}sum_{1}^{i-1}ALW_{js},forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


    Where A_{s}^{E} and x_{i} are variables.
    To calculate ALW_{js} we need to read the upper triangle of a distance matrix and then sort the distances, d_{ij} for i,jinC and j>i, descending. The sorted result can be represented as follows:



    d_{1} geqslant d_{2} geqslant ... geqslant d_{frac{c(c-1)}{2}}


    Where d_{1}=max⁡{d_{ij}} and d_{frac{c(c-1)}{2}=min{d_{ij}}.
    Each sorted distance above, meaning d_{1}, d_{2}, ..., d_{frac{c(c-1)}{2}}, has a corresponding value in scenario s



    ALN_{ijs}=(1-0.05)(1-0.72) max left { AN_{is},AN{js} right }, forall sin S,forall i,jin C and j>i


    Where AN_{is} is read from a file. ALN_{ijs} can be written



    ALW_{js}, forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


    My codes for this are below. The Gurobi provides the solution, but it says the constraint



    f_{s}^{E}=x_{i}sum_{1}^{i-1}ALW_{js},forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


    Is quadratic, which is not really. I would appreciate if anybody can guide me.



    x={}
    fE={}
    for s in range (S):
    fE[s]=Z.addVar(lb=0, vtype=GRB.CONTINUOUS, name='fE%s'%(s))

    DtN={}
    with open('Distances.csv', 'rU') as file:
    table = [row for row in csv.reader(file)]
    for i in range (C):
    for j in range (C):
    if j>i:
    DtN[i,j]=round(-1*float(table[i][j]),2)
    SortDis=
    SortDisKey=
    for key, value in sorted(DtN.iteritems(), key=lambda (k,v): (v,k)):
    SortDis.append(abs(value))
    SortDisKey.append(key)
    for i in range (len(DtN)):
    x[i]=Z.addVar(lb=0, vtype=GRB.BINARY, name='x%s'%(i))

    with open('Feed1.txt', 'r') as Fee:
    for i in range(C):
    Feed= round(float(Fee.readline()),3)
    for s in L11:
    AN[i,s]=round(Feed/10**6,9)
    for s in L12:
    AN[i,s] = round(Feed*1.28/10**6,9)
    for s in L13:
    AN[i,s] = round(Feed*0.95/10**6,9)
    ALN={}
    for s in range (S):
    ALN[s]={}

    for s in range(S):
    for i in range(C):
    for j in range(C):
    if j>i:
    ALN[s][i,j]=max((1-0.05)*(1-0.72)*AN[i,s],(1-0.05)*(1-0.72)*AN[j,s])
    ALW={}
    for s in range (S):
    ALW[s]=

    for s in range (S):
    for j in SortDisKey:
    ALW[s].append(ALN[s][j])
    for s in range (S):
    for i in range (len(DtN)):
    Z.addConstr(fE[s]>=(x[i]*(quicksum(ALW[s][j] for j in range (0,i-1)))), name='N3%s%s'%(s,i))









    share|improve this question

























      0












      0








      0








      I am new to doing optimization with Python and Gurobi. I have tried to code the constraint



      f_{s}^{E}=x_{i}sum_{1}^{i-1}ALW_{js},forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


      Where A_{s}^{E} and x_{i} are variables.
      To calculate ALW_{js} we need to read the upper triangle of a distance matrix and then sort the distances, d_{ij} for i,jinC and j>i, descending. The sorted result can be represented as follows:



      d_{1} geqslant d_{2} geqslant ... geqslant d_{frac{c(c-1)}{2}}


      Where d_{1}=max⁡{d_{ij}} and d_{frac{c(c-1)}{2}=min{d_{ij}}.
      Each sorted distance above, meaning d_{1}, d_{2}, ..., d_{frac{c(c-1)}{2}}, has a corresponding value in scenario s



      ALN_{ijs}=(1-0.05)(1-0.72) max left { AN_{is},AN{js} right }, forall sin S,forall i,jin C and j>i


      Where AN_{is} is read from a file. ALN_{ijs} can be written



      ALW_{js}, forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


      My codes for this are below. The Gurobi provides the solution, but it says the constraint



      f_{s}^{E}=x_{i}sum_{1}^{i-1}ALW_{js},forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


      Is quadratic, which is not really. I would appreciate if anybody can guide me.



      x={}
      fE={}
      for s in range (S):
      fE[s]=Z.addVar(lb=0, vtype=GRB.CONTINUOUS, name='fE%s'%(s))

      DtN={}
      with open('Distances.csv', 'rU') as file:
      table = [row for row in csv.reader(file)]
      for i in range (C):
      for j in range (C):
      if j>i:
      DtN[i,j]=round(-1*float(table[i][j]),2)
      SortDis=
      SortDisKey=
      for key, value in sorted(DtN.iteritems(), key=lambda (k,v): (v,k)):
      SortDis.append(abs(value))
      SortDisKey.append(key)
      for i in range (len(DtN)):
      x[i]=Z.addVar(lb=0, vtype=GRB.BINARY, name='x%s'%(i))

      with open('Feed1.txt', 'r') as Fee:
      for i in range(C):
      Feed= round(float(Fee.readline()),3)
      for s in L11:
      AN[i,s]=round(Feed/10**6,9)
      for s in L12:
      AN[i,s] = round(Feed*1.28/10**6,9)
      for s in L13:
      AN[i,s] = round(Feed*0.95/10**6,9)
      ALN={}
      for s in range (S):
      ALN[s]={}

      for s in range(S):
      for i in range(C):
      for j in range(C):
      if j>i:
      ALN[s][i,j]=max((1-0.05)*(1-0.72)*AN[i,s],(1-0.05)*(1-0.72)*AN[j,s])
      ALW={}
      for s in range (S):
      ALW[s]=

      for s in range (S):
      for j in SortDisKey:
      ALW[s].append(ALN[s][j])
      for s in range (S):
      for i in range (len(DtN)):
      Z.addConstr(fE[s]>=(x[i]*(quicksum(ALW[s][j] for j in range (0,i-1)))), name='N3%s%s'%(s,i))









      share|improve this question














      I am new to doing optimization with Python and Gurobi. I have tried to code the constraint



      f_{s}^{E}=x_{i}sum_{1}^{i-1}ALW_{js},forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


      Where A_{s}^{E} and x_{i} are variables.
      To calculate ALW_{js} we need to read the upper triangle of a distance matrix and then sort the distances, d_{ij} for i,jinC and j>i, descending. The sorted result can be represented as follows:



      d_{1} geqslant d_{2} geqslant ... geqslant d_{frac{c(c-1)}{2}}


      Where d_{1}=max⁡{d_{ij}} and d_{frac{c(c-1)}{2}=min{d_{ij}}.
      Each sorted distance above, meaning d_{1}, d_{2}, ..., d_{frac{c(c-1)}{2}}, has a corresponding value in scenario s



      ALN_{ijs}=(1-0.05)(1-0.72) max left { AN_{is},AN{js} right }, forall sin S,forall i,jin C and j>i


      Where AN_{is} is read from a file. ALN_{ijs} can be written



      ALW_{js}, forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


      My codes for this are below. The Gurobi provides the solution, but it says the constraint



      f_{s}^{E}=x_{i}sum_{1}^{i-1}ALW_{js},forall sin S,forall i={1,2,...,frac{c(c-1)}{2}}


      Is quadratic, which is not really. I would appreciate if anybody can guide me.



      x={}
      fE={}
      for s in range (S):
      fE[s]=Z.addVar(lb=0, vtype=GRB.CONTINUOUS, name='fE%s'%(s))

      DtN={}
      with open('Distances.csv', 'rU') as file:
      table = [row for row in csv.reader(file)]
      for i in range (C):
      for j in range (C):
      if j>i:
      DtN[i,j]=round(-1*float(table[i][j]),2)
      SortDis=
      SortDisKey=
      for key, value in sorted(DtN.iteritems(), key=lambda (k,v): (v,k)):
      SortDis.append(abs(value))
      SortDisKey.append(key)
      for i in range (len(DtN)):
      x[i]=Z.addVar(lb=0, vtype=GRB.BINARY, name='x%s'%(i))

      with open('Feed1.txt', 'r') as Fee:
      for i in range(C):
      Feed= round(float(Fee.readline()),3)
      for s in L11:
      AN[i,s]=round(Feed/10**6,9)
      for s in L12:
      AN[i,s] = round(Feed*1.28/10**6,9)
      for s in L13:
      AN[i,s] = round(Feed*0.95/10**6,9)
      ALN={}
      for s in range (S):
      ALN[s]={}

      for s in range(S):
      for i in range(C):
      for j in range(C):
      if j>i:
      ALN[s][i,j]=max((1-0.05)*(1-0.72)*AN[i,s],(1-0.05)*(1-0.72)*AN[j,s])
      ALW={}
      for s in range (S):
      ALW[s]=

      for s in range (S):
      for j in SortDisKey:
      ALW[s].append(ALN[s][j])
      for s in range (S):
      for i in range (len(DtN)):
      Z.addConstr(fE[s]>=(x[i]*(quicksum(ALW[s][j] for j in range (0,i-1)))), name='N3%s%s'%(s,i))






      python gurobi






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 3:58









      DavidDavid

      12




      12
























          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%2f53455039%2fwriting-an-optimization-constraint-in-python%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%2f53455039%2fwriting-an-optimization-constraint-in-python%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

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen