Strange python code

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Strange python code

Post by Isaac »

Note: I'm not confused about the math. Just the way these methods are used.

Here's a minimalist example

Code: Select all

a=myclass()
x,y = a.variable(), a.variable()

a.formula ( 2*x+5*y )

a.constraint ( x>2)
a.constraint ( x<20)
a.constraint ( y >3)
what the function's going on here? We're reusing the constraint module to add more constraints? How does this not overwrite what constraint is holding? Is there a list object inside? Very confusing.

Source of my confusion: http://morepypy.blogspot.com/2011/05/pl ... -pypy.html
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6511
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re: Strange python code

Post by Jeff250 »

Note that a is an object and constraint is a method. So constraint might be doing something like appending each constraint to some constraints attribute on the object (can't know for sure without the source).

The rest looks like a clever use of operator overloading on a Variable class. In python, you can override operators like +, *, >, and < in your classes. So here instead of doing some kind of arithmetic on the operands, the operators over the Variable class are probably overridden to store the operands in some kind of intermediate "formula" representation.
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6511
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re: Strange python code

Post by Jeff250 »

Note: a simpler operator overloading that you're already used to is + or * on strings:

Code: Select all

>>> 'abc' + '123'
'abc123'
>>> 'abc' * 3
'abcabcabc'
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: Strange python code

Post by Isaac »

Do you think constraints module is doing something that I've never seen a class do before? Like maybe it lets you create a bunch of micro instances within your class instance? That would be very cool.

But I can sort of imagine it being a simple function that writes to a list... kinda sounds like a hack to get a desired effect, no?
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6511
Joined: Sun Sep 05, 1999 2:01 am
Location: ❄️❄️❄️

Re: Strange python code

Post by Jeff250 »

Again, constraint is a method. I don't understand where your confusion is or why you would think it is a hack. Consider this simple class:

Code: Select all

>>> class Foo(object):
...     def __init__(self):
...         self.constraints = []
...     def constraint(self, c):
...         self.constraints.append(c)
... 
>>> x = Foo()
>>> x.constraint('constraint1')
>>> x.constraint('constraint2')
>>> x.constraint('constraint3')
>>> x.constraints
['constraint1', 'constraint2', 'constraint3']
It looks like the article is talking about a python class for solving linear programming problems, and linear programming problems generally have multiple constraints.

edit: Is it the method name? If they had named it add_constraint, would that make more sense?
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: Strange python code

Post by Isaac »

:o

Thanks

edit:
We're suppose to have this software for solving large LP stuff from school, but the lab is closed and I think I can write my own version. wolframalpha works fine, but I like python!!!
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: Strange python code

Post by Isaac »

OH my god this is so slow

Code: Select all


from frange import frange
log=[]
for a  in frange(0,100,0.1):
        for b  in frange(0,100,0.1):
                for c  in frange(0,100,0.1):
                        for d  in frange(0,100,0.1):
                                if a<=12 and b<=5 and c<=25 and d<=20 and c+d>=5:
                                        if 800*a+925*b+290*c+380*d <= 8000:
                                                log.append(5000*a+8500*b+2400*c+2800*d)
        print "-"
print max(log)
Two things make this go faster. 1) If I loose the frange() function and use range() it goes a LITTLE faster, but still at a snails pace.
2) If I DON'T use python and use the pypy interpreter it goes a super fast! But it goes back to slow when I use frange()... GRRR!!!

However, even using pypy this becomes just as slow...

Code: Select all

log=[]
for a  in range(0,1000,1):
        a=a*0.1
        for b  in range(0,1000,1):
                b=b*0.1
                for c  in range(0,1000,1):
                        c=c*0.1
                        for d  in range(0,1000,1):
                                d=d*0.1
                                if a<=12 and b<=5 and c<=25 and d<=20 and c+d>=5:
                                        if 800*a+925*b+290*c+380*d <= 8000:
                                                log.append(5000*a+8500*b+2400*c+2800*d)
        print ("-")
print (max(log))

Conclusion: :E :E :E :E :E
I'm guessing there's complicated way to do this faster, but I think I've hit the limit, for the first time, on python's, and its variants, speed.
For now I'll be using c based user friendly python libraries for Linear Programming, which seem to be abundant. Sort of disappointed that such a simple loop would cost so much, but that's learning.
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Sirius
DBB Master
DBB Master
Posts: 5616
Joined: Fri May 28, 1999 2:01 am
Location: Bellevue, WA
Contact:

Re: Strange python code

Post by Sirius »

If I'm reading that right, that's a trillion iterations. Yeah, that's going to be slow no matter what you run it on.
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7649
Joined: Mon Aug 01, 2005 8:47 am
Location: 🍕

Re: Strange python code

Post by Isaac »

You make it sound like a big number! :P
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ :E ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
User avatar
Sirius
DBB Master
DBB Master
Posts: 5616
Joined: Fri May 28, 1999 2:01 am
Location: Bellevue, WA
Contact:

Re: Strange python code

Post by Sirius »

It is fairly. You could look at it like this: a CPU is generally capable of executing a few billion instructions per thread per second. Since this is likely single-threaded, and you have a trillion iterations, it would take at least in the hundreds of seconds even if each iteration was only one instruction. Obviously, it's not going to be that efficient; since Python is an interpreted language you probably have dozens of instructions per iteration, maybe hundreds. Thus, I'd expect the entire loop to potentially take hours to execute.
Post Reply