Re: Writing a chess-playing AI like Alphago in Python

2017-12-25 Thread caigy84
On Sunday, December 24, 2017 at 1:26:22 PM UTC+8, Steve D'Aprano wrote:
> On Sun, 24 Dec 2017 12:20 pm, Cai Gengyang wrote:
> 
> > How many lines of code in Python would it take to create a Go-playing AI
> > like AlphaGo ? Estimates ?
> 
> Somewhere between 1 and 1 billion.
> 
> How about you start by telling us:
> 
> - do you mean AlphaGo or AlphaGo Zero?
> 
> - how many lines of code AlphaGo [Zero] has;
> 
> - in what language or languages;
> 
> - is Python allowed to call out to libraries written in other 
>   languages, e.g. machine learning and neural net libraries,
>   or databases, or does it have to implement *everything*
>   from scratch?
> 
> 
> The Michi Go engine uses about 550 lines of Python:
> 
> https://github.com/pasky/michi
> 
> but I don't believe it does any machine learning.
> 
> 
> See also:
> 
> https://github.com/rossumai/nochi
> 
> https://medium.com/rossum/building-our-own-version-of-alphago-zero-b918642bd2b5
> 
> 
> 
> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.

> - do you mean AlphaGo or AlphaGo Zero?  - AlphaGo
> 
> - how many lines of code AlphaGo [Zero] has;- No Idea
> 
> - in what language or languages;- Python
> 
> - is Python allowed to call out to libraries written in other
>   languages, e.g. machine learning and neural net libraries,
>   or databases, or does it have to implement *everything*
>   from scratch? - Allowed to call out
-- 
https://mail.python.org/mailman/listinfo/python-list


user input string to function

2017-12-25 Thread Nico Vogeli
Hi everybody. First ad foremost, happy Christmas!

I want to let the use input a function (like x**2) and parse it after that 
through code (for my numeric class)

import numpy as np
import matplotlib.pyplot as plt
import scipy.linalg
from sympy.abc import o, h
import sympy
from sympy import symbols
from scitools.StringFunction import StringFunction
from sympy.plotting import plot

x = symbols('x')
def newton(x0, s, s2, tol, n = 2):
'''
Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
x0 = Startwert
f = zu lösende Funktion
fx = Ableitung der Funktion

'''
def f(a):
y = s
return y

def fx(a):
y = s2
return y


a = x0
k = 0

while n > k:
k += 1
b = a - f(a)/fx(a)
print('x', k, '\n',b)

print('Startwert -> x0', '\n', x0)
print('Anzahl Iterationen', '\n', k)
print('f(x~)', '\n', f(x))
print('Näherungswert -> x~', '\n', b)

 
return x0, k, f(x), x

(Please ignore all the input, they are used latter)

Now the output looks like this:

newton(2, 3*x**2, 6*x, 0.1, 2)
x 1 
 -x/2 + 2
x 2 
 -x/2 + 2
Startwert -> x0 
 2
Anzahl Iterationen 
 2
f(x~) 
 3*x**2
Näherungswert -> x~ 
 -x/2 + 2
Out[166]: (2, 2, 3*x**2, x)

I notice that the x is not converted to an integer, because of the x = 
symplos('x')
But I don't know how I could possibli change the code to work...

Regards

Nicco
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: user input string to function

2017-12-25 Thread Chris Angelico
On Tue, Dec 26, 2017 at 12:36 AM, Nico Vogeli  wrote:
> Hi everybody. First ad foremost, happy Christmas!

Same to you!

> I want to let the use input a function (like x**2) and parse it after that 
> through code (for my numeric class)
>
> def newton(x0, s, s2, tol, n = 2):
> '''
> Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
> x0 = Startwert
> f = zu lösende Funktion
> fx = Ableitung der Funktion
>
> '''
> def f(a):
> y = s
> return y
>
> def fx(a):
> y = s2
> return y


