title: How to model consecutive location in opl integer programs?
body: <p>Can someone model this MIP problem? I could not think of a way to model the constraint &quot;all balls of the same color has to be in consecutive locations&quot;</p>
  <p>Problem description:</p>
  <pre><code>There are 10 locations: 1 to 10, loc 1 worth $1, loc 2 worth $2 ... loc 10 worth $10
  There are 10 balls: b1 to b10
  There are 3 colors: red, green, blue. red worth $3, green worth $2, blue worth $1
  
  assign each ball to a color and a distinctive location so that:
  
  all balls of the same color are next to each other in consecutive locations. for example, all red balls take location 4,5,6 is ok, but not 4,5,8
  no color can be assigned to more than 4 balls
  each color has to be assigned to at least 1 ball
  
  ball in a location is worth $color * $loc, for example, red ball in loc 5 = $3 * $5 = $15
  
  maximize total worth of the assignment 
  </code></pre>
  <p>Obviously the solution is: blue, blue, green, green, green, green, red, red, red, red</p>
  <p>This is an illustrative problem to see how to model consecutive constraint in mip.</p>
  <p>I have following opl program without even the neighboring constraint, it takes very long, for such a small problem, what did I do wrong?</p>
  <pre><code>range locations = 1..10;
  int nbcolors=3;
  int reward[l in locations]=l;
  range colors=1..nbcolors;
  string colorNames[colors]=[&quot;blue&quot;,&quot;green&quot;,&quot;red&quot;];
  
  dvar boolean assign[colors][locations];
  
  dexpr int color[l in locations] = sum(c in colors) assign[c][l] * c;
  dexpr int totalworth=sum(l in locations) color[l]*reward[l];
  maximize totalworth;
  subject to
  {
    //each location assigned once
    forall(l in locations) sum(c in colors) assign[c][l] == 1;
    // no color can be assigned to more than 4 balls
    forall(c in colors) sum(l in locations) assign[c][l] &lt;= 4;
    //each color has to be assigned to at least 1 ball
    forall(c in colors) sum(l in locations) assign[c][l] &gt;= 1;
  }
  </code></pre>
  <p>Thanks for your help.</p>