some OT: how to solve this kind of problem in our program?

oyster lepto.python at gmail.com
Sat Dec 23 23:39:11 EST 2006


1. first of all, what is the English jargon (Optimize? But I think
this is not a very good keyword :( )for this problem? So I can use it
to search on the internet
2. is there any free/open lib for this?
3. I know for some questions(case 1, case 2, and sudoku), we can use
bundles of "FOR...NEXT" loop to program. however I think it is clumsy
and inconvenient, especially when there is many vars
4. I don't know how to deal with case 3 and case 4


case:
1. choose x0~x9 from 1~9, and must use all of 1~9, let
x0/(10*x1+x2)+x3/(10*x4+x5)+x6/(10*x7+x8)=1/2

2. choose x0~x15 from 1~16, and must use all of 1~16, let
+-----+-----+-----+-----+
|  x0 |  x1 |  x2 |  x3 |
+-----+-----+-----+-----+
|  x4 |  x5 |  x6 |  x7 |
+-----+-----+-----+-----+
|  x8 |  x9 | x10 | x11 |
+-----+-----+-----+-----+
| x12 | x13 | x14 | x15 |
+-----+-----+-----+-----+

sum of every column =sum of of every row
= x0+x5+x10+x11 =x3+x6+x9+x12

3: calculate the minimum of
sin((z*x-0.5)^2 + x*2*y^2-z/10)*exp(-((x-0.5-exp(-y+z))^2 + y^2-z/5+3))
where x in [-1,7], y in [-2,2]

4: calculate the root [x,y,z], let
(x-0.3)^y^z+x/y/z-x*y*sin(z)+(x+y-z)^cos(x-1) = 1
(y-0.2)^z^x+y/z/x-y*z*sin(x)+(y+z-x)^cos(y-2) = 2
(z-0.1)^x^y+z/x/y-z*x*sin(y)+(z+x-y)^cos(z-3) = 3


I have written the case 1 in python, it needs 90 seconds on my pc, and
the same approach in www.freebasic.net takes less than 1 seconds
[code for python]
import sets
import time
try:
  import psyco
  psyco.full()
except:
  pass

d0, d1=1, 2

st=time.time()
result=[]
for a0 in range(1,10):
  for a1 in sets.Set(range(1,10))-sets.Set([a0]):
    for a2 in sets.Set(range(1,10))-sets.Set([a0,a1]):
      a1a2=a1*10+a2
      if 2*a0< a1a2:
        for b0 in sets.Set(range(1,10))-sets.Set([a0,a1,a2]):
          for b1 in sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0]):
            for b2 in sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0, b1]):
              b1b2=b1*10+b2
              if 2*a0*b1b2 + 2*b0*a1a2 < a1a2*b1b2:
                for c0 in sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0, b1, b2]):
                  for c1 in
sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0, b1, b2, c0]):
                    for c2 in
sets.Set(range(1,10))-sets.Set([a0,a1,a2,b0, b1, b2, c0, c1]):
                      c1c2=c1*10+c2
                      if d1*a0*b1b2*c1c2 + d1*b0*a1a2*c1c2 +
d1*c0*a1a2*b1b2 == d0*a1a2*b1b2*c1c2:
                        aresult=[[a0, a1, a2], [b0, b1, b2], [c0, c1, c2]]
                        aresult.sort()
                        if aresult not in result:
                          result.append(aresult)
et=time.time()
print 'time elapsed: %s s' % (et-st)
for [[a0, a1, a2], [b0, b1, b2], [c0, c1, c2]] in result:
  print '  %0d     %0d     %0d     %0d' % (a0, b0, c0, d0)
  print '--- + --- + --- = ---'
  print ' %0d%0d    %0d%0d    %0d%0d     %0d' %(a1, a2, b1, b2, c1,c2, d1)
  print
[/code]



More information about the Python-list mailing list