Permalink
Browse files

Python 2to3

  • Loading branch information...
morishin committed Oct 13, 2016
1 parent c65aea5 commit 75475c1578674150a14a6eccb4048413eaeae761
View
@@ -32,7 +32,7 @@ python src/python/competition/ipymario.py
Run the following setup if necessary.
```sh
virtualenv env #Use Python 2.7
virtualenv env #Use Python 3
source env/bin/activate
pip install -r requirements.txt
```
@@ -1,7 +1,7 @@
__author__="Sergey Karakovskiy, sergey at idsia fullstop ch"
__date__ ="$May 13, 2009 12:16:29 AM$"
from forwardagent import ForwardAgent
from forwardrandomagent import ForwardRandomAgent
from marioagent import MarioAgent
from .forwardagent import ForwardAgent
from .forwardrandomagent import ForwardRandomAgent
from .marioagent import MarioAgent
@@ -2,7 +2,7 @@
__author__ = "Sergey Karakovskiy, sergey at idsia fullstop ch"
__date__ = "$May 1, 2009 2:46:34 AM$"
from marioagent import MarioAgent
from .marioagent import MarioAgent
class ForwardAgent(MarioAgent):
""" In fact the Python twin of the
@@ -52,15 +52,15 @@ def _a2(self):
Just substitue getAction by this method and see how it behaves.
"""
if (self.mayMarioJump):
print "m: %d, %s, %s, 12: %d, 13: %d, j: %d" \
print("m: %d, %s, %s, 12: %d, 13: %d, j: %d" \
% (self.levelScene[11, 11], self.mayMarioJump, self.isMarioOnGround, \
self.levelScene[11, 12], self.levelScene[11, 12], self.trueJumpCounter)
self.levelScene[11, 12], self.levelScene[11, 12], self.trueJumpCounter))
else:
if self.levelScene == None:
print "Bad news....."
print "m: %d, 12: %d, 13: %d, j: %d" \
print("Bad news.....")
print("m: %d, 12: %d, 13: %d, j: %d" \
% (self.levelScene[11, 11], \
self.levelScene[11, 12], self.levelScene[11, 12], self.trueJumpCounter)
self.levelScene[11, 12], self.levelScene[11, 12], self.trueJumpCounter))
a = numpy.zeros(5, int)
a[1] = 1
@@ -90,10 +90,10 @@ def _a2(self):
elif a[i] == 0:
actionStr += '0'
else:
print "something very dangerous happen...."
print("something very dangerous happen....")
actionStr += "\r\n"
print "action: " , actionStr
print("action: " , actionStr)
return actionStr
def getAction(self):
@@ -138,7 +138,7 @@ def printLevelScene(self):
for y in range(22):
tmpData += self.mapElToStr(self.levelScene[x][y]);
ret += "\n%s" % tmpData;
print ret
print(ret)
def mapElToStr(self, el):
"""maps element of levelScene to str representation"""
@@ -152,4 +152,4 @@ def mapElToStr(self, el):
def printObs(self):
"""for debug"""
print repr(self.observation)
print(repr(self.observation))
@@ -2,7 +2,7 @@
__author__ = "Sergey Karakovskiy, sergey at idsia fullstop ch"
__date__ = "$Apr 30, 2009 6:46:04 PM$"
from marioagent import MarioAgent
from .marioagent import MarioAgent
class ForwardRandomAgent(MarioAgent):
"""
@@ -33,9 +33,9 @@ def integrateObservation(self, mayMarioJump, isMarioOnGround, levelScene):
def printObs(self):
"""for debug"""
print self.observation
print(self.observation)
if __name__ != "__main__":
print "Importing %s " % __name__;
print("Importing %s " % __name__);
else:
print "This is the module to import rather than run"
print("This is the module to import rather than run")
@@ -8,10 +8,10 @@ class MarioAgent:
"""
def integrateObservation(self, obs):
raise "Not implemented"
raise Exception("Not implemented")
def getAction(self):
raise "Not implemented"
raise Exception("Not implemented")
def giveReward(self, reward):
pass
@@ -32,5 +32,3 @@ def __repr__(self):
return "<%s '%s'>" % (self.__class__.__name__, self.name)
def newEpisode(self):
pass
@@ -1,7 +1,7 @@
__author__="Sergey Karakovskiy, sergey at idsia fullstop ch"
__date__ ="$May 13, 2009 12:17:14 AM$"
from environment import Environment
from tcpenvironment import TCPEnvironment
from marioenvironment import MarioEnvironment
from .environment import Environment
from .tcpenvironment import TCPEnvironment
from .marioenvironment import MarioEnvironment
@@ -32,56 +32,55 @@ def connectToDefaultServer(self):
def connectToServer(self, host, port):
"""connects to a server"""
try:
print "Client: Trying to connect to %s:%s" % (host, port)
print("Client: Trying to connect to %s:%s" % (host, port))
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
except socket.error as msg:
sys.stderr.write("[SOCKET CREATION ERROR1] %s\n" % msg[1])
raise
# self.sock.settimeout(5)
# print "Timeout set to 5"
try:
self.sock.connect((host, port))
print "Client: Connection to %s:%s succeeded!" % (host, port)
print "Client: Looking forward to receive greeting message..."
print("Client: Connection to %s:%s succeeded!" % (host, port))
print("Client: Looking forward to receive greeting message...")
data = self.recvData()
print "Client: Greeting received: %s" % data
print("Client: Greeting received: %s" % data)
except socket.error, msg:
except socket.error as msg:
sys.stderr.write("[CONNECTION ERROR] %s\n" % msg[1])
raise
GreatingMessage = "Client: Dear Server, hello! I am %s\r\n" % self.OwnerName
self.sendData(GreatingMessage)
def printConnectionData(self):
"""Print to standard output the current connection data"""
print "Client: %s Connection Info: \r\nHost = %s, port = %d" % (self, self.Host, self.Port)
print("Client: %s Connection Info: \r\nHost = %s, port = %d" % (self, self.Host, self.Port))
def disconnet(self):
"""disconnects from the server"""
print "Client is about to close the connection"
print("Client is about to close the connection")
self.disconnected = True
self.sock.close()
print "Connection closed"
print("Connection closed")
def recvData(self):
"""receive arbitrary data from server"""
try:
return self.sock.recv(self.bufSize)
except socket.error, msg:
except socket.error as msg:
sys.stderr.write("[SOCKET PIPE ERROR WHILE RECEIVING] %s\n.Possible reason: socket closed due to time out and/or requested server is currently busy" % msg[1])
raise
def sendData(self, data):
"""send arbitrary string to server"""
try:
self.sock.send(data)
except socket.error, msg:
self.sock.send(data.encode())
except socket.error as msg:
sys.stderr.write("[SOCKET PIPE ERROR WHILE SENDING] %s\n" % msg[1])
raise
@@ -22,17 +22,17 @@ def getSensors(self):
@rtype: by default, this is assumed to be a numpy array of doubles
@note: This function is abstract and has to be implemented.
"""
raise "Not implemented"
raise Exception("Not implemented")
def performAction(self, action):
""" perform an action on the world that changes it's internal state (maybe stochastically)
@param action: an action that should be executed in the Environment.
@type action: by default, this is assumed to be a numpy array of doubles
@note: This function is abstract and has to be implemented.
"""
raise "Not implemented"
raise Exception("Not implemented")
def reset(self):
""" Most environments will implement this optional method that allows for reinitialization.
"""
pass
pass
@@ -1,7 +1,7 @@
__author__ = "Sergey Karakovskiy, sergey at idsia fullstop ch"
__date__ = "$May 13, 2009 1:29:41 AM$"
from tcpenvironment import TCPEnvironment
from .tcpenvironment import TCPEnvironment
from utils.dataadaptor import extractObservation
class MarioEnvironment(TCPEnvironment):
@@ -1,8 +1,8 @@
__author__ = "Sergey Karakovskiy, sergey at idsia fullstop ch"
__date__ = "$May 13, 2009 1:25:30 AM$"
from client import Client
from environment import Environment
from .client import Client
from .environment import Environment
from utils.dataadaptor import show
class TCPEnvironment(Environment):
@@ -13,7 +13,7 @@ def __init__(self, agentName = "UnnamedClient", host = 'localhost', port = 4242,
self.host = host
self.port = port
if self.verbose:
print "TCPENV: agentName ", agentName
print("TCPENV: agentName ", agentName)
self.client = Client(host, port, agentName)
self.connected = True
@@ -22,21 +22,21 @@ def isAvailable(self):
return self.connected
def to_unicode_or_bust(self, obj, encoding = 'utf-8'):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
if isinstance(obj, str):
if not isinstance(obj, str):
obj = str(obj, encoding)
return obj
def getSensors(self):
""" receives an observation via tcp connection"""
# print "Looking forward to receive data"
data = self.client.recvData()
data = self.to_unicode_or_bust(data)
if data == "ciao":
self.client.disconnect()
self.connected = False
self.connected = False
elif len(data) > 5:
# print data
# for i in range(31):
@@ -55,6 +55,6 @@ def performAction(self, action):
elif action[i] == 0:
actionStr += '0'
else:
raise "something very dangerous happen...."
raise Exception("something very dangerous happen....")
actionStr += "\r\n"
self.client.sendData(actionStr)
@@ -1,6 +1,6 @@
__author__="Sergey Karakovskiy, sergey at idsia fullstop ch"
__date__ ="$May 13, 2009 12:15:50 AM$"
from experiment import Experiment
from episodicexperiment import EpisodicExperiment
from .experiment import Experiment
from .episodicexperiment import EpisodicExperiment
@@ -1,7 +1,7 @@
__author__ = "Sergey Karakovskiy, sergey at idsia fullstop ch"
__date__ = "$May 12, 2009 11:18:19 PM$"
from experiment import Experiment
from .experiment import Experiment
#class EpisodicExperiment(Experiment):
@@ -23,7 +23,7 @@
# return all_rewards
from experiment import Experiment
from .experiment import Experiment
class EpisodicExperiment(Experiment):
""" The extension of Experiment to handle episodic tasks. """
@@ -18,39 +18,39 @@ def main():
agent = ForwardAgent()
task = MarioTask(agent.name, initMarioMode = 2)
exp = EpisodicExperiment(task, agent)
print 'Task Ready'
print('Task Ready')
exp.doEpisodes(2)
print 'mm 2:', task.reward
print('mm 2:', task.reward)
task.env.initMarioMode = 1
exp.doEpisodes(1)
print 'mm 1:', task.reward
print('mm 1:', task.reward)
task.env.initMarioMode = 0
exp.doEpisodes(1)
print 'mm 0:', task.reward
print('mm 0:', task.reward)
task.env.initMarioMode = 0
exp.doEpisodes(1)
print 'mm 0:', task.reward
print('mm 0:', task.reward)
task.env.initMarioMode = 0
task.env.levelDifficulty = 5
exp.doEpisodes(1)
print 'mm 0, ld 5: ', task.reward
print('mm 0, ld 5: ', task.reward)
task.env.initMarioMode = 1
task.env.levelDifficulty = 5
exp.doEpisodes(1)
print 'mm 1, ld 5: ', task.reward
print('mm 1, ld 5: ', task.reward)
task.env.initMarioMode = 2
task.env.levelDifficulty = 5
exp.doEpisodes(1)
print 'mm 2, ld 5: ', task.reward
print('mm 2, ld 5: ', task.reward)
print "finished"
print("finished")
# clo = CmdLineOptions(sys.argv)
# task = MarioTask(MarioEnvironment(clo.getHost(), clo.getPort(), clo.getAgent().name))
@@ -60,4 +60,4 @@ def main():
if __name__ == "__main__":
main()
else:
print "This is module to be run rather than imported."
print("This is module to be run rather than imported.")
@@ -2,7 +2,7 @@
__date__ ="$May 13, 2009 12:15:26 AM$"
from task import Task
from episodictask import EpisodicTask
from mariotask import MarioTask
from .task import Task
from .episodictask import EpisodicTask
from .mariotask import MarioTask
Oops, something went wrong.

0 comments on commit 75475c1

Please sign in to comment.