Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90646 > unrolled thread

Python: How to find out values of all feasible x under constraints.

Started byXiang Zhang <zhangxiangsunny@gmail.com>
First post2015-05-14 20:58 -0700
Last post2015-05-16 01:16 +0100
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Python: How to find out values of all feasible x under constraints. Xiang Zhang <zhangxiangsunny@gmail.com> - 2015-05-14 20:58 -0700
    Re: Python: How to find out values of all feasible x under constraints. Peter Otten <__peter__@web.de> - 2015-05-15 09:47 +0200
    Re: Python: How to find out values of all feasible x under constraints. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-15 13:35 +0100
    Re: Python: How to find out values of all feasible x under constraints. Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-16 01:16 +0100

#90646 — Python: How to find out values of all feasible x under constraints.

FromXiang Zhang <zhangxiangsunny@gmail.com>
Date2015-05-14 20:58 -0700
SubjectPython: How to find out values of all feasible x under constraints.
Message-ID<12da2c70-fd4e-430b-83d2-fad4ac1e3f8f@googlegroups.com>
Dear all,

I am writing a code using Python now.

I want to know how to find out values of all feasible x under constraints.

x = [x_1, x_2, x_3,..., x_10]


constraints:
x_i = 0,1,2,3 or 4,          where i=1,2,....10
x_1 + x_2 + x_3 +...+x_10 <= 15

How to find out all the feasible solutions x (domain of x) using python, like [0,0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1,1] etc ? What should be the code?

Any hint or help would be highly appreciated!

Sincerely,

Xiang Zhang

[toc] | [next] | [standalone]


#90655

FromPeter Otten <__peter__@web.de>
Date2015-05-15 09:47 +0200
Message-ID<mailman.30.1431676060.17265.python-list@python.org>
In reply to#90646
Xiang Zhang wrote:

> I want to know how to find out values of all feasible x under constraints.
> 
> x = [x_1, x_2, x_3,..., x_10]
> 
> 
> constraints:
> x_i = 0,1,2,3 or 4,          where i=1,2,....10
> x_1 + x_2 + x_3 +...+x_10 <= 15

That are 5**10 == 9765625 candidates. That's still feasible to check using 
brute force.

> How to find out all the feasible solutions x (domain of x) using python,
> like [0,0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1,1] etc ? What should be the
> code?
> 
> Any hint or help would be highly appreciated!

>>> solutions = [x for x in itertools.product(range(5), repeat=10) if sum(x) 
<= 15]
>>> len(solutions)
1556215

The first ten solutions:

>>> solutions[:10]
[(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 1), (0, 0, 0, 
0, 0, 0, 0, 0, 0, 2), (0, 0, 0, 0, 0, 0, 0, 0, 0, 3), (0, 0, 0, 0, 0, 0, 0, 
0, 0, 4), (0, 0, 0, 0, 0, 0, 0, 0, 1, 0), (0, 0, 0, 0, 0, 0, 0, 0, 1, 1), 
(0, 0, 0, 0, 0, 0, 0, 0, 1, 2), (0, 0, 0, 0, 0, 0, 0, 0, 1, 3), (0, 0, 0, 0, 
0, 0, 0, 0, 1, 4)]

[toc] | [prev] | [next] | [standalone]


#90672

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-05-15 13:35 +0100
Message-ID<mailman.35.1431693331.17265.python-list@python.org>
In reply to#90646
On 15/05/2015 04:58, Xiang Zhang wrote:
> Dear all,
>
> I am writing a code using Python now.
>
> I want to know how to find out values of all feasible x under constraints.
>
> x = [x_1, x_2, x_3,..., x_10]
>
>
> constraints:
> x_i = 0,1,2,3 or 4,          where i=1,2,....10
> x_1 + x_2 + x_3 +...+x_10 <= 15
>
> How to find out all the feasible solutions x (domain of x) using python, like [0,0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1,1] etc ? What should be the code?
>
> Any hint or help would be highly appreciated!
>
> Sincerely,
>
> Xiang Zhang
>

There are several constraint libraries on pypi if you don't want to roll 
your own.  See for example 
https://pypi.python.org/pypi/python-constraint/1.2

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#90700

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-05-16 01:16 +0100
Message-ID<mailman.52.1431735384.17265.python-list@python.org>
In reply to#90646
On 15/05/2015 13:35, Mark Lawrence wrote:
> On 15/05/2015 04:58, Xiang Zhang wrote:
>> Dear all,
>>
>> I am writing a code using Python now.
>>
>> I want to know how to find out values of all feasible x under
>> constraints.
>>
>> x = [x_1, x_2, x_3,..., x_10]
>>
>>
>> constraints:
>> x_i = 0,1,2,3 or 4,          where i=1,2,....10
>> x_1 + x_2 + x_3 +...+x_10 <= 15
>>
>> How to find out all the feasible solutions x (domain of x) using
>> python, like [0,0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1,1] etc ? What
>> should be the code?
>>
>> Any hint or help would be highly appreciated!
>>
>> Sincerely,
>>
>> Xiang Zhang
>>
>
> There are several constraint libraries on pypi if you don't want to roll
> your own.  See for example
> https://pypi.python.org/pypi/python-constraint/1.2
>

Forgot to mention this http://numberjack.ucc.ie/

Maybe of general interest see also http://www.csplib.org/ 
http://www.hakank.org/constraint_programming_blog/



-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web