[CyLP] Working with matrices of variables

Haroldo Santos haroldo.santos at gmail.com
Sat Nov 25 19:47:18 EST 2017


Hi,

I'm trying to learn how to create models in CyLP.

My first attempt was to solve the n-queens puzzle.

I need a way to create a matrix of binary variables indicating if there is
a queen at each cell.

Basic constraints are those restricting one queen per row and per column.

Since the example in CyLP adds a list of variables, my first idea was to
create a list of variables and iterate some parts of this list to create
constraints. It failed because it seems and CyLP variables can't be
iterated in the same way of python lists. How can I retrieve a range of
some variables to add in constraints ? Is there a more elegant way of
implementing these simple constrains ? Thanks !

Follows my bugged example:

import numpy as np
from cylp.cy import CyClpSimplex
from cylp.py.modeling.CyLPModel import CyLPArray

n = 8

s = CyClpSimplex()

x = s.addVariable( 'x', n*n, True )

xrow = []*n
for i in range(n):
    xrow[i] = x[i*n:i*n+n]

for i in range(n):
    s += (CyLPArray([1]*n)) * xrow[i] == 1

s.writeLp('a.lp')


-- 
=============================================================
Haroldo Gambini Santos
Computing Department
Universidade Federal de Ouro Preto - UFOP
email: haroldo [at ] iceb.ufop.br
home/research page: www.decom.ufop.br/haroldo


It has long been an axiom of mine that the little things are infinitely
the most important.
-- Sir Arthur Conan Doyle, "A Case of Identity"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://list.coin-or.org/pipermail/cylp/attachments/20171125/ceccb56a/attachment.html>


More information about the CyLP mailing list