Re: bpp plus type constrain
Graciela Coitinho <[email protected]> Fri, 6 Oct 2023 17:52:55 +0200
| Newsgroups | gmane.comp.gnu.glpk |
|---|---|
| Message-ID | <CAKNVfHiS5qxjrNXvnnHrePQZVCHxnjC2mwbFf9F-hccmepiRaA@mail.gmail.com> |
--00000000000083289106070e3cd9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Por favor no me env=C3=ADen m=C3=A1s correos, no pertenezco al grupo!! El El vie, oct. 6, 2023 a la(s) 5:32 p.m., Leonardo Corato <[email protected]= m> escribi=C3=B3: > Thank you very much Michael, > and sorry for missing the maling list and sending the email directly to > you. > > I applied > s.t. fred{b in 1..n, t in mouldTypes}: sum{i in I: mo[i]=3D=3Dt} x[i, b] = <=3D 2 ; > doesn't limit the types to max 2. > > To check whether s.t. fred worked properly, I added some printf statement= s > between solve and data: > > solve; > > > printf "Min number of bins used: %d \n", obj; > for {j in J: sum{i in I} x[i,j] !=3D 0} { > printf "Bin %d # items: %d - Total weight: %d - Filling %% %d \n", j= , > sum{i in I} x[i,j], sum{i in I} w[i] * x[i,j], sum{i in I} w[i] * x[i,j] = / > cmax * 100 ; > printf "Item Batch Alloy Mould\n"; > for {i in I: x[i,j]=3D=3D1} { > printf "%3d %s %d %s\n",i ,ba[i] ,al[i],mo[i]; > } > printf "mold types: %d\n", card(setof{i in I: x[i,j]=3D=3D1} mo[i= ] ); > > > } > > > data; > > > When run, I got: > > Min number of bins used: 4 > Bin 1 # items: 4 - Total weight: 254 - Filling % 85 > Item Batch Alloy Mould > 1 H27587V 2502 P27795 > 2 H27587V 2502 P27795 > 9 H27587V 2502 P27532 > 10 H27587V 2502 P27532 > mold types: 2 > Bin 2 # items: 5 - Total weight: 288 - Filling % 96 > Item Batch Alloy Mould > 5 H27587V 2502 P12396 > 6 H27587V 2502 P12396 > 11 H27587V 2502 P27532 > 12 H27587V 2502 P27532 > 15 H27587V 2502 P35495 > mold types: 3 > Bin 3 # items: 4 - Total weight: 290 - Filling % 97 > Item Batch Alloy Mould > 3 H27587V 2502 P27795 > 4 H27587V 2502 P27795 > 13 H27587V 2502 P27532 > 14 H27587V 2502 P35495 > mold types: 3 > Bin 4 # items: 3 - Total weight: 204 - Filling % 68 > Item Batch Alloy Mould > 7 H27587V 2502 P12396 > 8 H27587V 2502 P12396 > 16 H27587V 2502 P35495 > mold types: 2 > Model has been successfully processed > > As you can see in bin 2 and 3 there are 3 mold types, so sadly s.t. fred > is not limiting to 2 > I also tried > s.t. cardset{j in J}: card(setof{i in I: x[i,j] =3D=3D 1} mo[i] ) <=3D 2; > which is the same I used in printfs to check how many type of items are > in every bin j, but it seems that this expression can't be used inside an > s.t. > > > Il giorno gio 5 ott 2023 alle ore 21:28 Michael Hennebry < > [email protected]> ha scritto: > >> I'm assuming the your e-mail was intended for the list. >> I'm quoting most of it because the list does not have it. >> >> In either case, forget about what I wrote. >> GMPL is more powerful than I remembered. >> >> I think the desired constraint is >> fred{b in 1..n, t in mouldTypes} sum{i in I: mo[i]=3D=3Dt} x[i, b] <=3D = 2 ; >> The above two lines are duplicated at an appropriate point. >> I did not add anything else. >> >> On Thu, 5 Oct 2023, Leonardo Corato wrote: >> >> > Thanks Michael, I understand. >> > But what it is not clear to me is once I have the type and numbers fo= r >> > type how to do it: >> > let's say there's this *data1.csv* having: >> > >> > ITEM,WEIGHT,ALLOY,BATCH,MOULD >> > 1,85,2502,H27587V,P27795 >> > 2,85,2502,H27587V,P27795 >> > 3,85,2502,H27587V,P27795 >> > 4,85,2502,H27587V,P27795 >> > 5,63,2502,H27587V,P12396 >> > 6,63,2502,H27587V,P12396 >> > 7,63,2502,H27587V,P12396 >> > 8,63,2502,H27587V,P12396 >> > 9,42,2502,H27587V,P27532 >> > 10,42,2502,H27587V,P27532 >> > 11,42,2502,H27587V,P27532 >> > 12,42,2502,H27587V,P27532 >> > 13,42,2502,H27587V,P27532 >> > 14,78,2502,H27587V,P35495 >> > 15,78,2502,H27587V,P35495 >> > 16,78,2502,H27587V,P35495 >> > >> > >> > where WEIGHT is the weight I'll use in the bpp example of Andrew >> Makhorin, >> > alloy and batch are descriptive and mould is the type. >> > >> > My code is: >> > set I; >> > param w{it in I}, > 0; # Weight of item i (w[i]) >> > param al{it in I}; # Alloy >> > param ba{it in I} symbolic; # Batch >> > param mo{it in I} symbolic; # Mould >> > table tab_items IN "CSV" "data1.csv" : >> > I <- [ITEM], w ~ WEIGHT, al ~ ALLOY, ba ~ BATCH, mo ~ MOULD; >> > >> > printf{it in I}: "%d %d %s %s \n", it,w[it],ba[it], mo[it]; >> > >> > set mouldTypes :=3D setof{it in I} mo[it]; >> > # Count the distinct molds >> > param cmt :=3D card(mouldTypes); >> > #display mouldTypes; >> > display cmt; >> > >> > #param itemCounts{ti in mouldTypes}; >> > param itemCounts{ti in mouldTypes} :=3D sum{i in I: mo[i] =3D ti} 1; >> > >> > for {ti in mouldTypes} { >> > printf "Mould Type: %s, Item Count: %d\n", ti, itemCounts[ti]; >> > } >> > >> > # total number of items >> > param m :=3D card(I),>0; >> > printf "Number of items: %d \n", m; >> > >> > # Bin capacity >> > param cmax, > 0; >> > >> > /* We need to estimate an upper bound of the number of bins sufficient >> > to contain all items. The number of items m can be used, however, it >> > is not a good idea. To obtain a more suitable estimation an easy >> > heuristic is used: we put items into a bin while it is possible, and >> > if the bin is full, we use another bin. The number of bins used in >> > this way gives us a more appropriate estimation. */ >> > >> > param z{i in I, j in 1..m} :=3D >> > # z[i,j] =3D 1 if item i is in bin j, otherwise z[i,j] =3D 0 >> > >> > if i =3D 1 and j =3D 1 then 1 >> > # put item 1 into bin 1 >> > >> > else if exists{jj in 1..j-1} z[i,jj] then 0 >> > # if item i is already in some bin, do not put it into bin j >> > >> > else if sum{ii in 1..i-1} w[ii] * z[ii,j] + w[i] > cmax then 0 >> > # if item i does not fit into bin j, do not put it into bin j >> > >> > else 1; >> > # otherwise put item i into bin j >> > >> > check{i in I}: sum{j in 1..m} z[i,j] =3D 1; >> > # each item must be exactly in one bin >> > >> > check{j in 1..m}: sum{i in I} w[i] * z[i,j] <=3D cmax; >> > # no bin must be overflowed >> > >> > param n :=3D sum{j in 1..m} if exists{i in I} z[i,j] then 1; >> > /* determine the number of bins used by the heuristic; obviously it is >> > an upper bound of the optimal solution */ >> > >> > set J :=3D 1..n; >> > # set of bins >> > >> > var x{i in I, j in J}, binary; >> > # x[i,j] =3D 1 means item i is in bin j >> >> I think the desired constraint is >> fred{b in 1..n, t in mouldTypes} sum{i in I: mo[i]=3D=3Dt} x[i, b] <=3D = 2 ; >> >> > var used{j in J}, binary; >> > # used[j] =3D 1 means bin j contains at least one item >> > >> > s.t. one{i in I}: sum{j in J} x[i,j] =3D 1; >> > # each item must be exactly in one bin >> > >> > s.t. limMax{j in J}: sum{i in I} w[i] * x[i,j] <=3D cmax * used[j]; >> > # if bin j is used, it must not be overflowed: can't exceed cmax >> > >> > minimize obj: sum{j in J} used[j]; >> > # objective is to minimize the number of bins used >> > >> > solve; >> > >> > data; >> > param cmax :=3D 300; # max weight >> > >> > end; >> > >> > So now I can see that I have 4 type of moulds, >> > cmt =3D 4 >> > And each one ha n items:Mould Type: P27795, Item Count: 4 >> > Mould Type: P12396, Item Count: 4 >> > Mould Type: P27532, Item Count: 5 >> > Mould Type: P35495, Item Count: 3 >> > For a total of items: >> > Number of items: 16 >> > >> > So now I can say I have the params for type: >> > ti, #typeitemCounts[ti] # number of items for >> that >> > type. >> > >> > Now, what it is not clear to me is: if I want max 2 types of n items f= or >> > each bin how to achieve this goal. >> > >> > e.g in the same bin I can have 2 of P27795 and 2 of P12396 because th= e >> > weight is : 85*2+63*2=3D296<300 and cardinality of #(P27795, P12396) i= s 2 >> > but I can't have 1 of P12396 and 1 of P12396 and 1 of P27532 >> because >> > the weight is 85+63+42=3D190<300, fine, but the cardinality of #(P2779= 5, >> > P12396, ) is 3 >> > >> > I tried adding a variable t[ti,j] like Makhorin did for z[i,j] , in >> this >> > case meaning 1 when there's a mould type ti in bin j ....but type must >> be >> > linked to i, because each item can be of just 1 mould type. >> > So I tried with a t[ti,i], but in this case it is not bound to bin an= d >> I >> > need this. >> > So I asked me if I just had to add ti as a third variable in z[i,j,ti] >> but >> > in this latter I would get that i can be of each type while each i is >> just >> > one type. >> > Any tips? >> > >> > Il giorno mer 4 ott 2023 alle ore 20:08 Michael Hennebry < >> > [email protected]> ha scritto: >> > >> >> On Sat, 30 Sep 2023, Leonardo Corato wrote: >> >> >> >>> param m :=3D 6; >> >>> param w :=3D 1 50, 2 60, 3 30, 4 40, 5 40, 6 40; >> >>> --> param t :=3D 1 A, 2 B , 3 B, 4 C, 5 C, 6 C; >> >>> param c :=3D 100; >> >>> >> >>> end; >> >>> >> >>> I have to add a constraint so that the number of types for every bin >> is >> >>> limited to maximum 2. >> >>> >> >>> Each bin can contain a number of the same type of items (i.e. A) or >> max 2 >> >>> different types (i.e. A and C). >> >>> it is not regarding the number of items, of course, I can have >> multiple >> >>> items. > > >> >> -- >> Michael [email protected] >> "Occasionally irrational explanations are required" -- Luke Roman >> > --00000000000083289106070e3cd9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"auto">Por favor no me env=C3=ADen m=C3=A1s correos, no pertenez= co al grupo!!</div><div><br><div class=3D"gmail_quote"><div dir=3D"ltr" cla= ss=3D"gmail_attr">El El vie, oct. 6, 2023 a la(s) 5:32 p.m., Leonardo Corat= o <<a href=3D"mailto:[email protected]">[email protected]</a>> escribi= =C3=B3:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px = 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;bo= rder-left-color:rgb(204,204,204)"><div dir=3D"ltr"><div>Thank you very much= Michael,</div><div>and sorry for missing the maling list and sending the e= mail directly to you.</div><div><br></div><div>I applied=C2=A0<br></div><di= v><font face=3D"monospace" style=3D"font-family:monospace;color:rgb(0,0,0)"= >s.t. fred{b in 1..n, t in mouldTypes}: sum{i in I: mo[i]=3D=3Dt} x[i, b] &= lt;=3D 2 ;</font><br></div><div>doesn't limit the types to max 2.<br></= div><div><br></div><div>To check whether s.t. fred worked properly, I added= some printf statements between solve and data:<br></div><div><br></div><bl= ockquote style=3D"margin:0px 0px 0px 40px;border:medium;padding:0px"><div><= font face=3D"monospace" style=3D"font-family:monospace;color:rgb(0,0,0)">so= lve;</font></div></blockquote><div><font face=3D"monospace" style=3D"font-f= amily:monospace;color:rgb(0,0,0)"><br></font></div><blockquote style=3D"mar= gin:0px 0px 0px 40px;border:medium;padding:0px"><div><font face=3D"monospac= e" style=3D"font-family:monospace;color:rgb(0,0,0)">printf "Min number= of bins used: %d \n", obj;</font></div><div><font face=3D"monospace" = style=3D"font-family:monospace;color:rgb(0,0,0)">for {j in J: sum{i in I} x= [i,j] !=3D 0} {</font></div><div><font face=3D"monospace" style=3D"font-fam= ily:monospace;color:rgb(0,0,0)">=C2=A0 =C2=A0 printf "Bin %d # items: = %d - Total weight: %d - Filling %% %d =C2=A0\n", j, sum{i in I} x[i,j]= , sum{i in I} w[i] * x[i,j], sum{i in I} w[i] * x[i,j] / cmax * 100 ;</font= ></div><div><font face=3D"monospace" style=3D"font-family:monospace;color:r= gb(0,0,0)">=C2=A0 =C2=A0 printf "Item =C2=A0 =C2=A0 Batch =C2=A0 =C2= =A0 =C2=A0Alloy =C2=A0 Mould\n";</font></div><div><font face=3D"monosp= ace" style=3D"font-family:monospace;color:rgb(0,0,0)">=C2=A0 =C2=A0 for {i = in I: x[i,j]=3D=3D1} { </font></div><div><font face=3D"monospace" style=3D"= font-family:monospace;color:rgb(0,0,0)">=C2=A0 =C2=A0 =C2=A0 =C2=A0 printf = "%3d =C2=A0 =C2=A0 =C2=A0%s =C2=A0 =C2=A0%d =C2=A0%s\n",i ,ba[i] = ,al[i],mo[i];</font></div><div><font face=3D"monospace" style=3D"font-famil= y:monospace;color:rgb(0,0,0)">=C2=A0 =C2=A0 }=C2=A0 =C2=A0 =C2=A0 =C2=A0=C2= =A0</font></div><div><font face=3D"monospace" style=3D"font-family:monospac= e;color:rgb(0,0,0)">=C2=A0 =C2=A0 =C2=A0 =C2=A0 printf "mold types: %d= \n", card(setof{i in I: x[i,j]=3D=3D1} mo[i] );</font></div></blockquo= te><div><font face=3D"monospace" style=3D"font-family:monospace;color:rgb(0= ,0,0)"><br></font></div><blockquote style=3D"margin:0px 0px 0px 40px;border= :medium;padding:0px"><div><font face=3D"monospace" style=3D"font-family:mon= ospace;color:rgb(0,0,0)">}</font></div></blockquote><div><font face=3D"mono= space" style=3D"font-family:monospace;color:rgb(0,0,0)"><br></font></div><b= lockquote style=3D"margin:0px 0px 0px 40px;border:medium;padding:0px"><div>= <font face=3D"monospace" style=3D"font-family:monospace;color:rgb(0,0,0)">d= ata;</font></div></blockquote><div><br></div><div>When run, I got:</div><di= v><br></div><div><font face=3D"monospace" style=3D"font-family:monospace;co= lor:rgb(0,0,0)"><span style=3D"font-family:monospace;color:rgb(0,0,0)">Min = number of bins used: 4 =C2=A0</span><br>Bin 1 # items: 4 - Total weight: 25= 4 - Filling % 85 =C2=A0=C2=A0<br>Item =C2=A0=C2=A0=C2=A0=C2=A0Batch =C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0Alloy =C2=A0=C2=A0Mould <br> =C2=A01 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 = =C2=A0P27795 <br> =C2=A02 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 = =C2=A0P27795 <br> =C2=A09 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 = =C2=A0P27532 <br> 10 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 =C2=A0= P27532 <br>mold types: 2 <br>Bin 2 # items: 5 - Total weight: 288 - Filling % 96 =C2=A0=C2=A0<br>Ite= m =C2=A0=C2=A0=C2=A0=C2=A0Batch =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Alloy =C2=A0= =C2=A0Mould <br> =C2=A05 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 = =C2=A0P12396 <br> =C2=A06 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 = =C2=A0P12396 <br> 11 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 =C2=A0= P27532 <br> 12 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 =C2=A0= P27532 <br> 15 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 =C2=A0= P35495 <br>mold types: 3 <br>Bin 3 # items: 4 - Total weight: 290 - Filling % 97 =C2=A0=C2=A0<br>Ite= m =C2=A0=C2=A0=C2=A0=C2=A0Batch =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Alloy =C2=A0= =C2=A0Mould <br> =C2=A03 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 = =C2=A0P27795 <br> =C2=A04 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 = =C2=A0P27795 <br> 13 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 =C2=A0= P27532 <br> 14 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 =C2=A0= P35495 <br>mold types: 3 <br>Bin 4 # items: 3 - Total weight: 204 - Filling % 68 =C2=A0=C2=A0<br>Ite= m =C2=A0=C2=A0=C2=A0=C2=A0Batch =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Alloy =C2=A0= =C2=A0Mould <br> =C2=A07 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 = =C2=A0P12396 <br> =C2=A08 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 = =C2=A0P12396 <br> 16 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0H27587V =C2=A0=C2=A0=C2=A02502 =C2=A0= P35495 <br>mold types: 2 <br>Model has been successfully processed</font><br></div><div><br></div><d= iv>As you can see in bin 2 and 3 there are 3 mold types, so sadly s.t. fred= is not limiting to 2</div><div>I also tried=C2=A0<br><font face=3D"monospa= ce" style=3D"font-family:monospace;color:rgb(0,0,0)">s.t. cardset{j in J}: = card(setof{i in I: x[i,j] =3D=3D 1} mo[i] ) <=3D 2;</font></div><div>whi= ch is the same=C2=A0 I used in printfs to check how many type of items are = in every bin j, but it seems that this expression can't be used inside = an s.t.=C2=A0</div><div><br></div><br><div class=3D"gmail_quote"><div dir= =3D"ltr" class=3D"gmail_attr">Il giorno gio 5 ott 2023 alle ore 21:28 Micha= el Hennebry <<a href=3D"mailto:[email protected]" target=3D= "_blank">[email protected]</a>> ha scritto:<br></div><block= quote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-w= idth:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204= ,204,204)">I'm assuming the your e-mail was intended for the list.<br> I'm quoting most of it because the list does not have it.<br> <br> In either case, forget about what I wrote.<br> GMPL is more powerful than I remembered.<br> <br> I think the desired constraint is<br> fred{b in 1..n, t in mouldTypes} sum{i in I: mo[i]=3D=3Dt} x[i, b] <=3D = 2 ;<br> The above two lines are duplicated at an appropriate point.<br> I did not add anything else.<br> <br> On Thu, 5 Oct 2023, Leonardo Corato wrote:<br> <br> > Thanks Michael, I understand.<br> > But what it is not clear to me is once I have=C2=A0 the type and numbe= rs for<br> > type how to do it:<br> > let's say there's this *data1.csv* having:<br> ><br> > ITEM,WEIGHT,ALLOY,BATCH,MOULD<br> > 1,85,2502,H27587V,P27795<br> > 2,85,2502,H27587V,P27795<br> > 3,85,2502,H27587V,P27795<br> > 4,85,2502,H27587V,P27795<br> > 5,63,2502,H27587V,P12396<br> > 6,63,2502,H27587V,P12396<br> > 7,63,2502,H27587V,P12396<br> > 8,63,2502,H27587V,P12396<br> > 9,42,2502,H27587V,P27532<br> > 10,42,2502,H27587V,P27532<br> > 11,42,2502,H27587V,P27532<br> > 12,42,2502,H27587V,P27532<br> > 13,42,2502,H27587V,P27532<br> > 14,78,2502,H27587V,P35495<br> > 15,78,2502,H27587V,P35495<br> > 16,78,2502,H27587V,P35495<br> ><br> ><br> > where WEIGHT is the weight I'll use in the bpp example of Andrew M= akhorin,<br> > alloy and batch are descriptive and mould is=C2=A0 the type.<br> ><br> > My code is:<br> > set I;<br> > param w{it in I}, > 0;=C2=A0 =C2=A0 =C2=A0 =C2=A0 # Weight of item = i (w[i])<br> > param al{it in I};=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 # Alloy<br= > > param ba{it in I} symbolic;=C2=A0 =C2=A0# Batch<br> > param mo{it in I} symbolic;=C2=A0 =C2=A0# Mould<br> > table tab_items IN "CSV" "data1.csv" :<br> > I <- [ITEM], w ~ WEIGHT, al ~ ALLOY, ba ~ BATCH, mo ~ MOULD;<br> ><br> > printf{it in I}: "%d %d %s %s \n", it,w[it],ba[it], mo[it];<= br> ><br> > set mouldTypes :=3D setof{it in I} mo[it];<br> > # Count the distinct molds<br> > param cmt :=3D card(mouldTypes);<br> > #display mouldTypes;<br> > display cmt;<br> ><br> > #param itemCounts{ti in mouldTypes};<br> > param itemCounts{ti in mouldTypes} :=3D sum{i in I: mo[i] =3D ti} 1;<b= r> ><br> > for {ti in mouldTypes} {<br> >=C2=A0 =C2=A0printf "Mould Type: %s, Item Count: %d\n", ti, i= temCounts[ti];<br> > }<br> ><br> > # total number of items<br> > param m :=3D card(I),>0;<br> > printf "Number of items: %d \n", m;<br> ><br> > # Bin capacity<br> > param cmax, > 0;<br> ><br> > /* We need to estimate an upper bound of the number of bins sufficient= <br> >=C2=A0 to contain all items. The number of items m can be used, however= , it<br> >=C2=A0 is not a good idea. To obtain a more suitable estimation an easy= <br> >=C2=A0 heuristic is used: we put items into a bin while it is possible,= and<br> >=C2=A0 if the bin is full, we use another bin. The number of bins used = in<br> >=C2=A0 this way gives us a more appropriate estimation. */<br> ><br> > param z{i in I, j in 1..m} :=3D<br> > # z[i,j] =3D 1 if item i is in bin j, otherwise z[i,j] =3D 0<br> ><br> >=C2=A0 if i =3D 1 and j =3D 1 then 1<br> >=C2=A0 # put item 1 into bin 1<br> ><br> >=C2=A0 else if exists{jj in 1..j-1} z[i,jj] then 0<br> >=C2=A0 # if item i is already in some bin, do not put it into bin j<br> ><br> >=C2=A0 else if sum{ii in 1..i-1} w[ii] * z[ii,j] + w[i] > cmax then = 0<br> >=C2=A0 # if item i does not fit into bin j, do not put it into bin j<br= > ><br> >=C2=A0 else 1;<br> >=C2=A0 # otherwise put item i into bin j<br> ><br> > check{i in I}: sum{j in 1..m} z[i,j] =3D 1;<br> > # each item must be exactly in one bin<br> ><br> > check{j in 1..m}: sum{i in I} w[i] * z[i,j] <=3D cmax;<br> > # no bin must be overflowed<br> ><br> > param n :=3D sum{j in 1..m} if exists{i in I} z[i,j] then 1;<br> > /* determine the number of bins used by the heuristic; obviously it is= <br> >=C2=A0 an upper bound of the optimal solution */<br> ><br> > set J :=3D 1..n;<br> > # set of bins<br> ><br> > var x{i in I, j in J}, binary;<br> > # x[i,j] =3D 1 means item i is in bin j<br> <br> I think the desired constraint is<br> fred{b in 1..n, t in mouldTypes} sum{i in I: mo[i]=3D=3Dt} x[i, b] <=3D = 2 ;<br> <br> > var used{j in J}, binary;<br> > # used[j] =3D 1 means bin j contains at least one item<br> ><br> > s.t. one{i in I}: sum{j in J} x[i,j] =3D 1;<br> > # each item must be exactly in one bin<br> ><br> > s.t. limMax{j in J}: sum{i in I} w[i] * x[i,j] <=3D cmax * used[j];= <br> > # if bin j is used, it must not be overflowed:=C2=A0 can't exceed = cmax<br> ><br> > minimize obj: sum{j in J} used[j];<br> > # objective is to minimize the number of bins used<br> ><br> > solve;<br> ><br> > data;<br> > param cmax :=3D 300;=C2=A0 # max weight<br> ><br> > end;<br> ><br> > So now I can see that I have 4 type of moulds,<br> > cmt =3D 4<br> > And each one=C2=A0 ha n items:Mould Type: P27795, Item Count: 4<br> > Mould Type: P12396, Item Count: 4<br> > Mould Type: P27532, Item Count: 5<br> > Mould Type: P35495, Item Count: 3<br> > For a total of items:<br> > Number of items: 16<br> ><br> > So now I can say I have the params for type:<br> > ti,=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0#typeitemCounts[ti]=C2=A0 =C2=A0# number of items for that= <br> > type.<br> ><br> > Now, what it is not clear to me is: if I want max 2 types of n items f= or<br> > each bin how to achieve this goal.<br> ><br> > e.g in=C2=A0 the same bin I can have 2 of P27795 and 2 of P12396 becau= se the<br> > weight is : 85*2+63*2=3D296<300 and cardinality of #(P27795, P12396= ) is 2<br> >=C2=A0 =C2=A0 =C2=A0 but I can't have 1 of P12396 and 1 of P12396 a= nd 1 of P27532 because<br> > the weight is 85+63+42=3D190<300, fine, but the cardinality of #(P2= 7795,<br> > P12396, ) is 3<br> ><br> > I tried adding a variable t[ti,j]=C2=A0 like Makhorin did for z[i,j] ,= in this<br> > case meaning 1 when there's a mould type ti in bin j ....but type = must be<br> > linked to i, because each item can be of just 1 mould type.<br> > So I tried with a=C2=A0 t[ti,i], but in this case it is not bound to b= in and I<br> > need this.<br> > So I asked me if I just had to add ti as a third variable in z[i,j,ti]= but<br> > in this latter I would get that i can be of each type while each i is = just<br> > one type.<br> > Any tips?<br> ><br> > Il giorno mer 4 ott 2023 alle ore 20:08 Michael Hennebry <<br> > <a href=3D"mailto:[email protected]" target=3D"_blank">he= [email protected]</a>> ha scritto:<br> ><br> >> On Sat, 30 Sep 2023, Leonardo Corato wrote:<br> >><br> >>> param m :=3D 6;<br> >>> param w :=3D=C2=A0 =C2=A0 =C2=A01 50, 2 60, 3 30, 4 40, 5 40, = 6 40;<br> >>> --> param t :=3D 1 A,=C2=A0 2 B , 3 B, 4 C, 5 C, 6 C;<br> >>> param c :=3D 100;<br> >>><br> >>> end;<br> >>><br> >>> I have to add a constraint so that the number of types for eve= ry bin is<br> >>> limited to maximum 2.<br> >>><br> >>> Each bin can contain a number of the same type of items (i.e. = A) or max 2<br> >>> different types (i.e. A and C).<br> >>> it is not regarding the number of items, of course, I can have= multiple<br> >>> items.</blockquote></div></div><div dir=3D"ltr"><div class=3D"= gmail_quote"><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px = 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border= -left-color:rgb(204,204,204)"><br> <br> -- <br> Michael=C2=A0 =C2=A0<a href=3D"mailto:[email protected]" targ= et=3D"_blank">[email protected]</a><br> "Occasionally irrational explanations are required"=C2=A0 --=C2= =A0 Luke Roman<br> </blockquote></div></div> </blockquote></div></div> --00000000000083289106070e3cd9--