> newton(2, 3*x**2, 6*x, 0.1, 2)
>
> I notice that the x is not converted to an integer, because of the x = 
> symplos('x')
> But I don't know how I could possibli change the code to work...

The easiest way is to pass a *function* to newton(). It'd look like this:

def newton(x0, f, fx, tol, n=2):
... as before, but without the nested functions

newton(2, lambda x: 3*x**2, lambda x: 6*x, 0.1, 2)

At least, I think that's how you're doing things. Inside the nested
functions, you use 'a', but outside, you use 'x'. Are those
representing the same concept? If so, the lambda functions given here
will have the same effect.

Hope that helps!

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: unabe to import /pyd file.

2017-12-25 Thread Matt Wheeler
On Fri, Dec 15, 2017, 09:58  wrote:

> Hi,
>
> I have created one library file
> "modified_python_automation_tcl.py"..Inside my library file I have set of
> codes as follows.
>
> import sys
> import time
> from datetime import datetime
> import re,sys,os,itertools,datetime,commands,logging,math,time,copy,inspect
> from robot.api import logger
> import unicodedata
> sys.path.append('C:\Python27\Lib\lib-tk')
> sys.path.append('C:\Python27\DLLs')
> sys.path.append('C:\Python27\libs')
>

The fact that you're having to add these paths is the first sign that
something is up, you shouldn't need to add the default paths to sys.path...

from Tkinter import Tcl
> tcl = Tcl()
> .
> .
> .
> .
> .
> .
>
> I am able to run the file from python IDLE.and able to source all kind of
> .py and .pyd file...But the issue is coming when trying through robo
> framework.
>
>
> I am using robo-framework and have defined the
> modified_python_automation_tcl.py  file as the library file. and tryuing to
> excute my testcase..but getting the below error..
>
>
> Someone please help me how to import .pyd files from robo framework
>
> 
>
>   C:\PCCAutomation
>   C:\Python27\Lib\lib-tk
>   C:\Python27\DLLs
>   C:\Python27\libs
> CLASSPATH:
>   C:/apache-maven-3.3.9/boot/plexus-classworlds-2.5.2.jar
> Acceptance.Tests.PCC.Automation 2.0.Spirent
>

And here we find the problem. You're running robotframework using the JAR
distribution, right? That means that your Python is Jython, which doesn't
support CPython extensions (i.e. .pyd)

Unfortunately, according to a quick google, there is no support for Tkinter
in Jython at all, so if you need a GUI you'll need to re-write your GUI in
a library that Jython/Java can talk to.

