Change N to create a larger array. I present test results for all combinations when N is 3. I leave it to readers to convince themselves that it works for all combinations for any given N.
/* Set Z to 1 if one and only one of an array of binary variables is 1. [email protected] October 7th., 2013 */ param N := 3; var x{n in 1..N} , binary; var Q{n in 1..N} , binary; st1 : sum{n in 1..N} x[n] = sum{n in 1..N} Q[n]; st2 {n in 1..N-1} : 0 <= Q[n] - Q[n+1]; st90 : x[1] = 1; st91 : x[2] = 1; st92 : x[3] = 1; solve; printf "x[1] = %d, x[2] = %d, x[3] = %d, and Z = %d\n",x[1],x[2],x[3],Q[1] - Q[2]; end; /* Changing st90, st91 and st92 for the eight possible combinations produces: INTEGER OPTIMAL SOLUTION FOUND BY MIP PREPROCESSOR Time used: 0.0 secs Memory used: 0.1 Mb (104314 bytes) x[1] = 1, x[2] = 1, x[3] = 1, and Z = 0 Model has been successfully processed INTEGER OPTIMAL SOLUTION FOUND Time used: 0.0 secs Memory used: 0.1 Mb (119549 bytes) x[1] = 1, x[2] = 1, x[3] = 0, and Z = 0 Model has been successfully processed INTEGER OPTIMAL SOLUTION FOUND Time used: 0.0 secs Memory used: 0.1 Mb (119549 bytes) x[1] = 1, x[2] = 0, x[3] = 1, and Z = 0 Model has been successfully processed INTEGER OPTIMAL SOLUTION FOUND Time used: 0.0 secs Memory used: 0.1 Mb (119549 bytes) x[1] = 0, x[2] = 1, x[3] = 1, and Z = 0 Model has been successfully processed INTEGER OPTIMAL SOLUTION FOUND Time used: 0.0 secs Memory used: 0.1 Mb (119549 bytes) x[1] = 0, x[2] = 1, x[3] = 0, and Z = 1 Model has been successfully processed INTEGER OPTIMAL SOLUTION FOUND Time used: 0.0 secs Memory used: 0.1 Mb (119549 bytes) x[1] = 1, x[2] = 0, x[3] = 0, and Z = 1 Model has been successfully processed INTEGER OPTIMAL SOLUTION FOUND BY MIP PREPROCESSOR Time used: 0.0 secs Memory used: 0.1 Mb (104314 bytes) x[1] = 0, x[2] = 0, x[3] = 0, and Z = 0 Model has been successfully processed */ -- Nigel Galloway [email protected] On Wed, Oct 2, 2013, at 11:41 AM, Andrew Makhorin wrote: > -------- Forwarded Message -------- > From: Pietro Scionti <[email protected]> > To: [email protected] <[email protected]> > Subject: I: Modelling binary variable > Date: Wed, 2 Oct 2013 17:52:19 +0200 > > Hi Andrew, > > I sent this message to the list but I’ve never seen it appear in the > digest or the online archives. Is the address changed since the last > time I used it? > > Thank you very much > > Pietro > > > > Da: Pietro Scionti > Inviato: lunedì 23 settembre 2013 10:50 > A: '[email protected]' > Oggetto: Modelling binary variable > > > > > Hi everyone, > > I need help to model this situation: I have a series of binary decision > variables > > var x{o in O, t in T, g in G} > > and I want to introduce a new binary variable > > var z{o in O, t in T} > > which takes 1 if and only if > > sum{g in G} x[o,t,g] = 1 > > > > I tried using a Big-M approach but it only covered one of the two > implications. > > Any suggestions? > > > > Thanks, > > Pietro > > > > > ________________________________________________________________________ > > Archimede S.r.l. > Sede Legale: > Via Manzoni, 82 > Ponte S. Giovanni > > P.IVA: 01992020543 > > Tel. 075 515 22 11 > Fax. 075 515 22 99 > www.archinet.it > > ********************************************************************************************************************************************************************************************************************** > La presente comunicazione, con le informazioni in essa contenute e ogni > documento o file allegato, e' rivolta unicamente alla/e persona/e cui e' > indirizzata ed alle altre da questa autorizzata/e a riceverla. Se non > siete i destinatari/autorizzati siete avvisati che qualsiasi azione, > copia, comunicazione, divulgazione o simili basate sul contenuto di tali > informazioni e' vietata e potrebbe essere contro la legge (art. 616 > C.P., D.Lgs n. 196/2003 Codice in materia di protezione dei dati > personali). Se avete ricevuto questa comunicazione per errore, vi > preghiamo di darne immediata notizia al mittente e di distruggere il > messaggio originale e ogni file allegato senza farne copia alcuna o > riprodurne in alcun modo il contenuto. > > This e-mail and its attachments are intended for the addressee(s) only > and are confidential and/or may contain legally privileged information. > If you have received this message by mistake or are not one of the > addressees above, you may take no action based on it, and you may not > copy or show it to anyone; please reply to this e-mail and point out the > error which has occurred. > ********************************************************************************************************************************************************************************************************************** > > > > _______________________________________________ > Help-glpk mailing list > [email protected] > https://lists.gnu.org/mailman/listinfo/help-glpk -- http://www.fastmail.fm - Send your email first class _______________________________________________ Help-glpk mailing list [email protected] https://lists.gnu.org/mailman/listinfo/help-glpk
