* FAQ    * Search  * Register * Login 
Active topics
Unanswered topics

All times are UTC-06:00



Post new topic  Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Strange python code
PostPosted: Sat Apr 07, 2012 12:44 pm 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
Note: I'm not confused about the math. Just the way these methods are used.

Here's a minimalist example

Code:
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

_________________
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ


Top
   
 Post subject: Re: Strange python code
PostPosted: Sat Apr 07, 2012 1:11 pm 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
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.


Top
   
 Post subject: Re: Strange python code
PostPosted: Sat Apr 07, 2012 1:15 pm 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
Note: a simpler operator overloading that you're already used to is + or * on strings:

Code:
>>> 'abc' + '123'
'abc123'
>>> 'abc' * 3
'abcabcabc'


Top
   
 Post subject: Re: Strange python code
PostPosted: Sat Apr 07, 2012 2:47 pm 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
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?

_________________
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ


Top
   
 Post subject: Re: Strange python code
PostPosted: Sat Apr 07, 2012 3:22 pm 
Offline
DBB Master
DBB Master
User avatar

Joined: Sun Sep 05, 1999 2:01 am
Posts: 6377
Location: ☃☃☃
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:
>>> 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?


Top
   
 Post subject: Re: Strange python code
PostPosted: Sat Apr 07, 2012 3:41 pm 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
: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!!!

_________________
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ


Top
   
 Post subject: Re: Strange python code
PostPosted: Sun Apr 08, 2012 9:23 am 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
OH my god this is so slow
Code:

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:
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.

_________________
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ


Top
   
 Post subject: Re: Strange python code
PostPosted: Sun Apr 08, 2012 3:08 pm 
Offline
DBB Master
DBB Master
User avatar

Joined: Fri May 28, 1999 2:01 am
Posts: 5426
ICQ: 28874658
Website: http://www.povterminal.net/
Location: Bellevue, WA
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.


Top
   
 Post subject: Re: Strange python code
PostPosted: Sun Apr 08, 2012 8:17 pm 
Offline
DBB Artist
DBB Artist
User avatar

Joined: Mon Aug 01, 2005 8:47 am
Posts: 7124
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝
You make it sound like a big number! :P

_________________
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ


Top
   
 Post subject: Re: Strange python code
PostPosted: Mon Apr 09, 2012 1:43 am 
Offline
DBB Master
DBB Master
User avatar

Joined: Fri May 28, 1999 2:01 am
Posts: 5426
ICQ: 28874658
Website: http://www.povterminal.net/
Location: Bellevue, WA
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.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 10 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron



Descent'rs have piloted these pages
 
The layout and contents contained within this site are © DescentBB.net 1997-2006.
Descent, Descent II are © Parallax Software Corporation.
Descent III is Outrage Entertainment.
Descent is a Trademark of Interplay Productions.

Miner Wars™ is trademark of Keen Software House s. r. o.
.


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group