(other possible ways around this could be dropping this
"plexus-classworlds" jar if you can and running robotframework from
CPython* instead, or (in theory) using the robotframework remote library [
https://github.com/robotframework/RemoteInterface] to run the
Java-dependent parts in Jython & the CPython-dependent parts in CPython
(I've not used the remote library myself so don't know how easy it is to
work with, but it should work either way around))

* pip install robotframework

> --

--
Matt Wheeler
http://funkyh.at
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: user input string to function

2017-12-25 Thread Nico Vogeli
Am Montag, 25. Dezember 2017 14:51:21 UTC+1 schrieb Chris Angelico:
> On Tue, Dec 26, 2017 at 12:36 AM, Nico Vogeli  wrote:
> > Hi everybody. First ad foremost, happy Christmas!
> 
> Same to you!
> 
> > I want to let the use input a function (like x**2) and parse it after that 
> > through code (for my numeric class)
> >
> > def newton(x0, s, s2, tol, n = 2):
> > '''
> > Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
> > x0 = Startwert
> > f = zu lösende Funktion
> > fx = Ableitung der Funktion
> >
> > '''
> > def f(a):
> > y = s
> > return y
> >
> > def fx(a):
> > y = s2
> > return y
> 
> 
> > newton(2, 3*x**2, 6*x, 0.1, 2)
> >
> > I notice that the x is not converted to an integer, because of the x = 
> > symplos('x')
> > But I don't know how I could possibli change the code to work...
> 
> The easiest way is to pass a *function* to newton(). It'd look like this:
> 
> def newton(x0, f, fx, tol, n=2):
> ... as before, but without the nested functions
> 
> newton(2, lambda x: 3*x**2, lambda x: 6*x, 0.1, 2)
> 
> At least, I think that's how you're doing things. Inside the nested
> functions, you use 'a', but outside, you use 'x'. Are those
> representing the same concept? If so, the lambda functions given here
> will have the same effect.
> 
> Hope that helps!
> 
> ChrisA

Hi Chris

Thanks very much for your quick response! 
I was in a bit of a rush, so I confused the variables (don't worry, I just 
messed arround to try different things, the original code was al tidy with the 
variable).

I tried your input, but now I get another error:


  File "C:/Users/Nicco ZHAW/Desktop/Test GUI/Test Projekt.py", line 42, in 
newton
b = x - f(x)/fx(x)

TypeError: unsupported operand type(s) for /: 'function' and 'function'

This did not occure befor I tried to implement the user input..

Thank you very much!

Nicco
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: user input string to function

2017-12-25 Thread Chris Angelico
On Tue, Dec 26, 2017 at 1:48 AM, Nico Vogeli  wrote:
> Am Montag, 25. Dezember 2017 14:51:21 UTC+1 schrieb Chris Angelico:
>> On Tue, Dec 26, 2017 at 12:36 AM, Nico Vogeli  wrote:
>> > Hi everybody. First ad foremost, happy Christmas!
>>
>> Same to you!
>>
>> > I want to let the use input a function (like x**2) and parse it after that 
>> > through code (for my numeric class)
>> >
>> > def newton(x0, s, s2, tol, n = 2):
>> > '''
>> > Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
>> > x0 = Startwert
>> > f = zu lösende Funktion
>> > fx = Ableitung der Funktion
>> >
>> > '''
>> > def f(a):
>> > y = s
>> > return y
>> >
>> > def fx(a):
>> > y = s2
>> > return y
>>
>>
>> > newton(2, 3*x**2, 6*x, 0.1, 2)
>> >
>> > I notice that the x is not converted to an integer, because of the x = 
>> > symplos('x')
>> > But I don't know how I could possibli change the code to work...
>>
>> The easiest way is to pass a *function* to newton(). It'd look like this:
>>
>> def newton(x0, f, fx, tol, n=2):
>> ... as before, but without the nested functions
>>
>> newton(2, lambda x: 3*x**2, lambda x: 6*x, 0.1, 2)
>>
>> At least, I think that's how you're doing things. Inside the nested
>> functions, you use 'a', but outside, you use 'x'. Are those
>> representing the same concept? If so, the lambda functions given here
>> will have the same effect.
>>
>> Hope that helps!
>>
>> ChrisA
>
> Hi Chris
>
> Thanks very much for your quick response!
> I was in a bit of a rush, so I confused the variables (don't worry, I just 
> messed arround to try different things, the original code was al tidy with 
> the variable).
>
> I tried your input, but now I get another error:
>
>
>   File "C:/Users/Nicco ZHAW/Desktop/Test GUI/Test Projekt.py", line 42, in 
> newton
> b = x - f(x)/fx(x)
>
> TypeError: unsupported operand type(s) for /: 'function' and 'function'
>
> This did not occure befor I tried to implement the user input..

Did you remove the nested functions? The lambda functions completely
replace your "def f(a)" and "def fx(a)" functions.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: user input string to function

2017-12-25 Thread Nico Vogeli
Am Montag, 25. Dezember 2017 15:58:26 UTC+1 schrieb Chris Angelico:
> On Tue, Dec 26, 2017 at 1:48 AM, Nico Vogeli  wrote:
> > Am Montag, 25. Dezember 2017 14:51:21 UTC+1 schrieb Chris Angelico:
> >> On Tue, Dec 26, 2017 at 12:36 AM, Nico Vogeli  wrote:
> >> > Hi everybody. First ad foremost, happy Christmas!
> >>
> >> Same to you!
> >>
> >> > I want to let the use input a function (like x**2) and parse it after 
> >> > that through code (for my numeric class)
> >> >
> >> > def newton(x0, s, s2, tol, n = 2):
> >> > '''
> >> > Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
> >> > x0 = Startwert
> >> > f = zu lösende Funktion
> >> > fx = Ableitung der Funktion
> >> >
> >> > '''
> >> > def f(a):
> >> > y = s
> >> > return y
> >> >
> >> > def fx(a):
> >> > y = s2
> >> > return y
> >>
> >>
> >> > newton(2, 3*x**2, 6*x, 0.1, 2)
> >> >
> >> > I notice that the x is not converted to an integer, because of the x = 
> >> > symplos('x')
> >> > But I don't know how I could possibli change the code to work...
> >>
> >> The easiest way is to pass a *function* to newton(). It'd look like this:
> >>
> >> def newton(x0, f, fx, tol, n=2):
> >> ... as before, but without the nested functions
> >>
> >> newton(2, lambda x: 3*x**2, lambda x: 6*x, 0.1, 2)
> >>
> >> At least, I think that's how you're doing things. Inside the nested
> >> functions, you use 'a', but outside, you use 'x'. Are those
> >> representing the same concept? If so, the lambda functions given here
> >> will have the same effect.
> >>
> >> Hope that helps!
> >>
> >> ChrisA
> >
> > Hi Chris
> >
> > Thanks very much for your quick response!
> > I was in a bit of a rush, so I confused the variables (don't worry, I just 
> > messed arround to try different things, the original code was al tidy with 
> > the variable).
> >
> > I tried your input, but now I get another error:
> >
> >
> >   File "C:/Users/Nicco ZHAW/Desktop/Test GUI/Test Projekt.py", line 42, in 
> > newton
> > b = x - f(x)/fx(x)
> >
> > TypeError: unsupported operand type(s) for /: 'function' and 'function'
> >
> > This did not occure befor I tried to implement the user input..
> 
> Did you remove the nested functions? The lambda functions completely
> replace your "def f(a)" and "def fx(a)" functions.
> 
> ChrisA

I am so so sorry!! I just did not know what you meant by sested functions.. :P

Thank you so much for helping me! You're great!

Cheers and all the best!

Nicco
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: user input string to function

2017-12-25 Thread Chris Angelico
On Tue, Dec 26, 2017 at 2:04 AM, Nico Vogeli  wrote:
> Am Montag, 25. Dezember 2017 15:58:26 UTC+1 schrieb Chris Angelico:
>> On Tue, Dec 26, 2017 at 1:48 AM, Nico Vogeli  wrote:
>> > Am Montag, 25. Dezember 2017 14:51:21 UTC+1 schrieb Chris Angelico:
>> >> On Tue, Dec 26, 2017 at 12:36 AM, Nico Vogeli  
>> >> wrote:
>> >> > Hi everybody. First ad foremost, happy Christmas!
>> >>
>> >> Same to you!
>> >>
>> >> > I want to let the use input a function (like x**2) and parse it after 
>> >> > that through code (for my numeric class)
>> >> >
>> >> > def newton(x0, s, s2, tol, n = 2):
>> >> > '''
>> >> > Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
>> >> > x0 = Startwert
>> >> > f = zu lösende Funktion
>> >> > fx = Ableitung der Funktion
>> >> >
>> >> > '''
>> >> > def f(a):
>> >> > y = s
>> >> > return y
>> >> >
>> >> > def fx(a):
>> >> > y = s2
>> >> > return y
>> >>
>> >>
>> >> > newton(2, 3*x**2, 6*x, 0.1, 2)
>> >> >
>> >> > I notice that the x is not converted to an integer, because of the x = 
>> >> > symplos('x')
>> >> > But I don't know how I could possibli change the code to work...
>> >>
>> >> The easiest way is to pass a *function* to newton(). It'd look like this:
>> >>
>> >> def newton(x0, f, fx, tol, n=2):
>> >> ... as before, but without the nested functions
>> >>
>> >> newton(2, lambda x: 3*x**2, lambda x: 6*x, 0.1, 2)
>> >>
>> >> At least, I think that's how you're doing things. Inside the nested
>> >> functions, you use 'a', but outside, you use 'x'. Are those
>> >> representing the same concept? If so, the lambda functions given here
>> >> will have the same effect.
>> >>
>> >> Hope that helps!
>> >>
>> >> ChrisA
>> >
>> > Hi Chris
>> >
>> > Thanks very much for your quick response!
>> > I was in a bit of a rush, so I confused the variables (don't worry, I just 
>> > messed arround to try different things, the original code was al tidy with 
>> > the variable).
>> >
>> > I tried your input, but now I get another error:
>> >
>> >
>> >   File "C:/Users/Nicco ZHAW/Desktop/Test GUI/Test Projekt.py", line 42, in 
>> > newton
>> > b = x - f(x)/fx(x)
>> >
>> > TypeError: unsupported operand type(s) for /: 'function' and 'function'
>> >
>> > This did not occure befor I tried to implement the user input..
>>
>> Did you remove the nested functions? The lambda functions completely
>> replace your "def f(a)" and "def fx(a)" functions.
>>
>> ChrisA
>
> I am so so sorry!! I just did not know what you meant by sested functions.. :P

No need to apologize! That's why we have two-directional communication
- I can ask you to clarify, you can ask me to clarify, and we get to a
solution.

The concept of passing functions around does take some getting used
to, so there's no shame in not instantly understanding it.

> Thank you so much for helping me! You're great!
>
> Cheers and all the best!

My pleasure! Have yourself an awesome holiday season.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: user input string to function

2017-12-25 Thread Nico Vogeli
Am Montag, 25. Dezember 2017 16:56:19 UTC+1 schrieb Chris Angelico:
> On Tue, Dec 26, 2017 at 2:04 AM, Nico Vogeli  wrote:
> > Am Montag, 25. Dezember 2017 15:58:26 UTC+1 schrieb Chris Angelico:
> >> On Tue, Dec 26, 2017 at 1:48 AM, Nico Vogeli  wrote:
> >> > Am Montag, 25. Dezember 2017 14:51:21 UTC+1 schrieb Chris Angelico:
> >> >> On Tue, Dec 26, 2017 at 12:36 AM, Nico Vogeli  
> >> >> wrote:
> >> >> > Hi everybody. First ad foremost, happy Christmas!
> >> >>
> >> >> Same to you!
> >> >>
> >> >> > I want to let the use input a function (like x**2) and parse it after 
> >> >> > that through code (for my numeric class)
> >> >> >
> >> >> > def newton(x0, s, s2, tol, n = 2):
> >> >> > '''
> >> >> > Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
> >> >> > x0 = Startwert
> >> >> > f = zu lösende Funktion
> >> >> > fx = Ableitung der Funktion
> >> >> >
> >> >> > '''
> >> >> > def f(a):
> >> >> > y = s
> >> >> > return y
> >> >> >
> >> >> > def fx(a):
> >> >> > y = s2
> >> >> > return y
> >> >>
> >> >>
> >> >> > newton(2, 3*x**2, 6*x, 0.1, 2)
> >> >> >
> >> >> > I notice that the x is not converted to an integer, because of the x 
> >> >> > = symplos('x')
> >> >> > But I don't know how I could possibli change the code to work...
> >> >>
> >> >> The easiest way is to pass a *function* to newton(). It'd look like 
> >> >> this:
> >> >>
> >> >> def newton(x0, f, fx, tol, n=2):
> >> >> ... as before, but without the nested functions
> >> >>
> >> >> newton(2, lambda x: 3*x**2, lambda x: 6*x, 0.1, 2)
> >> >>
> >> >> At least, I think that's how you're doing things. Inside the nested
> >> >> functions, you use 'a', but outside, you use 'x'. Are those
> >> >> representing the same concept? If so, the lambda functions given here
> >> >> will have the same effect.
> >> >>
> >> >> Hope that helps!
> >> >>
> >> >> ChrisA
> >> >
> >> > Hi Chris
> >> >
> >> > Thanks very much for your quick response!
> >> > I was in a bit of a rush, so I confused the variables (don't worry, I 
> >> > just messed arround to try different things, the original code was al 
> >> > tidy with the variable).
> >> >
> >> > I tried your input, but now I get another error:
> >> >
> >> >
> >> >   File "C:/Users/Nicco ZHAW/Desktop/Test GUI/Test Projekt.py", line 42, 
> >> > in newton
> >> > b = x - f(x)/fx(x)
> >> >
> >> > TypeError: unsupported operand type(s) for /: 'function' and 'function'
> >> >
> >> > This did not occure befor I tried to implement the user input..
> >>
> >> Did you remove the nested functions? The lambda functions completely
> >> replace your "def f(a)" and "def fx(a)" functions.
> >>
> >> ChrisA
> >
> > I am so so sorry!! I just did not know what you meant by sested functions.. 
> > :P
> 
> No need to apologize! That's why we have two-directional communication
> - I can ask you to clarify, you can ask me to clarify, and we get to a
> solution.
> 
> The concept of passing functions around does take some getting used
> to, so there's no shame in not instantly understanding it.
> 
> > Thank you so much for helping me! You're great!
> >
> > Cheers and all the best!
> 
> My pleasure! Have yourself an awesome holiday season.
> 
> ChrisA

Hello again

So, I tried my best, was looking into the eval and exec function, but still no 
succsess..

Maybe it helps when I provide the code I want to implement:

def newton_verfahren(self):
if self.cB_1.currentText() == 'Newton':

x0 = self.newton_x.text()
f1 = self.func_1.text()
f2 = self.func_2.text()
tol = self.newton_tol.text()
n = self.newton_n.text()
k = Numerik_Funktionen.newton(int(x0), eval(f1), eval(f2),
  int(tol), int(n))
k1 = str(k[0])
k2 = str(k[1])
k3 = str(k[2])
self.ausgabe.setText('Startwert -> x0: '+ k1 + "\n" + 
  'Anzahl Iterationen: ' + k2 +"\n" + 
  'f(x~): ' + k3)  

Notice that cB_1 is a combobox, newton_x, func_1, func_2, newton_tol and 
newton_n are all QLineEdits.

The code above doesn't work, so I've opend a 'test' file to just play around 
with the functions.

Withs test, this return a correct value for the two x functions:

from sympy import symbols

x = symbols('x')
f1 = eval(input('ssd '))
f2 = eval(input('fd '))

But now I get and error from the imported module in the line where the 
abs(f(x)) > tol.

It states that the 'Pow' object is not callable. And now I don't know how to 
proceed, I can't figure out a way to make the call to the imported module:

x = symbols('x')
def newton(x0, f, fx, tol, n = 2):
'''
Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
x0 = Startwert
f = zu lösende Funktion
fx = Ableitung der Funktion

'''


x = x0
k = 0

while n > k and abs(f(x))

Re: acircle.getCenter() to (x,y) coordinates in Python

2017-12-25 Thread MRAB

On 2017-12-25 02:42, G Yu wrote:

Ah, I get it now.  I have to store the acircle.getCenter() in a point Point, 
and then access Point.getX() and Point.getY() separately.  It was just that 
middle step that I was missing.  Thanks so much!

It's not strictly true that you _have to_ store the result of 
acircle.getCenter().


You could call it twice:

(acircle.getCenter().getX(), acircle.getCenter().getY())

or you could write a function for it:

def as_cartesian(point):
return point.getX(), point.getY()

and call it:

as_cartesian(acircle.getCenter())

Just pick whatever way makes the most sense.
--
https://mail.python.org/mailman/listinfo/python-list