Ejemplo n.º 1
0
class I18nFileAccessor(object):
	def __init__(self, path):
		self.b = Buffer()
  
		with open(path, 'r') as f:
			self.b.addInfos(f.read())

		self._indexes = dict()
    
		self.b.setCursor(self.b.readInt())
		nbValues = self.b.readInt() / 8;
		for i in range(0, nbValues):
			key = self.b.readInt()
			val = self.b.readInt()

			self._indexes[key] = val

	def getText(self, arg1):
		pos = self._indexes[arg1]
  
		self.b.setCursor(pos)

		return self.b.readUTF()

	def hasText(self, arg1):
		return self._indexes[arg1]
Ejemplo n.º 2
0
class VertexStream(object):
    """
    VertexFormat must be set for this class to render correctly.
    And before vertexformat is set, you have to bind the vbo.
    """
    def __init__(self, fmt, which=GL_TRIANGLES, maxCount=4096):
        self.fmt      = fmt
        self.fmt_ctype_p = POINTER(self.fmt.ctype)
        self.which    = which
        self.maxCount = 4096
        self.vbo = Buffer(usage=GL_STREAM_DRAW)
        self.vbo.setSize(sizeof(fmt.ctype) * self.maxCount)
        self.data = None
        self.count = 0

    def vertex(self, *data):
        if self.count == 0:
            address = self.vbo.map(GL_WRITE_ONLY)
            assert address != 0
            self.data = cast(address, self.fmt_ctype_p)
        self.data[self.count] = data
        self.count += 1
        if self.count >= self.maxCount:
            self.flush()

    def flush(self):
        if self.count > 0:
            self.data = None
            self.vbo.unmap()
            glDrawArrays(self.which, 0, self.count)
            self.count = 0
Ejemplo n.º 3
0
class PseudoStreamDecoder(Stream):
    def __init__(self, transport_class, transport_kwargs):
        self.bufin=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
        self.bufout=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
        self.upstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
        self.downstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
        self.transport=transport_class(self, **transport_kwargs)
        self.lockin=threading.Lock()
        self.lockout=threading.Lock()

    def decode_data(self, data):
        with self.lockin:
            #print "decoding %s"%repr(data)
            self.bufin.drain()
            self.bufin.write(data)
            self.transport.downstream_recv(self.bufin)
            cookie=self.bufin.cookie
            self.bufin.cookie=None
            return self.upstream.read(), cookie

    def encode_data(self, data, cookie):
        with self.lockout:
            #print "encoding %s"%repr(data)
            self.bufout.drain()
            self.bufout.write(data)
            self.bufout.cookie=cookie
            self.transport.upstream_recv(self.bufout)
            return self.downstream.read()
Ejemplo n.º 4
0
 def __init__(self, transport_class, transport_kwargs):
     self.bufin=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
     self.bufout=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
     self.upstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
     self.downstream=Buffer(transport_func=addGetPeer(("127.0.0.1", 443)))
     self.transport=transport_class(self, **transport_kwargs)
     self.lockin=threading.Lock()
     self.lockout=threading.Lock()
Ejemplo n.º 5
0
 def test_buffer_resize(self):
     data = np.zeros(10)
     B = Buffer(data=data)
     data = np.zeros(20)
     B._need_resize == False
     B.set_data(data)
     assert B.nbytes == data.nbytes
     assert B._need_resize == True
Ejemplo n.º 6
0
class EditorShellNotebook(wx.Notebook):
    """A notebook containing an editor page and a shell page."""

    def __init__(self, parent, filename=None):
        """Create EditorShellNotebook instance."""
        wx.Notebook.__init__(self, parent, id=-1)
        usePanels = True
        if usePanels:
            editorparent = editorpanel = wx.Panel(self, -1)
            shellparent = shellpanel = wx.Panel(self, -1)
        else:
            editorparent = self
            shellparent = self
        self.buffer = Buffer()
        self.editor = Editor(parent=editorparent)
        self.buffer.addEditor(self.editor)
        self.buffer.open(filename)
        self.shell = Shell(
            parent=shellparent, locals=self.buffer.interp.locals, style=wx.CLIP_CHILDREN | wx.SUNKEN_BORDER
        )
        self.buffer.interp.locals.clear()
        if usePanels:
            self.AddPage(page=editorpanel, text="Editor", select=True)
            self.AddPage(page=shellpanel, text="Shell")
            # Setup sizers
            editorsizer = wx.BoxSizer(wx.VERTICAL)
            editorsizer.Add(self.editor.window, 1, wx.EXPAND)
            editorpanel.SetSizer(editorsizer)
            editorpanel.SetAutoLayout(True)
            shellsizer = wx.BoxSizer(wx.VERTICAL)
            shellsizer.Add(self.shell, 1, wx.EXPAND)
            shellpanel.SetSizer(shellsizer)
            shellpanel.SetAutoLayout(True)
        else:
            self.AddPage(page=self.editor.window, text="Editor", select=True)
            self.AddPage(page=self.shell, text="Shell")
        self.editor.setFocus()
        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged, id=self.GetId())

    def OnPageChanged(self, event):
        """Page changed event handler."""
        selection = event.GetSelection()
        if selection == 0:
            self.editor.setFocus()
        else:
            self.shell.SetFocus()
        event.Skip()

    def SetFocus(self):
        wx.Notebook.SetFocus(self)
        selection = self.GetSelection()
        if selection == 0:
            self.editor.setFocus()
        else:
            self.shell.SetFocus()
Ejemplo n.º 7
0
 def parse(cls,buffer):
     rname = buffer.decode_name()
     rtype,rclass,ttl,rdlength = buffer.unpack("!HHIH")
     if rtype == QTYPE.OPT:
         options = []
         option_buffer = Buffer(buffer.get(rdlength))
         while option_buffer.remaining() > 4:
             code,length = option_buffer.unpack("!HH")
             data = option_buffer.get(length)
             options.append(EDNSOption(code,data))
         rdata = options
     else:
         rdata = RDMAP.get(QTYPE[rtype],RD).parse(buffer,rdlength)
     return cls(rname,rtype,rclass,ttl,rdata)
Ejemplo n.º 8
0
 def __init__(self, parent, filename=None):
     """Create EditorShellNotebook instance."""
     wx.Notebook.__init__(self, parent, id=-1)
     usePanels = True
     if usePanels:
         editorparent = editorpanel = wx.Panel(self, -1)
         shellparent = shellpanel = wx.Panel(self, -1)
     else:
         editorparent = self
         shellparent = self
     self.buffer = Buffer()
     self.editor = Editor(parent=editorparent)
     self.buffer.addEditor(self.editor)
     self.buffer.open(filename)
     self.shell = Shell(parent=shellparent, locals=self.buffer.interp.locals,
                        style=wx.CLIP_CHILDREN | wx.SUNKEN_BORDER)
     self.buffer.interp.locals.clear()
     if usePanels:
         self.AddPage(page=editorpanel, text='Editor', select=True)
         self.AddPage(page=shellpanel, text='Shell')
         # Setup sizers
         editorsizer = wx.BoxSizer(wx.VERTICAL)
         editorsizer.Add(self.editor.window, 1, wx.EXPAND)
         editorpanel.SetSizer(editorsizer)
         editorpanel.SetAutoLayout(True)
         shellsizer = wx.BoxSizer(wx.VERTICAL)
         shellsizer.Add(self.shell, 1, wx.EXPAND)
         shellpanel.SetSizer(shellsizer)
         shellpanel.SetAutoLayout(True)
     else:
         self.AddPage(page=self.editor.window, text='Editor', select=True)
         self.AddPage(page=self.shell, text='Shell')
     self.editor.setFocus()
     self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged, id=self.GetId())
Ejemplo n.º 9
0
    def __init__(self, loop, socket, remote = None):
        self.__sock = socket
        self.__sock.setblocking(False)
        self.__peeraddr = remote

        self.__loop = loop

        self.__recvbuf = Buffer()
        self.__sendbuf = Buffer()
        self.__shouldPollWrite = False

        self.__onConnectCallback = None
        self.__onDisconnectCallback = None

        self.__msgCallback = None
        self.__futures = []
Ejemplo n.º 10
0
Archivo: bot.py Proyecto: Raeon/pygar
    def __init__(self, game):
        self.game = game

        # core variables
        # self.running = False  # no point, is there? we have is_connected() and is_alive()
        # self.thread = None  # instances are updated by their Game or Server if no game has been found yet
        self.session = Session()
        self.buffer = Buffer()

        # game information
        self.name = 'Test'  #''.join([random.choice('0123456789abcdefghijlkmnopqrstuvwxyz') for i in range(8)])
        self.last_x = 0  # last sent mouse X coordinate
        self.last_y = 0  # last sent mouse Y coordinate
        self.view_x = 0  # viewport x
        self.view_y = 0  # viewport y
        self.view_w = 0  # viewport width
        self.view_h = 0  # viewport height

        # our state
        self.has_sent_init = False
        self.last_sent_spawn = 0
        self.last_update = 0

        # cell information
        self.ids = []  # list of ids (to get cell, query id in all_cells)
        # no longer track cell ids
        self.ladder = []
        self.mode = 'ffa'
Ejemplo n.º 11
0
    def __init__(self, parent, filename=None):
        """Create EditorShellNotebook instance."""
        if DEBUG : print "EditorShellNotebook"

        self.parent=parent
        wx.Notebook.__init__(self, parent, id=-1)
        editorparent = editorpanel = wx.Panel(self, -1)
        logparent = logpanel = wx.Panel(self, -1)
        plotparent = plotpanel = wx.Panel(self, -1)
        self.buffer = Buffer()
        
        self.editor = Editor(parent=editorparent)
        self.buffer.addEditor(self.editor)
        self.buffer.open(filename)
        self.log= ViewLog(parent=logparent)
        self.editor.log = self.log
        
        self.plot = PlotFigure(parent=plotparent,filename=filename)
        self.editor.plot = self.plot
        
        # Set the log target to be this textctrl using our own class
        wx.Log_SetActiveTarget(MyLog(self.log, 0))
        
        # for serious debugging
        if SERIOUSDEBUG:
            wx.Log_SetActiveTarget(wx.LogStderr())
            wx.Log_SetTraceMask(wx.TraceMessages)

        self.AddPage(page=editorpanel, text='Editor', select=True)
        self.AddPage(page=logpanel, text='Log')
        self.AddPage(page=plotpanel, text='Plot')

        # Setup sizers

        editorsizer = wx.BoxSizer(wx.VERTICAL)
        editorsizer.Add(self.editor.window, 1, wx.EXPAND)
        editorpanel.SetSizer(editorsizer)
        editorpanel.SetAutoLayout(True)

        logsizer = wx.BoxSizer(wx.VERTICAL)
        logsizer.Add(self.log.window, 1, wx.EXPAND)
        logpanel.SetSizer(logsizer)
        logpanel.SetAutoLayout(True)

        plotsizer1 = wx.BoxSizer(wx.HORIZONTAL)
        plotsizer1.Add(self.plot.canvas, 1, wx.EXPAND)
        plotsizer1.Add(self.plot.choice, 0, wx.EXPAND)
        plotsizer = wx.BoxSizer(wx.VERTICAL)
        plotsizer.Add(plotsizer1, 1, wx.EXPAND)
        plotsizer.Add(self.plot.toolbar, 0, wx.GROW)
        plotpanel.SetSizer(plotsizer)

        self.plot.Fit()

        # Plot the spectra read from filename
        if  self.plot.plot_data() :
            self.SetSelection(2)
            self.plot.Show()
        
        wx.EVT_NOTEBOOK_PAGE_CHANGED(self, self.GetId(), self.OnPageChanged)
Ejemplo n.º 12
0
 def __init__(self, filename):
     self.debug = True
     self.filename = filename
     self.buf = Buffer(filename, False)
     self.read_header()
     self.buf.close()
     self.read_matrix()
     self.num_rows = len(self.trace)
Ejemplo n.º 13
0
 def bufferCreate(self, filename=None):
     """Create new buffer."""
     buffer = Buffer()
     panel = wx.Panel(parent=self.notebook, id=-1)
     panel.Bind(wx.EVT_ERASE_BACKGROUND, lambda x: x)        
     editor = Editor(parent=panel)
     panel.editor = editor
     sizer = wx.BoxSizer(wx.VERTICAL)
     sizer.Add(editor.window, 1, wx.EXPAND)
     panel.SetSizer(sizer)
     panel.SetAutoLayout(True)
     sizer.Layout()
     buffer.addEditor(editor)
     buffer.open(filename)
     self.setEditor(editor)
     self.notebook.AddPage(page=panel, text=self.buffer.name, select=True)
     self.editor.setFocus()
Ejemplo n.º 14
0
def parse_line(line, chars=None, depth=0):
    """Convert a single line of Logo into a list of tokens or lists.

    >>> parse_line('print sum 10 difference 7 3')
    ['print', 'sum', '10', 'difference', '7', '3']
    >>> parse_line('print "this [is a [deep] list]')
    ['print', '"this', ['is', 'a', ['deep'], 'list']]
    """
    if chars == None:
        chars = Buffer(line.strip()[:])

    tokens = []
    while True:
        if chars.current == None: # End of the line
            if depth != 0:
                raise SyntaxError('Unmatched "[" at ' + str(chars))
            return tokens
        elif chars.current == ' ': # Skip over spaces
            chars.pop()
        elif chars.current == '[':
            chars.pop()
            tokens.append(parse_line(line, chars, depth + 1))
        elif chars.current == ']':
            if depth == 0:
                raise SyntaxError('Unexpected "]" at ' + str(chars))
            else:
                chars.pop()
                return tokens
        else:
            tokens.append(parse_token(chars))
Ejemplo n.º 15
0
 def __init__(self, fmt, which=GL_TRIANGLES, maxCount=4096):
     self.fmt      = fmt
     self.fmt_ctype_p = POINTER(self.fmt.ctype)
     self.which    = which
     self.maxCount = 4096
     self.vbo = Buffer(usage=GL_STREAM_DRAW)
     self.vbo.setSize(sizeof(fmt.ctype) * self.maxCount)
     self.data = None
     self.count = 0
Ejemplo n.º 16
0
 def lose(self):
   for period in self._periods:
     period.stop()
     del period
   
   del self.buffer
   self.buffer = Buffer()
   
   return self.loseConnection()
Ejemplo n.º 17
0
 def bufferCreate(self, filename=None):
     """Create new buffer."""
     self.bufferDestroy()
     buffer = Buffer()
     self.panel = panel = wx.Panel(parent=self, id=-1)
     panel.Bind (wx.EVT_ERASE_BACKGROUND, lambda x: x)        
     editor = Editor(parent=panel)
     panel.editor = editor
     sizer = wx.BoxSizer(wx.VERTICAL)
     sizer.Add(editor.window, 1, wx.EXPAND)
     panel.SetSizer(sizer)
     panel.SetAutoLayout(True)
     sizer.Layout()
     buffer.addEditor(editor)
     buffer.open(filename)
     self.setEditor(editor)
     self.editor.setFocus()
     self.SendSizeEvent()
Ejemplo n.º 18
0
def read(s):
    """Parse an expression from a string. If the string does not contain an
    expression, None is returned. If the string cannot be parsed, a SyntaxError
    is raised.

    >>> read('lambda f: f(0)')
    LambdaExpr(['f'], CallExpr(Name('f'), [Literal(0)]))
    >>> read('(lambda x: x)(5)')
    CallExpr(LambdaExpr(['x'], Name('x')), [Literal(5)])
    >>> read('(lambda: 5)()')
    CallExpr(LambdaExpr([], Literal(5)), [])
    >>> read('lambda x y: 10')
    Traceback (most recent call last):
      ...
    SyntaxError: expected ':' but got 'y'
    >>> read('  ')  # returns None
    """
    src = Buffer(tokenize(s))
    if src.current() is not None:
        return read_expr(src)
Ejemplo n.º 19
0
    def __init__(self, password):
        Buffer.__init__(self)
        
        if not len(password):
            raise CipherError("Password cannot be blank")
        self.password = password

        # build and initialize the context
        self.ctx = evp.EVP_CIPHER_CTX_new()
        if not self.ctx:
            raise CipherError("Could not create encryption context")
        evp.EVP_CIPHER_CTX_init(self.ctx)

        # get the cipher object
        cipher_object = evp.EVP_aes_256_cbc()
        if not cipher_object:
            raise CipherError("Could not create cipher object")

        # finish the context and cipher object
        if not evp.EVP_EncryptInit_ex(self.ctx, cipher_object, None, None, None):
            raise CipherError("Could not initialize encryption context")
Ejemplo n.º 20
0
	def __init__(self, path):
		self.b = Buffer()
  
		with open(path, 'r') as f:
			self.b.addInfos(f.read())

		self._indexes = dict()
    
		self.b.setCursor(self.b.readInt())
		nbValues = self.b.readInt() / 8;
		for i in range(0, nbValues):
			key = self.b.readInt()
			val = self.b.readInt()

			self._indexes[key] = val
Ejemplo n.º 21
0
 def parse(self, filename):
     size = os.stat(filename).st_size
     remaining = size
     f = open(filename, 'r')
     buf = Buffer(f.read())
     f.close()
     classname = buf.unpack_utf_string()
     paramcount = buf.unpack_int()
     params = {}
     for i in xrange(paramcount):
         name = buf.unpack_utf_string()
         value = buf.unpack_utf_string()
         params[name] = value
     chunklen = buf.unpack_int()
     uncompressedlen = buf.unpack_longlong()
     chunkcount = buf.unpack_int()
     offsets = []
     for i in xrange(chunkcount):
         chunkoffset = buf.unpack_longlong()
         offsets.append(chunkoffset)
     return CompresssionInfo(classname, paramcount, params, uncompressedlen, chunklen, chunkcount, offsets)
Ejemplo n.º 22
0
	def __init__(self, io_loop, iostream, peer_addr):
		"""although iostream has read buffer, but provide a buffer object
		is more helpful for upper logic
		:param
		peer_addr (host, prot)
		"""
		self.io_loop = io_loop
		self._iostream = iostream
		self._read_buffer = Buffer()
		self._peer_addr = peer_addr
		self._connection_cb = None
		self._write_complete_cb = None
		self._message_cb = None
		self._iostream.read_until_close(streaming_callback=self._on_message)
		self._is_connected = True
		self._context = None
Ejemplo n.º 23
0
 def parse(self, filename):
     size = os.stat(filename).st_size
     remaining = size
     f = open(filename, 'r')
     buf = Buffer(f.read())
     f.close()
     entries = []
     while buf.remaining() > 0:
         key = buf.unpack_utf_string()
         pos = buf.unpack_longlong()
         buf.skip_data()
         entries.append((key, pos))
     return IndexInfo(entries)
Ejemplo n.º 24
0
    def __init__(self, backend, sup, drip_rate, drip_time):
        self.backend = backend
        self.vtable = sup.vtable
        self.blocksize = sup.blocksize
        self.headerlen = sup.headerlen
        self.N = sup.total_blocks
        self.K = drip_rate
        self.T = drip_time
        self.fbsize = sup.fbsize
        self.split_maxnum = sup.split_maxnum
        self.split_maxsize = sup.split_maxsize

        self.buf = Buffer()
        self.rlock, self.wlock = get_rw_locks()
        self.syncer = Syncer(self, self.T)

        self.active = False # is the sync thread running
        self.syncing = False # is a sync operation in progress
        self.recent = None # set of (vnode, boff) pairs for what has changed during the sync op
Ejemplo n.º 25
0
def parse(buf):
    if isinstance(buf, basestring):
        buf = Buffer(buf)

    obj = {}
    typ = buf.read('B')
    name = buf.read_string()

    if typ == Type.none:
        obj[name] = parse(buf)
    elif typ == Type.string:
        obj[name] = buf.read_string()
    elif typ in (Type.int32, Type.color, Type.pointer):
        obj[name] = buf.read('<i')
    elif typ == Type.uint64:
        obj[name] = buf.read('<Q')
    elif typ == Type.float32:
        obj[name] = buf.read('<f')

    return obj
Ejemplo n.º 26
0
 def __init__(self, peer):
   self.server = peer.server
   self.peer = peer
   
   ## queue containing all received data
   self.buffer = Buffer();
   
   self.tx = 0
   self.rx = 0
   
   ## just a small optimization for slow connections
   self.expectsize = None
   
   ## all running periodcalls
   self._periods = list();
   
   assert self.headerframe is not None, "headerframe is None"
   
   if self.default:
     self.switch(self.default)
   
   self.sessionInit();
Ejemplo n.º 27
0
	def __init__(self, path):
		self.b = Buffer()

		with open(path, 'r') as f:
			self.b.addInfos(f.read())

		c = self.b.readUTF(3)
		if(cmp(c, "D2O") != 0):
			print("D2O error: {}".format(c))
			raise Exception()

		self._indexes = dict()

		self.b.setCursor(self.b.readUnsignedInt());

		nbValues = self.b.readInt() / 8;
		for i in range(0, nbValues):
			key = self.b.readInt();
			val = self.b.readInt();

			self._indexes[key] = val;

		'''
Ejemplo n.º 28
0
 def addLife(self):
     if not self.lobbier:
         self.coins += 30
         self.sendBin(0x23, Buffer().writeInt8(0))
     for i in range(30):
         self.sendBin(0x21, Buffer().writeInt8(0))
Ejemplo n.º 29
0
# Interactive loop
def read_print_loop():
    """Run a read-print loop for Scheme expressions."""
    while True:
        try:
            src = buffer_input('read> ')
            while src.more_on_line:
                expression = scheme_read(src)
                if expression == 'exit':
                    print()
                    return
                print('str :', expression)
                print('repr:', repr(expression))
        except (SyntaxError, ValueError) as err:
            print(type(err).__name__ + ':', err)
        except (KeyboardInterrupt, EOFError):  # <Control>-D, etc.
            print()
            return


@main
def main(*args):
    if len(args) and '--repl' in args:
        read_print_loop()


#marker 3

print((read_tail(Buffer(tokenize_lines(['2 (3 4))'])))))
Ejemplo n.º 30
0
 def serializePlayerObject(self):
     return Buffer().writeInt16(self.id).writeInt8(self.level).writeInt8(self.zone).writeShor2(self.posX, self.posY).writeInt16(self.skin).writeInt8(self.isDev).toBytes()
Ejemplo n.º 31
0
class MyCog(commands.Cog):
    def __init__(self, bot):
        self.chan = None
        self.bot = bot
        self.run_thread = True
        self.send_thread = Thread(target=lambda: self.packet_queue_thread())
        self.send_buffer = Buffer()

    async def transmit_bulk_packets(self, buffer):
        with buffer.buffer_lock:
            if not len(buffer.packets):
                return

            message = ''
            for packet in buffer.packets:
                message += packet + " "
            message = message[:-1]

            bio = io.BytesIO(message.encode())
            m_file = discord.File(bio)
            await self.chan.send(file=m_file, delete_after=1)
            buffer.clear()
            buffer.signal_free()

    def cog_unload(self):
        self.send_buffer.buffer_lock.release()
        self.run_thread = False
        self.send_thread.join()
        self.autoflusher.cancel()

    @tasks.loop(seconds=1)
    async def autoflusher(self):
        if self.send_buffer.totalSize:
            print(self.send_buffer.totalSize)
            print("autoflushing")
        else:
            print("not flushing, ", self.send_buffer.totalSize)
        await self.transmit_bulk_packets(self.send_buffer)

    def packet_queue_thread(self):
        if not self.chan:
            return
        print("sending has started")
        while self.run_thread:
            packet = tun.read(tun.mtu + 16)
            converted = ''.join([chr(i + int('0x80', 16)) for i in packet])
            self.send_buffer.queue_packet(converted)

    @commands.Cog.listener()
    async def on_message(self, message):
        if not message.author.bot:
            return
        if message.author == self.bot.user:
            return
        if not len(message.attachments):
            return

        data = io.BytesIO()

        await message.attachments[0].save(data)

        packets = data.getvalue().decode().split('\x20')
        for packet in packets:
            decoded_bytes = bytes([ord(i) - int('0x80', 16) for i in packet])
            tun.write(decoded_bytes)

    @commands.Cog.listener()
    async def on_ready(self):
        print('Ready!')
        print('Logged in as ---->', self.bot.user)
        print('ID:', self.bot.user.id)
        self.chan = discord.utils.get(bot.get_all_channels(), name="general")
        print("channel is: {}".format(self.chan))
        if not self.chan:
            bot.remove_cog('MyCog')

        self.autoflusher.start()
        self.send_thread.start()
Ejemplo n.º 32
0
def mem_replay(introspector,
               qbuf,
               dbuf,
               device,
               times='3,5',
               batch_size_inference=16):
    '''
        times: increased number of blocks each replay.
    '''
    times = [int(x) for x in times.split(',')]
    inputs = torch.zeros(3,
                         batch_size_inference,
                         CAPACITY,
                         dtype=torch.long,
                         device=device)
    B_set = []  # the poses of B blks in qbuf
    for k, inc in enumerate(times):
        num_to_keep = len(qbuf) + inc
        # stage one: continuous
        estimations = torch.zeros(len(dbuf), device='cpu')
        bufs, t = qbuf.fill(dbuf), 0
        for i in range((len(bufs) - 1) // batch_size_inference + 1):
            l, r = batch_size_inference * i, min(
                len(bufs), batch_size_inference * (i + 1))
            for j, buf in enumerate(bufs[l:r]):
                buf.export(out=(inputs[0, j], inputs[1, j], inputs[2, j]))
            logits = introspector(*inputs[:, :r - l]).sigmoid_()
            for j, buf in enumerate(bufs[l:r]):
                estimation = _score_blocks(buf, logits[j])[len(qbuf):]
                estimations[t:t + len(estimation)] = estimation
                t += len(estimation)
        assert t == len(dbuf)

        # estimations = positional_smoothing(dbuf, estimations)
        # fill the buffer up
        indices = estimations.argsort(descending=True)
        qbuf_size = qbuf.calc_size()
        for idx in indices:
            if qbuf_size + len(dbuf[idx]) > CAPACITY:
                break
            if dbuf[idx] in B_set:
                continue
            qbuf_size += len(dbuf[idx])
            qbuf.insert(dbuf[idx])

        # keep only num_to_keep blks
        qbuf.export(out=(inputs[0, 0], inputs[1, 0], inputs[2, 0]))
        relevance_token = torch.sigmoid(introspector(*inputs[:, :1]).view(-1))
        relevance_blk = _score_blocks(qbuf, relevance_token)
        keeped_indices = relevance_blk.argsort(descending=True)
        if len(keeped_indices) > num_to_keep and k < len(times) - 1:
            keeped_indices = keeped_indices[:num_to_keep]
        else:
            return qbuf, relevance_blk
        # manually filtering
        filtered_qbuf, filtered_relevance_blk = Buffer(), []
        for i, blk in enumerate(qbuf):
            if i in keeped_indices:
                filtered_qbuf.blocks.append(blk)
                filtered_relevance_blk.append(relevance_blk[i])
        qbuf = filtered_qbuf
        # record the blocks already in the qbuf
        B_set = [blk for blk in qbuf if blk.blk_type == 1]
    return filtered_qbuf, torch.tensor(filtered_relevance_blk)
Ejemplo n.º 33
0
 def test_oversized_data(self):
     data = np.zeros(10)
     B = Buffer(data=data, resizeable=False)
     with self.assertRaises(ValueError):
         B.set_data(np.ones(20))
Ejemplo n.º 34
0
def buffer_input():
    return Buffer(tokenize_lines(InputReader('> ')))
Ejemplo n.º 35
0
 def sendBin(self, code, buff):
     msg = Buffer().writeInt8(code).write(
         buff.toBytes() if isinstance(buff, Buffer) else buff).toBytes()
     self.sendMessage(msg, True)
Ejemplo n.º 36
0
def run_ppo(epochs=30,epoch_steps = 4000 , max_ep_len=500 ,pi_lr = 3e-4, vf_lr=1e-3,
            gamma=0.99,lam=0.97,pi_iters = 80,target_kl = 0.01,val_iters=80, clip_ratio=0.2,
            hidden_sizes = (64,64), act_dim=None, obs_dim= None, action_scale=None,n_proc = 2, model_path = SAVE_MODEL_PATH):

    if not(act_dim and obs_dim):
            logging.info("Missing action or obs dimension")

    if action_scale is None:
        action_scale = np.ones(act_dim)

    #tensorflow graph inputs
    with tf.name_scope('graph_inputs'):
        obs_ph = tf.placeholder(dtype = tf.float32, shape=(None,obs_dim), name='observations')
        act_ph = tf.placeholder(dtype = tf.float32, shape=(None,act_dim), name='actions')
        adv_ph = tf.placeholder(dtype = tf.float32, shape=(None,), name='advantage')
        ret_ph = tf.placeholder(dtype = tf.float32, shape=(None,), name='return')
        logp_old_ph = tf.placeholder(dtype = tf.float32, shape=(None,), name='logp_old')

    graph_inputs = [obs_ph, act_ph, adv_ph, ret_ph, logp_old_ph]
    

    #tensorflow graph outputs
    pi, logp, logp_pi, val = actor_critic(obs_ph, act_ph, action_scale, hidden_sizes)

    #experience buffer
    buf = Buffer(obs_dim, act_dim, epoch_steps, gamma, lam)


    #count variables
    var_counts = tuple(count_vars(scope) for scope in ['pi', 'v'])
    logging.info('\nNumber of parameters: \t pi: %d, \t v: %d\n'%var_counts)

    #loss functions
    with tf.name_scope('pi_loss'):
        ratio = tf.exp(logp - logp_old_ph)      #pi(a|s) / pi_old(a|s)
        min_adv = tf.where(adv_ph > 0, (1 + clip_ratio) * adv_ph, (1 - clip_ratio) * adv_ph)
        pi_loss = -tf.reduce_mean(tf.minimum(ratio * adv_ph, min_adv))

    with tf.name_scope('val_loss'):
        v_loss = tf.reduce_mean((ret_ph - val)**2)

    #avg_ret = tf.reduce_mean(ret_ph)
    #avg_ret_ph = tf.placeholder(tf.float32, shape=None,name='return_summary')
    #ret_summary = tf.summary.scalar('ret_summ', avg_ret_ph)

    # Info (useful to watch during learning)
    with tf.name_scope('kl_divergence'):
        approx_kl = tf.reduce_mean(logp_old_ph - logp)  # a sample estimate for KL-divergence, easy to compute
        clipped = tf.logical_or(ratio > (1 + clip_ratio), ratio < (1 - clip_ratio))
        clipfrac = tf.reduce_mean(tf.cast(clipped, tf.float32))

        #kl_ph = tf.placeholder(tf.float32,shape=None,name='kl_summary')
        #kl_summary = tf.summary.scalar('kl_summ', kl_ph)
    
    
    with tf.name_scope('entropy'):
        approx_ent = tf.reduce_mean(-logp)  # a sample estimate for entropy, also easy to compute
        #ent_ph = tf.placeholder(tf.float32,shape=None,name='entropy')
        #ent_summary = tf.summary.scalar('ent_summ', ent_ph)


    #with tf.name_scope('loss'):
        #pi_loss_ph = tf.placeholder(tf.float32,shape=None,name='piloss_summary')
        #v_loss_ph = tf.placeholder(tf.float32,shape=None,name='vloss_summary')
        
        #pi_loss_summary = tf.summary.scalar('pi_loss_summ', pi_loss_ph)
        #v_loss_summary = tf.summary.scalar('val_loss_summ', v_loss_ph)
        
    
    #performance_summaries = tf.summary.merge([pi_loss_summary,v_loss_summary,kl_summary, ent_summary,ret_summary])

    #optimizer
    with tf.name_scope('Train_pi'):
        opt_pi = tf.train.AdamOptimizer(learning_rate = pi_lr).minimize(pi_loss)
    
    with tf.name_scope('Train_val'):
        opt_val = tf.train.AdamOptimizer(learning_rate = vf_lr).minimize(v_loss)

    sess = tf.Session()

    #if not os.path.exists('summaries'):
       # os.mkdir('summaries')
    #if not os.path.exists(os.path.join('summaries','first')):
        #os.mkdir(os.path.join('summaries','first'))
    
    #summ_writer = tf.summary.FileWriter(os.path.join('summaries','first'), sess.graph)

    sess.run(tf.global_variables_initializer())

    #gradient update
    def update():
        #create input dictionary from trajector and graph inputs
        inputs =  {g:t for g,t in zip(graph_inputs,buf.get())}

        pi_l_old, val_l_old, ent = sess.run([pi_loss, v_loss, approx_ent], feed_dict=inputs)

        #Training
        for i in range(pi_iters):
            _, kl, rat, min, l , l_old = sess.run([opt_pi, approx_kl, ratio, min_adv, logp, logp_old_ph], feed_dict=inputs)
            kl = np.mean(kl)
            if kl > 1.5 * target_kl:
                logging.info('Early stopping at step %d due to reaching max kl.'%i)
                print("kl too big", kl, "after", i+1)
                break

        #value function training
        for i in range(val_iters):
            sess.run(opt_val, feed_dict=inputs)

        pi_l_new, val_l_new, kl, cf = sess.run([pi_loss, v_loss, approx_kl, clipfrac], feed_dict=inputs)
        update_info = "policy loss: " + str(pi_l_old) + \
                      "\n" + "value func loss:" + str(val_l_old) + "\n" +\
                      "delta policy loss: " + str(pi_l_new-pi_l_old) + "\n" +\
                      "delta value func loss: " + str(val_l_new - val_l_old) + "\n" +\
                      "kl: " + str(kl) + "\n" +"entropy: " + str(ent) + "\n"

        print(update_info)
        logging.info(update_info)

        return [pi_l_new, val_l_new, kl, cf, ent]

        #summ = sess.run(performance_summaries, feed_dict={pi_loss_ph:pi_l_new, v_loss_ph:val_l_new, kl_ph:kl, ent_ph:ent, avg_ret_ph:ret})
        #summ_writer.add_summary(summ, epoch)

    #initialize model saving
    saver = ModelSaver(sess, inputs = {"obs":obs_ph},outputs={"pi": pi, "val": val, "logp_pi": logp_pi}, export_dir= model_path)

    #experience and training loop
    for epoch in range(epochs):
        # save model
        saver.save_model()

        #build world files only for first epoch
        if epoch == 0:
            first = True

        #timer
        t = time.time()

        epoch_data = run_job(n_proc=n_proc, total_steps=epoch_steps, max_ep_steps= max_ep_len,model_path =saver.fpath,build_files=first)

        runtime = time.time() - t

        #save epoch data
        max_ret, min_ret, avg_return, max_len, min_len, avg_len, avg_ene, avg_clipped, avg_dist, avg_abs_dist = buf.store_epoch(epoch_data)

        epoch_info = [max_ret, min_ret, avg_return, max_len, min_len, avg_len, avg_ene, avg_clipped, avg_dist, avg_abs_dist]


        ep_info = "============Epoch " + str(epoch) + " max/min/avg return " + str('%.3f'%max_ret)\
                  +" " + str('%.3f'%min_ret) + " " + str('%.3f'%avg_return) + " max/min/avg length "\
                  + " " + str('%.3f'%max_len) + " " + str('%.3f'%min_len) +" "+ str('%.3f'%avg_len) \
                  + " avg energy/action_clip/distance/abs_distance" \
                  + " " + str('%.3f'%avg_ene) + " " + str('%.3f'%avg_clipped) +" "+ str('%.3f'%avg_dist)+" " \
                  + str('%.3f'%avg_abs_dist) + " time " + str(runtime) + " "\
                  + "============"
        print(ep_info)
        logging.info(ep_info)

        #update policy
        update_info = update()

        with open("sum_ep"+str(epoch)+".p", "wb") as f:
            pickle.dump([epoch_info, update_info], f)
Ejemplo n.º 37
0
    parser.add_argument('--src', required=True, \
        help='nn cambricon code file')
    
    args = parser.parse_args()
    
    return args.cfg, args.src


if __name__ == '__main__':

    # computation unit and buffer

    MUT = Mmunit(HEIGHT, WIDTH, DEPTH)
    VUT = Vecunit(WIDTH)

    UBUF = Buffer(8*MB/4, 358*GB/4, 100, 'UBUF')
    WBUF = Buffer(4*MB/4, 358*GB/4, 100, 'WBUF')
    ACCQ = Buffer(WIDTH*DEPTH, 358*GB/4, 0, 'ACCQ')
    
    # random container generator
    # for sample, just all fc layer instance

    container_1 = Container()
    container_2 = Container()
    container_3 = Container()
    container_4 = Container()

    layer1 = Layer(Type.FC, batch=200, in_dim=100, out_dim=400)
    layer2 = Layer(Type.FC, batch=200, in_dim=400, out_dim=400, previous_input=True)
    layer3 = Layer(Type.FC, batch=200, in_dim=400, out_dim=400, previous_input=True)
    layer4 = Layer(Type.FC, batch=200, in_dim=400, out_dim=10, previous_input=True)
Ejemplo n.º 38
0
    def __init__(self, config, rng):
        self.config = config
        self.rng = rng

        self.task = config.task
        self.model_dir = config.model_dir
        self.gpu_memory_fraction = config.gpu_memory_fraction

        self.log_step = config.log_step
        self.max_step = config.max_step

        self.K_d = config.K_d
        self.K_g = config.K_g
        self.initial_K_d = config.initial_K_d
        self.initial_K_g = config.initial_K_g
        self.checkpoint_secs = config.checkpoint_secs

        DataLoader = {
            'gaze': gaze_data.DataLoader,
            'hand': hand_data.DataLoader,
        }[config.data_set]
        self.data_loader = DataLoader(config, rng=self.rng)

        self.model = Model(config, self.data_loader)
        self.history_buffer = Buffer(config, self.rng)

        self.summary_ops = {
            'test_synthetic_images': {
                'summary':
                tf.summary.image("test_synthetic_images",
                                 self.model.resized_x,
                                 max_outputs=config.max_image_summary),
                'output':
                self.model.resized_x,
            },
            'test_refined_images': {
                'summary':
                tf.summary.image("test_refined_images",
                                 self.model.denormalized_R_x,
                                 max_outputs=config.max_image_summary),
                'output':
                self.model.denormalized_R_x,
            }
        }

        self.saver = tf.train.Saver()
        self.summary_writer = tf.summary.FileWriter(self.model_dir)

        sv = tf.train.Supervisor(logdir=self.model_dir,
                                 is_chief=True,
                                 saver=self.saver,
                                 summary_op=None,
                                 summary_writer=self.summary_writer,
                                 save_summaries_secs=300,
                                 save_model_secs=self.checkpoint_secs,
                                 global_step=self.model.discrim_step)

        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=self.gpu_memory_fraction,
            allow_growth=True)  # seems to be not working
        sess_config = tf.ConfigProto(allow_soft_placement=True,
                                     gpu_options=gpu_options)

        self.sess = sv.prepare_or_wait_for_session(config=sess_config)
Ejemplo n.º 39
0
    def forward(self, sentence, transitions, num_ops, other_sent,
                teacher_prob):
        batch_size, sent_len, _ = sentence.size()
        out = self.word(sentence)  # batch, |sent|, h * 2s

        # batch normalization and dropout
        if not self.args.no_batch_norm:
            out = out.transpose(1, 2).contiguous()
            out = self.batch_norm1(
                out
            )  # batch,  h * 2, |sent| (Normalizes batch * |sent| slices for each feature
            out = out.transpose(1, 2)

        if self.args.dropout_rate_input > 0:
            out = self.dropout(out)  # batch, |sent|, h * 2

        (h_sent,
         c_sent) = torch.chunk(out, 2,
                               2)  # ((batch, |sent|, h), (batch, |sent|, h))

        buffer_batch = [
            Buffer(h_s, c_s, self.args)
            for h_s, c_s in zip(list(torch.split(h_sent, 1, 0)),
                                list(torch.split(c_sent, 1, 0)))
        ]

        stack_batch = [create_stack(self.args) for _ in buffer_batch]

        if self.args.tracking:
            self.track.initialize_states(other_sent)
        else:
            assert transitions is not None

        if transitions is None:
            num_transitions = (2 * sent_len) - 1
        else:
            transitions_batch = [
                trans.squeeze(1)
                for trans in list(torch.split(transitions, 1, 1))
            ]
            num_transitions = len(transitions_batch)

        lstm_actions, true_actions = [], []

        for time_stamp in range(num_transitions):
            ops_left = num_transitions - time_stamp

            reduce_ids = []
            reduce_lh, reduce_lc = [], []
            reduce_rh, reduce_rc = [], []
            reduce_valences = []
            reduce_tracking_states = []
            teacher_valences = None
            if self.args.tracking:
                valences, tracking_state = self.update_tracker(
                    buffer_batch, stack_batch, batch_size)
                _, pred_trans = valences.max(dim=1)
                if self.training and self.args.teacher:
                    use_teacher = True  # TODO for now always use teacher - later --> random() < teacher_prob
                    if use_teacher and self.args.continuous_stack:
                        teacher_valences = cudify(
                            self.args,
                            Variable(torch.zeros(valences.size()),
                                     requires_grad=False))

                    temp_trans = transitions_batch[time_stamp]

                    for b_id in range(batch_size):
                        if temp_trans[b_id].data[0] > PAD:
                            true_actions.append(temp_trans[b_id])
                            lstm_actions.append(valences[b_id].unsqueeze(0))

                            if teacher_valences is not None:
                                teacher_valences[
                                    b_id, temp_trans[b_id].data[0]] = 1.0

                    temp_trans = temp_trans.data if use_teacher else pred_trans.data
                else:
                    temp_trans = pred_trans.data
            else:
                valences = None
                temp_trans = transitions_batch[time_stamp].data

            for b_id in range(batch_size):
                stack_size, buffer_size = stack_batch[b_id].size(
                ), buffer_batch[b_id].size()
                # this sentence is done!
                my_ops_left = num_ops[b_id] - time_stamp
                if my_ops_left <= 0:
                    # should coincide with teacher padding or else num_ops has a bug
                    if self.training and self.args.teacher:
                        assert temp_trans[b_id] == PAD
                    continue
                else:
                    act = temp_trans[b_id]

                    # ensures it's a valid act according to state of buffer, batch, and timestamp
                    # safe check actions if not using teacher forcing... or using teacher forcing but in evaluation
                    if self.args.tracking and (not self.args.teacher or
                                               (self.args.teacher
                                                and not self.training)):
                        act, act_ignored = self.resolve_action(
                            buffer_batch[b_id], stack_batch[b_id], buffer_size,
                            stack_size, act, time_stamp, my_ops_left)

                if self.args.tracking:
                    # use teacher valences over predicted valences
                    if teacher_valences is not None:
                        reduce_valence, shift_valence = teacher_valences[b_id]
                    else:
                        reduce_valence, shift_valence = valences[b_id]
                else:
                    reduce_valence, shift_valence = None, None

                no_action = True

                # 2 - REDUCE
                if act == REDUCE or (self.args.continuous_stack
                                     and not self.args.teacher
                                     and stack_size >= 2):
                    no_action = False
                    reduce_ids.append(b_id)

                    r = stack_batch[b_id].peek()
                    if not stack_batch[b_id].pop(reduce_valence):
                        print(sentence[b_id, :, :].sum(dim=1),
                              transitions[b_id, :])
                        raise Exception("Tried to pop from an empty list.")

                    l = stack_batch[b_id].peek()
                    if not stack_batch[b_id].pop(reduce_valence):
                        print(sentence[b_id, :, :].sum(dim=1),
                              transitions[b_id, :])
                        raise Exception("Tried to pop from an empty list.")

                    reduce_lh.append(l[0])
                    reduce_lc.append(l[1])
                    reduce_rh.append(r[0])
                    reduce_rc.append(r[1])

                    if self.args.tracking:
                        reduce_valences.append(reduce_valence)
                        reduce_tracking_states.append(
                            tracking_state[b_id].unsqueeze(0))

                # 3 - SHIFT
                if act == SHIFT or (self.args.continuous_stack
                                    and not self.args.teacher
                                    and buffer_size > 0):
                    no_action = False
                    word = buffer_batch[b_id].pop()
                    stack_batch[b_id].add(word, shift_valence, time_stamp)

                if no_action:
                    print(
                        "\n\nWarning: Didn't choose an action.  Look for a bug!  Attempted %d action but was denied!"
                        % act)

            if len(reduce_ids) > 0:
                h_lefts = torch.cat(reduce_lh)
                c_lefts = torch.cat(reduce_lc)
                h_rights = torch.cat(reduce_rh)
                c_rights = torch.cat(reduce_rc)

                if self.args.tracking:
                    e_out = torch.cat(reduce_tracking_states)
                    h_outs, c_outs = self.reduce((h_lefts, c_lefts),
                                                 (h_rights, c_rights), e_out)
                else:
                    h_outs, c_outs = self.reduce((h_lefts, c_lefts),
                                                 (h_rights, c_rights))

                for i, state in enumerate(zip(h_outs, c_outs)):
                    reduce_valence = reduce_valences[
                        i] if self.args.tracking else None
                    stack_batch[reduce_ids[i]].add(state, reduce_valence)

        outputs = []
        for (i, stack) in enumerate(stack_batch):
            if not self.args.continuous_stack:
                if not stack.size() == 1:
                    print("Stack size is %d.  Should be 1" % stack.size())
                    assert stack.size() == 1
            top_h = stack.peek()[0]
            outputs.append(top_h)

        if len(true_actions) > 0 and self.training:
            return torch.cat(outputs), torch.cat(true_actions), torch.log(
                torch.cat(lstm_actions))
        return torch.cat(outputs), None, None
Ejemplo n.º 40
0
class Agent:
    def __init__(self, input_dim, output_dim, lr, gamma):
        self.input_dim = input_dim
        self.output_dim = output_dim
        self.actions = range(output_dim)
        self.lr = lr
        self.gamma = gamma
        self.epsilon = 0.2
        self.batchsize = 32
        self.memory_size = 2000
        self.buffer = Buffer(output_dim, self.memory_size, self.batchsize)

        #Make neural nets
        self.model = self._make_model()
        #self.model = self._make_model_atari()
        self.target_model = self._make_model()
        #self.target_model = self._make_model_atari()
        self.target_model.set_weights(
            self.model.get_weights())  # clone the networks
        self.C = 1000  # hard update parameter
        self.tau = 0.1  # this is the soft update parameter -- see 'def update_target_network'

    def _make_model(self):

        model = Sequential()
        model.add(Dense(64, input_dim=self.input_dim, activation='relu'))
        model.add(Dense(64, activation='relu'))
        model.add(Dense(self.output_dim, activation='linear'))
        model.compile(loss='mse', optimizer=Adam(lr=self.lr))

        return model

    #Taken from https://becominghuman.ai/lets-build-an-atari-ai-part-1-dqn-df57e8ff3b26
    def _make_model_atari(self):

        n_actions = self.output_dim

        # We assume a theano backend here, so the "channels" are first.
        ATARI_SHAPE = (4, 105, 80)

        # With the functional API we need to define the inputs.
        frames_input = Input(ATARI_SHAPE, name='frames')
        actions_input = Input((n_actions, ), name='mask')

        # Assuming that the input frames are still encoded from 0 to 255. Transforming to [0, 1].
        normalized = Lambda(lambda x: x / 255.0)(frames_input)

        conv_1 = Convolution2D(32, (3, 3),
                               activation='relu',
                               data_format='channels_first')(normalized)
        conv_2 = Convolution2D(32, (3, 3), activation='relu')(conv_1)
        # Flattening the second convolutional layer.
        conv_flattened = Flatten()(conv_2)
        # "The final hidden layer is fully-connected and consists of 256 rectifier units."
        hidden = Dense(256, activation='relu')(conv_flattened)
        # "The output layer is a fully-connected linear layer with a single output for each valid action."
        output = Dense(n_actions)(hidden)
        # Finally, we multiply the output by the mask!
        #filtered_output = merge([output, actions_input], mode='mul')
        filtered_output = concatenate([output, actions_input])
        model = Model(input=[frames_input, actions_input],
                      output=filtered_output)
        optimizer = RMSprop(lr=0.00025, rho=0.95, epsilon=0.01)
        model.compile(optimizer, loss='mse')

        return model

    def remember(self, state, action, reward, next_state, done):

        #Find TD error:  abs(Q(s,a) - yi)
        #First find action from behavior network
        Q_next = self.model.predict(self.make_tensor(next_state))[0]
        action_next_best = np.argmax(Q_next)

        #Then find the yi
        Q_next_target = self.target_model.predict(
            self.make_tensor(next_state))[0]
        yi = reward + (1 - done) * self.gamma * Q_next_target[action_next_best]

        #Then find Q(s,a)
        Q_vec = self.target_model.predict(self.make_tensor(state))[0]
        action_scalar = np.argmax(Q_vec)  #actions are stored as 1 hots
        Q = Q_vec[action_scalar]

        #Define td_error
        td_error = abs(yi - Q)

        self.buffer.remember(state, action, reward, next_state, done, td_error)

    def act(self, state):
        """ epsilon greedy """

        if np.random.rand() <= self.epsilon:
            action = np.random.choice(self.actions)
        else:
            Qs = self.model.predict(state)[
                0]  #keras returns a tensor (2d array), so take first element
            action = np.argmax(Qs)

        return action

    def replay(self, prioritised=False):
        """ Does experience replay.
        """

        #grab random batch
        S, A, R, S1, D, TD = self.buffer.get_batch(prioritised=prioritised)

        #instantiate
        states = []
        Q_wants = []

        #Find updates
        for i in range(len(S)):
            state, action, reward, next_state, done, td = S[i], A[i], R[i], S1[
                i], D[i], TD[i]
            action = np.argmax(action)  #convert from 1-hot
            states.append(state)

            #Find Q_target
            state_tensor = np.reshape(state,
                                      (1, len(state)))  # keras takes 2d arrays
            Q_want = self.model.predict(state_tensor)[
                0]  # all elements of this, except the action chosen, stay
            # the same

            #If state is terminal, Q_target(action) = reward
            if done == True:
                Q_want[action] = reward

            # Q_want(action) = reward + gamma*max_a Q_target(next_state, a*)  -- note I sample from the target network
            # where a* = argmax Q(next_state,a)
            # Doing this -- i.e. finding a* from the behavior network, is what
            # distinguihses DDQN from DQN
            else:
                next_state_tensor = np.reshape(next_state,
                                               (1, len(next_state)))
                Q_next_state_vec = self.model.predict(next_state_tensor)
                action_max = np.argmax(Q_next_state_vec)

                Q_target_next_state_vec = self.target_model.predict(
                    next_state_tensor)[0]
                Q_target_next_state_max = Q_target_next_state_vec[action_max]

                Q_want[action] = reward + self.gamma * Q_target_next_state_max
                Q_want_tensor = np.reshape(Q_want, (1, len(Q_want)))
                #self.model.fit(state_tensor,Q_want_tensor,verbose=False,epochs=1)

            Q_wants.append(Q_want)

        #Here I fit on the whole batch. Others seem to fit line-by-line
        #Dont' think (hope) it makes much difference
        states = np.array(states)
        Q_wants = np.array(Q_wants)
        self.model.fit(states, Q_wants, verbose=False, epochs=1)

    def hard_update_target_network(self, step):
        """ Here the target network is updated every K timesteps
            By update, I mean clone the behavior network.
        """

        if step % self.C == 0:
            pars = self.model.get_weights()
            self.target_model.set_weights(pars)

    def soft_update_target_network(self):
        """  Here the target network is updated every timestep,
             according to 
             
             theta_target = (1-tau)*theta_target + theta_behavior*tau
             
             where,
             tau = parameter (the smaller, the softer)
             theta_target = parameters from the target network
             theta_behaviour = parameters from the behavior network
  
        """

        pars_behavior = self.model.get_weights(
        )  # these have form [W1, b1, W2, b2, ..], Wi = weights of layer i
        pars_target = self.target_model.get_weights()  # bi = biases in layer i

        ctr = 0
        for par_behavior, par_target in zip(pars_behavior, pars_target):
            par_target = par_target * (1 - self.tau) + par_behavior * self.tau
            pars_target[ctr] = par_target
            ctr += 1

        self.target_model.set_weights(pars_target)

    def make_tensor(self, vec):
        """ Reshapes a 1-hot vector
            into a 1-hot tensor -- keras requires the tensor
        """

        return np.reshape(vec, (1, len(vec)))
class HG_DAGGER:
    def __init__(self, dim_a, action_upper_limits, action_lower_limits, buffer_min_size, buffer_max_size,
                 buffer_sampling_rate, buffer_sampling_size, number_training_iterations, train_end_episode):
        # Initialize variables
        self.dim_a = dim_a
        self.action_upper_limits = str_2_array(action_upper_limits, type_n='float')
        self.action_lower_limits = str_2_array(action_lower_limits, type_n='float')
        self.count = 0
        self.buffer_sampling_rate = buffer_sampling_rate
        self.buffer_sampling_size = buffer_sampling_size
        self.number_training_iterations = number_training_iterations
        self.train_end_episode = train_end_episode

        # Initialize HG_DAgger buffer
        self.buffer = Buffer(min_size=buffer_min_size, max_size=buffer_max_size)

    def feed_h(self, h):
        self.h = np.reshape(h, [1, self.dim_a])

    def action(self, neural_network, state_representation):
        self.count += 1

        if np.any(self.h):  # if feedback, human teleoperates
            action = self.h
            print("feedback:", self.h[0])
        else:
            action = neural_network.sess.run(neural_network.policy_output,
                                             feed_dict={'policy/state_representation:0': state_representation})

        out_action = []

        for i in range(self.dim_a):
            action[0, i] = np.clip(action[0, i], -1, 1) * self.action_upper_limits[i]
            out_action.append(action[0, i])

        return np.array(out_action)

    def train(self, neural_network, transition_model, action, t, done):
        # Add last step to memory buffer
        if transition_model.last_step(action) is not None and np.any(self.h):  # if human teleoperates, add action to database
            self.buffer.add(transition_model.last_step(action))

        # Train policy every k time steps from buffer
        if self.buffer.initialized() and (t % self.buffer_sampling_rate == 0 or (self.train_end_episode and done)):
            for i in range(self.number_training_iterations):
                if i % (self.number_training_iterations / 20) == 0:
                    print('Progress Policy training: %i %%' % (i / self.number_training_iterations * 100))

                batch = self.buffer.sample(batch_size=self.buffer_sampling_size)
                observation_sequence_batch = [np.array(pair[0]) for pair in batch]  # state(t) sequence
                action_sequence_batch = [np.array(pair[1]) for pair in batch]
                current_observation_batch = [np.array(pair[2]) for pair in batch]  # last
                action_label_batch = [np.array(pair[3]) for pair in batch]

                state_representation_batch = transition_model.get_state_representation_batch(neural_network,
                                                                                             observation_sequence_batch,
                                                                                             action_sequence_batch,
                                                                                             current_observation_batch)

                neural_network.sess.run(neural_network.train_policy,
                                        feed_dict={'policy/state_representation:0': state_representation_batch,
                                                   'policy/policy_label:0': action_label_batch})
Ejemplo n.º 42
0
def read_line(line):
    """Read a single string LINE as a Scheme expression."""
    return scheme_read(Buffer(tokenize_lines([line])))
Ejemplo n.º 43
0
def buffer_lines(lines, prompt="scm> "):
    """Return a Buffer instance iterating through LINES."""
    return Buffer(tokenize_lines(LineReader(lines, prompt)))
    if args.reproc:
        np.random.seed(run)
        torch.manual_seed(run)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False

    # CLASSIFIER
    if args.use_conv:
        model = ResNet18(args.n_classes, nf=20, input_size=args.input_size)
    else:
        model = MLP(args)
    if args.cuda:
        model = model.to(args.device)

    opt = torch.optim.SGD(model.parameters(), lr=args.lr)
    buffer = Buffer(args)
    if run == 0:
        print("number of classifier parameters:",
              sum([np.prod(p.size()) for p in model.parameters()]))
        print("buffer parameters: ", np.prod(buffer.bx.size()))

    #----------
    # Task Loop
    for task, tr_loader in enumerate(train_loader):
        sample_amt = 0

        model = model.train()

        #---------------
        # Minibatch Loop
        for i, (data, target) in enumerate(tr_loader):
Ejemplo n.º 45
0
    def handlePkt(self, code, b, pktData):
        if code == 0x10: # CREATE_PLAYER_OBJECT
            level, zone, pos = b.readInt8(), b.readInt8(), b.readShor2()
            self.level = level
            self.zone = zone
            
            self.dead = False
            self.client.stopDCTimer()
            
            self.match.broadBin(0x10, Buffer().writeInt16(self.id).write(pktData).writeInt16(self.skin))

        elif code == 0x11: # KILL_PLAYER_OBJECT
            if self.dead:
                return
            
            self.dead = True
            self.client.startDCTimer(60)
            
            self.match.broadBin(0x11, Buffer().writeInt16(self.id))
            
        elif code == 0x12: # UPDATE_PLAYER_OBJECT
            if self.dead or self.lastUpdatePkt == pktData:
                return

            level, zone, pos, sprite, reverse = b.readInt8(), b.readInt8(), b.readVec2(), b.readInt8(), b.readBool()

            if self.level != level or self.zone != zone:
                self.match.onPlayerWarp(self, level, zone)
                
            self.level = level
            self.zone = zone
            self.posX = pos[0]
            self.posY = pos[1]
            self.lastUpdatePkt = pktData

            if sprite > 5 and self.match.world == "lobby" and zone == 0:
                self.client.block(0x1)
                return
            
            self.match.broadPlayerUpdate(self, pktData)
            
        elif code == 0x13: # PLAYER_OBJECT_EVENT
            if self.dead:
                return

            type = b.readInt8()

            if self.match.world == "lobby":
                self.client.block(0x2)
                return
            
            self.match.broadBin(0x13, Buffer().writeInt16(self.id).write(pktData))

        elif code == 0x17:
            killer = b.readInt16()
            if self.id == killer:
                return
            
            killer = self.match.getPlayer(killer)
            if killer is None:
                return

            killer.sendBin(0x17, Buffer().writeInt16(self.id).write(pktData))

        elif code == 0x18: # PLAYER_RESULT_REQUEST
            if self.dead or self.win:
                return

            self.win = True
            self.client.startDCTimer(120)

            pos = self.match.getWinners()
            if self.server.discordWebhook is not None and pos == 1 and not self.match.private:
                name = self.name
                # We already filter players that have a squad so...
                if len(self.team) == 0 and self.server.checkCurse(self.name):
                    name = "[ censored ]"
                embed = DiscordEmbed(description='**%s** has achieved **#1** victory royale!' % name, color=0xffff00)
                self.server.discordWebhook.add_embed(embed)
                self.server.discordWebhook.execute()
                self.server.discordWebhook.remove_embed(0)
            
            self.match.broadBin(0x18, Buffer().writeInt16(self.id).writeInt8(pos).writeInt8(0))
            
        elif code == 0x19:
            self.trustCount += 1
            if self.trustCount > 8:
                self.client.block(0x3)

        elif code == 0x20: # OBJECT_EVENT_TRIGGER
            if self.dead:
                return

            level, zone, oid, type = b.readInt8(), b.readInt8(), b.readInt32(), b.readInt8()

            if self.match.world == "lobby" and oid == 458761:
                self.match.goldFlowerTaken = True

            self.match.broadBin(0x20, Buffer().writeInt16(self.id).write(pktData))
            
        elif code == 0x30: # TILE_EVENT_TRIGGER
            if self.dead:
                return

            level, zone, pos, type = b.readInt8(), b.readInt8(), b.readShor2(), b.readInt8()

            self.match.broadBin(0x30, Buffer().writeInt16(self.id).write(pktData))
Ejemplo n.º 46
0
 def test_offlimit_offset(self):
     data = np.zeros(10)
     B = Buffer(data=data, resizeable=False)
     with self.assertRaises(ValueError):
         B.set_data(np.ones(1),offset=10*data.dtype.itemsize)
Ejemplo n.º 47
0
class Switch:

    LOCAL_ADDR = -1

    def __init__(self, _config):
        _srv_gen = PROCS[_config["SRV_PROC"]](_config["SRV_RATE"], None)
        _comp_gen = PROCS[_config["COMP_PROC"]](_config["COMP_COST"], None)

        self.__id = id_manager.get_next_id("Switch")
        self.__arr_buffers = DataSource(_config)
        self.__arr_buffers.init_fillup()

        self.__proc_buffer = Buffer()  # processing buffer
        self.__srv_gen = _srv_gen
        self.__srv_cap = 0
        self.__assoc_elements = dict({self.LOCAL_ADDR: self})
        self.__cost_table = dict({self.LOCAL_ADDR: _comp_gen})

        self.__next_costs = dict({})
        self.__comm_cost = 0
        self.__comp_cost = 0

        self.generate_srv_rate()

    def add_assoc_controllers(self, _controllers, _comm_cost_gens):
        self.__assoc_elements.update(_controllers)
        self.__cost_table.update(_comm_cost_gens)
        self.generate_next_unit_costs()

    def admit_reqs(self):
        return self.__arr_buffers.slide()

    def forward(self, _addr, _num):
        _reqs = self.__arr_buffers.emit(_num)
        _actual_num = len(_reqs)
        self.__assoc_elements[_addr].store(_reqs)

        self.__clear_costs()

        # compute the corresponding communication and computational costs
        if _addr == self.LOCAL_ADDR:
            self.__comp_cost = _actual_num * self.next_unit_costs[
                self.LOCAL_ADDR]
        else:
            self.__comm_cost = _actual_num * self.next_unit_costs[_addr]

        self.generate_next_unit_costs()

    def store(self, _reqs):
        self.__proc_buffer.put(_reqs)

    def serve(self, _t):
        _actual_num = min(self.next_srv_rate, self.proc_buffer_size)
        _served_reqs = self.__proc_buffer.serve(_actual_num)

        self.generate_srv_rate()

        for req in _served_reqs:
            req.set_fin_time(_t)

        return _served_reqs

    def __clear_costs(self):
        self.__comm_cost = 0
        self.__comp_cost = 0

    def generate_next_unit_costs(self):
        self.__next_costs = {
            _addr: _gen.next
            for _addr, _gen in self.__cost_table.items()
        }

    def generate_srv_rate(self):
        self.__srv_cap = self.__srv_gen.next

    @property
    def incurred_costs(self):
        return self.__comp_cost, self.__comm_cost

    @property
    def next_unit_costs(self):
        return self.__next_costs.copy()

    @property
    def id(self):
        return self.__id

    @property
    def arr_buffer_sizes(self):
        return self.__arr_buffers.buffer_sizes

    @property
    def pred_win_size(self):
        return self.__arr_buffers.win_size

    @property
    def proc_buffer_size(self):
        return self.__proc_buffer.length

    @property
    def assoc_elements(self):
        return self.__assoc_elements.copy()

    @property
    def next_srv_rate(self):
        return self.__srv_cap
Ejemplo n.º 48
0
def sac(args):
    #set seed if non default is entered
    if args.seed != -1:
        torch.manual_seed(args.seed)
        np.random.seed(args.seed)

    env, test_env = TorchEnv(args.env_name, args.max_ep_len), TorchEnv(
        args.env_name, args.max_ep_len)
    state_dim = env.observation_space.shape[0]
    action_dim = env.action_space.shape[0]
    action_limit = env.action_space.high[0]
    # Create actor-critic module and target networks
    ac = ActorCritic(state_dim,
                     action_dim,
                     action_limit,
                     args.hidden_size,
                     args.gamma,
                     args.alpha,
                     device=args.device)
    ac_targ = deepcopy(ac)
    # Freeze target networks with respect to optimizers (only update via polyak averaging)
    for p in ac_targ.parameters():
        p.requires_grad = False
    # List of parameters for both Q-networks (save this for convenience)
    q_params = itertools.chain(ac.q1.parameters(), ac.q2.parameters())
    # Experience buffer
    buffer = Buffer(state_dim,
                    action_dim,
                    buffer_size=args.buffer_size,
                    device=args.device)
    # Set up optimizers for policy and q-function
    pi_optimizer = Adam(ac.pi.parameters(), lr=args.lr)
    q_optimizer = Adam(q_params, lr=args.lr)

    def update(data):
        # First run one gradient descent step for Q1 and Q2
        q_optimizer.zero_grad()
        loss_q = ac.compute_loss_q(data, ac_targ)
        loss_q.backward()
        q_optimizer.step()

        # Freeze Q-networks so you don't waste computational effort computing gradients for them during the policy learning step.
        for p in q_params:
            p.requires_grad = False

        # Next run one gradient descent step for pi.
        pi_optimizer.zero_grad()
        loss_pi = ac.compute_loss_pi(data)
        loss_pi.backward()
        pi_optimizer.step()

        # Unfreeze Q-networks so you can optimize it at next DDPG step.
        for p in q_params:
            p.requires_grad = True

        # Finally, update target networks by polyak averaging.
        with torch.no_grad():
            for p, p_targ in zip(ac.parameters(), ac_targ.parameters()):
                # NB: We use an in-place operations "mul_", "add_" to update target params, as opposed to "mul" and "add", which would make new tensors.
                p_targ.data.mul_(args.polyak)
                p_targ.data.add_((1 - args.polyak) * p.data)

    def test_agent(deterministic=True):
        for j in range(args.num_test_episodes):
            o, d, ep_ret, ep_len = test_env.reset(), False, 0, 0
            while not (d or (ep_len == args.max_ep_len)):
                # Take deterministic actions at test time
                o, r, d = test_env.step(
                    ac.act(
                        torch.as_tensor(o,
                                        dtype=torch.float32).to(args.device),
                        deterministic))
                ep_ret += r
                ep_len += 1

    # Prepare for interaction with environment
    total_steps = args.steps_per_epoch * args.epochs
    start_time = time.time()
    o, ep_ret, ep_len = env.reset(), 0, 0

    # Main loop: collect experience in env and update/log each epoch
    for t in range(total_steps):
        # Until start_steps have elapsed, randomly sample actions from a uniform distribution for better exploration. Afterwards, use the learned policy.
        if t > args.start_steps:
            a = ac.act(torch.as_tensor(o, dtype=torch.float32).to(args.device))
        else:
            a = env.action_space.sample()
        # Step the env
        o2, r, d = env.step(a)
        if args.render_env:
            env.render()
        ep_ret += r
        ep_len += 1
        # Ignore the "done" signal if it comes from hitting the time horizon (that is, when it's an artificial terminal signal that isn't based on the agent's state)
        d = False if ep_len == args.max_ep_len else d
        # Store experience to replay buffer
        buffer.add(o, a, r, o2, d)
        o = o2
        # End of trajectory handling
        if d or (ep_len == args.max_ep_len):
            print("EPISODE REWARD: ", ep_ret)
            o, ep_ret, ep_len = env.reset(), 0, 0

        # Update handling
        if t >= args.update_after and t % args.update_every == 0:
            batch_generator = buffer.get_train_batches(args.batch_size)
            for j in range(args.update_every):
                #my_batch = my_buffer.get_train_batches(args.batch_size).__next__()
                try:
                    batch = batch_generator.__next__()
                except:
                    batch_generator = buffer.get_train_batches(args.batch_size)
                    batch = batch_generator.__next__()
                update(batch)

        # End of epoch handling
        if (t + 1) % args.steps_per_epoch == 0:
            epoch = (t + 1) // args.steps_per_epoch
            # Test the performance of the deterministic version of the agent.
            test_agent()
Ejemplo n.º 49
0
 def test_negative_offset(self):
     data = np.zeros(10)
     B = Buffer(data=data, resizeable=False)
     with self.assertRaises(ValueError):
         B.set_data(np.ones(1),offset=-1)
Ejemplo n.º 50
0
 def addLeaderBoardCoins(self, coins):
     if not self.lobbier:
         self.coins += coins
     self.sendBin(0x22, Buffer().writeInt32(coins))
Ejemplo n.º 51
0
 def __init__(self, bot):
     self.chan = None
     self.bot = bot
     self.run_thread = True
     self.send_thread = Thread(target=lambda: self.packet_queue_thread())
     self.send_buffer = Buffer()
Ejemplo n.º 52
0
def interagtion_test(num_senders):

    print("---- Number of senders:", num_senders)

    # setup the test variables
    origin_file = 'filedata.txt'
    genera_file = 'newfile.txt'
    splited_files = 'split_file'
    output_size = 100000
    wait_time = 1
    max_time = 5
    db_name = 'test.db'

    # make larger file and split file
    file_iterations(origin_file, output_size, genera_file)
    files = []  # create a array of splited file names to hold splited files
    for i in range(num_senders):
        files.append('%s%d.txt' % (splited_files, i))
    split_file(num_senders, genera_file,
               splited_files)  # split large file into # of files = num_sender

    # create buffers array, one buffer for each process
    # each buffer managed by the shared memory manager
    buffers = []
    for i in range(num_senders):
        buffers.append(Buffer())
        buffers[i].index.value = i

    #create a sync manager to share info between the receiver and the analytics
    sm = SyncManager()

    #setup the database
    create_table_db(db_name)

    #### invoke multiple senders

    sender_pool = []  # pool for collecting sender process
    sent_ct = Queue(
    )  # sent_ct - use shared memory Queue to store # of records processed

    start = time.time()  # start latency measurement

    for i in range(num_senders):
        p = Process(target=stream_sender,
                    args=(files[i], buffers[i], sent_ct))  # senders
        p.start()
        sender_pool.append(p)

    reader = Process(target=stream_receiver,
                     args=(buffers, sm, wait_time, max_time))  # receivers
    # analytics = Process(target=realtime_analysis, args=(db_name,sm))
    reader.start()
    # analytics.start()

    for p in sender_pool:
        p.join()
    reader.join()
    # analytics.join()

    t = time.time() - start  # end latency measurement

    # attempt to read the database for the intial entries (this will eventually be PART 2)
    log_results(read_all_records_db(db_name))

    # end & clean up
    for i in range(num_senders):
        os.remove(files[i])

    total_bytes = 0
    while not sent_ct.empty():
        total_bytes += sent_ct.get(
        )  # total memory size of data streamed into buffer
    total_Bytes = total_bytes / 1000

    return t, total_Bytes
Ejemplo n.º 53
0
from buffer import Buffer
import pandas as pd

"""
The reader Buffer will be used to hold a DataFrame that was just read in from a csv.
Each iteration of the loop will overwrite the value in reader.value -> forgetful worker pattern

Pandas concat is slow at scale, so we want to reduce the number of concats
Instead we append query results to a list, and then perform one pd.concat at the end
"""

# Not a real file list
DIRECTORY = 'C:/'
file_list = ['{0}example_file_{1}.csv'.format(DIRECTORY, index) for index in range(10)]

reader = Buffer()
query_return = Buffer()
query_return.value = []


# Look through all the files and find the results where Column == 12345
search_value = 12345

for file in file_list:
    reader.value = pd.read_csv(file)
    reader.value = reader.value.query('Column == @search_value')
    query_return.value.append(reader.value)

query_return.value = pd.concat(query_return.value)
Ejemplo n.º 54
0
    def handlePkt(self, code, b, pktData):
        if code == 0x10: # CREATE_PLAYER_OBJECT
            level, zone, pos = b.readInt8(), b.readInt8(), b.readShor2()
            self.level = level
            self.zone = zone
            self.posX = pos[0]
            self.posY = pos[1]

            self.dead = False
            self.client.stopDCTimer()
            self.match.broadBin(0x10, self.serializePlayerObject())

        elif code == 0x11: # KILL_PLAYER_OBJECT
            if self.dead or self.win:
                return

            self.dead = True
            self.client.startDCTimer(60)

            self.addDeath()
            self.match.broadBin(0x11, Buffer().writeInt16(self.id))
            self.addLeaderBoardCoins(-10)

        elif code == 0x12: # UPDATE_PLAYER_OBJECT
            if self.dead or self.lastUpdatePkt == pktData:
                return

            level, zone, pos, sprite, reverse = b.readInt8(), b.readInt8(), b.readVec2(), b.readInt8(), b.readBool()

            if self.level != level or self.zone != zone:
                self.match.onPlayerWarp(self, level, zone)

            if (self.level < level):
                self.flagTouched = False
            self.level = level
            self.zone = zone
            self.posX = pos[0]
            self.posY = pos[1]
            tile = self.match.getTile(level,zone,int(self.posX),int(self.posY))
            tileDef = (tile>>16)&0xff
            extraData = (tile>>24)&0xff
            if (tileDef == 160 and extraData == 1 and not self.flagTouched):
                self.addLeaderBoardCoins(20)
            if (tileDef == 160 and extraData == 2 and not self.flagTouched):
                self.addLife()
            if (tileDef == 160):
                self.flagTouched = True
            self.lastUpdatePkt = pktData

            if sprite > 5 and self.match.world == "lobby" and zone == 0:
                self.client.block(0x1)
                return
            
            self.match.broadPlayerUpdate(self, pktData)
            
        elif code == 0x13: # PLAYER_OBJECT_EVENT
            if self.dead or self.win:
                return

            type = b.readInt8()

            if self.match.world == "lobby":
                self.client.block(0x2)
                return
            
            self.match.broadBin(0x13, Buffer().writeInt16(self.id).write(pktData))

        elif code == 0x17:
            killer = b.readInt16()
            if self.id == killer:
                return
            
            killer = self.match.getPlayer(killer)
            if killer is None:
                return

            killer.addKill()
            killer.sendBin(0x17, Buffer().writeInt16(self.id).write(pktData))
            killer.addLeaderBoardCoins(30)

        elif code == 0x18: # PLAYER_RESULT_REQUEST
            if self.dead or self.win:
                return

            self.win = True
            self.client.startDCTimer(120)

            pos = self.match.getWinners()
            if pos == 1:
                self.addWin()
            try:
                # Maybe this should be asynchronous?
                if self.server.discordWebhook is not None and pos == 1 and not self.match.private:
                    name = self.name
                    # We already filter players that have a squad so...
                    if len(self.team) == 0 and not self.isDev and util.checkCurse(self.name):
                        name = "[ censored ]"
                    embed = DiscordEmbed(description='**%s** has achieved **#1** victory royale!%s' % (name, " (PVP Mode)" if self.gameMode == 1 else " (Hell mode)" if self.gameMode == 2 else ""), color=0xffff00)
                    self.server.discordWebhook.add_embed(embed)
                    self.server.discordWebhook.execute()
                    self.server.discordWebhook.remove_embed(0)
            except:
                pass

            # Make sure that everyone knows that the player is at the axe
            self.match.broadPlayerUpdate(self, self.lastUpdatePkt)

            if pos == 1:
                self.addLeaderBoardCoins(100)
            elif pos == 2:
                self.addLeaderBoardCoins(50)
            elif pos == 3:
                self.addLeaderBoardCoins(25)
            self.match.broadBin(0x18, Buffer().writeInt16(self.id).writeInt8(pos).writeInt8(0))
            
        elif code == 0x19:
            self.trustCount += 1
            if self.trustCount > 8:
                self.client.block(0x3)

        elif code == 0x20: # OBJECT_EVENT_TRIGGER
            if self.dead:
                return

            self.match.objectEventTrigger(self, b, pktData)
            
        elif code == 0x30: # TILE_EVENT_TRIGGER
            if self.dead:
                return

            self.match.tileEventTrigger(self, b, pktData)
Ejemplo n.º 55
0
class SACAgent:
    def __init__(self, env, gamma, tau, alpha, q_lr, policy_lr, a_lr,
                 buffer_maxlen):
        self.device = torch.device(
            "cuda" if torch.cuda.is_available() else "cpu")

        self.env = env
        self.action_range = [env.action_space.low, env.action_space.high]
        self.obs_dim = env.observation_space.shape[0]
        self.action_dim = env.action_space.shape[0]

        # hyperparameters
        self.gamma = gamma
        self.tau = tau

        # initialize networks
        self.q_net1 = SoftQNetwork(self.obs_dim,
                                   self.action_dim).to(self.device)
        self.q_net2 = SoftQNetwork(self.obs_dim,
                                   self.action_dim).to(self.device)
        self.target_q_net1 = SoftQNetwork(self.obs_dim,
                                          self.action_dim).to(self.device)
        self.target_q_net2 = SoftQNetwork(self.obs_dim,
                                          self.action_dim).to(self.device)
        self.policy_net = GaussianPolicy(self.obs_dim,
                                         self.action_dim).to(self.device)

        # copy params to target param
        for target_param, param in zip(self.target_q_net1.parameters(),
                                       self.q_net1.parameters()):
            target_param.data.copy_(param)

        for target_param, param in zip(self.target_q_net2.parameters(),
                                       self.q_net2.parameters()):
            target_param.data.copy_(param)

        # initialize optimizers
        self.q1_optimizer = optim.Adam(self.q_net1.parameters(), lr=q_lr)
        self.q2_optimizer = optim.Adam(self.q_net2.parameters(), lr=q_lr)
        self.policy_optimizer = optim.Adam(self.policy_net.parameters(),
                                           lr=policy_lr)

        # entropy temperature
        self.alpha = alpha
        self.target_entropy = -torch.prod(
            torch.Tensor(self.env.action_space.shape).to(self.device)).item()
        self.log_alpha = torch.zeros(1, requires_grad=True, device=self.device)
        self.alpha_optim = optim.Adam([self.log_alpha], lr=a_lr)

        self.replay_buffer = Buffer(buffer_maxlen)

    def get_action(self, state):
        state = torch.FloatTensor(state).unsqueeze(0).to(self.device)
        mean, log_std = self.policy_net.forward(state)
        std = log_std.exp()

        normal = Normal(mean, std)
        z = normal.sample()
        action = torch.tanh(z)
        action = action.cpu().detach().squeeze(0).numpy()

        return action

    def update(self, batch_size):
        states, actions, rewards, next_states, dones = self.replay_buffer.sample(
            batch_size)
        states = torch.FloatTensor(states).to(self.device)
        actions = torch.FloatTensor(actions).to(self.device)
        rewards = torch.FloatTensor(rewards).to(self.device)
        next_states = torch.FloatTensor(next_states).to(self.device)
        dones = torch.FloatTensor(dones).to(self.device)
        dones = dones.view(dones.size(0), -1)

        _, _, next_zs, next_log_pi = self.policy_net.sample(next_states)
        next_actions = torch.tanh(next_zs)
        next_q1 = self.target_q_net1(next_states, next_actions)
        next_q2 = self.target_q_net2(next_states, next_actions)
        next_q_target = torch.min(next_q1, next_q2) - self.alpha * next_log_pi
        expected_q = rewards + (1 - dones) * self.gamma * next_q_target

        # q loss
        curr_q1 = self.q_net1.forward(states, actions)
        curr_q2 = self.q_net2.forward(states, actions)
        q1_loss = F.mse_loss(curr_q1, expected_q.detach())
        q2_loss = F.mse_loss(curr_q2, expected_q.detach())

        # update q networks
        self.q1_optimizer.zero_grad()
        q1_loss.backward()
        self.q1_optimizer.step()

        self.q2_optimizer.zero_grad()
        q2_loss.backward()
        self.q2_optimizer.step()

        # delayed update for policy network and target q networks
        _, _, new_zs, log_pi = self.policy_net.sample(states)
        new_actions = torch.tanh(new_zs)
        min_q = torch.min(self.q_net1.forward(states, new_actions),
                          self.q_net2.forward(states, new_actions))
        policy_loss = (self.alpha * log_pi - min_q).mean()
        self.policy_optimizer.zero_grad()
        policy_loss.backward()
        self.policy_optimizer.step()

        # target networks
        for target_param, param in zip(self.target_q_net1.parameters(),
                                       self.q_net1.parameters()):
            target_param.data.copy_(self.tau * param +
                                    (1 - self.tau) * target_param)

        for target_param, param in zip(self.target_q_net2.parameters(),
                                       self.q_net2.parameters()):
            target_param.data.copy_(self.tau * param +
                                    (1 - self.tau) * target_param)

        # update temperature
        alpha_loss = (self.log_alpha *
                      (-log_pi - self.target_entropy).detach()).mean()

        self.alpha_optim.zero_grad()
        alpha_loss.backward()
        self.alpha_optim.step()
        self.alpha = self.log_alpha.exp()
Ejemplo n.º 56
0
 def addCoin(self):
     if not self.lobbier:
         self.coins += 1
     self.sendBin(0x21, Buffer().writeInt8(0))
Ejemplo n.º 57
0
def buffer_input(prompt='scm> '):
    """Return a Buffer instance containing interactive input."""
    return Buffer(tokenize_lines(InputReader(prompt)))
Ejemplo n.º 58
0
class FIX:

    class Message:

        def __init__(self, sub: SubID = None, msg_type: str = None, parent = None):
            self.fields = []
            if parent:
                self.origin = True
                self.fields.append((Field.BeginString, "FIX.4.4"))
                self.fields.append((Field.BodyLength, 0))
                self.fields.append((Field.MsgType, msg_type))
                self.fields.append((Field.SenderCompID, parent.broker + "." + parent.login))
                self.fields.append((Field.SenderSubID, sub))
                self.fields.append((Field.TargetCompID, "CSERVER"))
                self.fields.append((Field.TargetSubID, sub))
                if sub == SubID.QUOTE:
                    self.fields.append((Field.MsgSeqNum, parent.qseq))
                    parent.qseq += 1
                elif sub == SubID.TRADE:
                    self.fields.append((Field.MsgSeqNum, parent.tseq))
                    parent.tseq += 1
                self.fields.append((Field.SendingTime, get_date()))
            else:
                self.origin = False

        def __getitem__(self, item):
            for k, v in self.fields:
                if k == item:
                    return v
            return None

        def __setitem__(self, key, value):
            self.fields.append((key, value))

        def get_repeating_groups(self, count_key, repeating_start, repeating_end = None):
            count = None
            result = []
            item = {}
            for k, v in self.fields[8:]:
                if count == 0:
                    return result
                if count is None:
                    if k == count_key:
                        count = int(v)
                    continue
                if (k == repeating_start and len(item) > 0) or k == repeating_end:
                    result.append(item)
                    item = {}
                    count -= 1
                item[k] = v
            result.append(item)
            return result

        def __bytes__(self):
            data = bytearray()
            for k, v in self.fields:
                data.extend(b"%b=%b\x01" % (str(k.value).encode(), str(v).encode()))
            if self.origin:
                data[12:13] = b"%d" % (len(data) - 14)
                cksm = sum(data) % 256
                data.extend(b"10=%03d\x01" % cksm)
            return bytes(data)

        def __str__(self):
            data = ""
            for k, v in self.fields:
                data += "%s=%s|" % (str(k.value), str(v))
            if self.origin:
                data = data[:12] + "%d" % (len(data) - 14) + data[13:]
                cksm = sum(data.replace("|", "\x01").encode()) % 256
                data += "10=%03d|" % cksm
            return data

        def __repr__(self):
            return pformat([(k.name, v) for k, v in self.fields])

    def __init__(self, server: str, broker: str, login: str, password: str, currency: str, position_list_callback, order_list_callback):
        self.qstream = Buffer()
        self.qs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.qs.connect((server, 5201))
        self.tstream = Buffer()
        self.ts = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.ts.connect((server, 5202))
        self.broker = broker
        self.login = login
        self.password = password
        self.currency = currency
        self.qseq = 1
        self.tseq = 1
        self.qtest_seq = 1
        self.ttest_seq = 1
        self.market_seq = 1
        self.subscribed_symbol = [-1, -1, -1]
        self.qworker_thread = threading.Thread(target=self.qworker)
        self.qworker_thread.start()
        self.tworker_thread = threading.Thread(target=self.tworker)
        self.tworker_thread.start()
        self.ping_qworker_thread = None
        self.ping_tworker_thread = None
        self.sec_list_callback = None
        self.market_callback = None
        self.sec_id_table = {}
        self.sec_name_table = {}
        self.position_list_callback = position_list_callback
        self.order_list_callback = order_list_callback
        self.market_data = {}
        self.position_list = {}
        self.spot_request_list = set()
        self.spot_price_list = {}
        self.base_convert_request_list = set()
        self.base_convert_list = {}
        self.order_list = {}
        self.logon()
        self.sec_list()

    def qworker(self):
        while True:
            data = self.qs.recv(65535)
            if len(data) == 0:
                logging.error("Disconnected")
                break
            self.qstream.write(data)
            self.parse_quote_message()

    def tworker(self):
        while True:
            data = self.ts.recv(65535)
            if len(data) == 0:
                logging.error("Disconnected")
                break
            self.tstream.write(data)
            self.parse_trade_message()

    def parse_quote_message(self):
        while len(self.qstream) > 0:
            match = re.search(rb"10=\d{3}\x01", self.qstream.peek(self.qstream.count()))
            if match:
                msg = FIX.Message()
                data = self.qstream.read(match.span()[1]).split(b"\x01")[:-1]
                for part in data:
                    tag, value = part.split(b"=", 1)
                    msg[Field(int(tag.decode()))] = value.decode()
                logging.debug("\033[32mRECV <<< %s\033[0m" % msg)
                self.process_message(msg)
            else:
                break

    def parse_trade_message(self):
        while len(self.tstream) > 0:
            match = re.search(rb"10=\d{3}\x01", self.tstream.peek(self.tstream.count()))
            if match:
                msg = FIX.Message()
                data = self.tstream.read(match.span()[1]).split(b"\x01")[:-1]
                for part in data:
                    tag, value = part.split(b"=", 1)
                    msg[Field(int(tag.decode()))] = value.decode()
                logging.debug("\033[92mRECV <<< %s\033[0m" % msg)
                self.process_message(msg)
            else:
                break

    def ping_qworker(self, interval: int):
        while True:
            self.qheartbeat()
            time.sleep(interval)

    def ping_tworker(self, interval: int):
        while True:
            self.theartbeat()
            time.sleep(interval)

    def process_ping(self, msg):
        pass

    def process_test(self, msg):
        if msg[Field.SenderSubID] == "QUOTE":
            self.qheartbeat(msg[Field.TestReqID])
        elif msg[Field.SenderSubID] == "TRADE":
            self.theartbeat(msg[Field.TestReqID])

    def process_logout(self, msg):
        logging.error("Logged out: %s" % msg[Field.Text])

    def process_exec_report(self, msg):
        if msg[Field.ExecType] == "F":
            self.position_list = {}
            self.position_request()
            self.order_list = {}
            self.order_request()
        elif msg[Field.ExecType] in ["0", "4", "5", "C"]:
            self.order_list = {}
            self.order_request()
        elif msg[Field.ExecType] == "I":
            name = self.sec_id_table[int(msg[Field.Symbol])]["name"]
            self.order_list[msg[Field.OrderID]] = {
                "name": name,
                "side": Side(int(msg[Field.Side])),
                "amount": float(msg[Field.LeavesQty]),
                "type": int(msg[Field.OrdType]),
                "pos_id": msg[Field.PosMaintRptID],
                "digits": self.sec_id_table[int(msg[Field.Symbol])]["digits"],
                "clid": msg[Field.ClOrdId],
            }
            if int(msg[Field.OrdType]) > 1:
                if price := msg[Field.Price]:
                    self.order_list[msg[Field.OrderID]]["price"] = float(price)
                else:
                    self.order_list[msg[Field.OrderID]]["price"] = float(msg[Field.StopPx])
            if name not in self.spot_request_list:
                self.spot_market_request(name)
            self.order_list_callback(self.order_list, self.spot_price_list)
Ejemplo n.º 59
0
class Trainer(object):
    def __init__(self, config, rng):
        self.config = config
        self.rng = rng

        self.task = config.task
        self.model_dir = config.model_dir
        self.gpu_memory_fraction = config.gpu_memory_fraction

        self.log_step = config.log_step
        self.max_step = config.max_step

        self.K_d = config.K_d
        self.K_g = config.K_g
        self.initial_K_d = config.initial_K_d
        self.initial_K_g = config.initial_K_g
        self.checkpoint_secs = config.checkpoint_secs

        DataLoader = {
            'gaze': gaze_data.DataLoader,
            'hand': hand_data.DataLoader,
        }[config.data_set]
        self.data_loader = DataLoader(config, rng=self.rng)

        self.model = Model(config, self.data_loader)
        self.history_buffer = Buffer(config, self.rng)

        self.summary_ops = {
            'test_synthetic_images': {
                'summary':
                tf.summary.image("test_synthetic_images",
                                 self.model.resized_x,
                                 max_outputs=config.max_image_summary),
                'output':
                self.model.resized_x,
            },
            'test_refined_images': {
                'summary':
                tf.summary.image("test_refined_images",
                                 self.model.denormalized_R_x,
                                 max_outputs=config.max_image_summary),
                'output':
                self.model.denormalized_R_x,
            }
        }

        self.saver = tf.train.Saver()
        self.summary_writer = tf.summary.FileWriter(self.model_dir)

        sv = tf.train.Supervisor(logdir=self.model_dir,
                                 is_chief=True,
                                 saver=self.saver,
                                 summary_op=None,
                                 summary_writer=self.summary_writer,
                                 save_summaries_secs=300,
                                 save_model_secs=self.checkpoint_secs,
                                 global_step=self.model.discrim_step)

        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=self.gpu_memory_fraction,
            allow_growth=True)  # seems to be not working
        sess_config = tf.ConfigProto(allow_soft_placement=True,
                                     gpu_options=gpu_options)

        self.sess = sv.prepare_or_wait_for_session(config=sess_config)

    def train(self):
        print("[*] Training starts...")
        self._summary_writer = None

        sample_num = functools.reduce(lambda x, y: x * y,
                                      self.config.sample_image_grid)
        idxs = self.rng.choice(len(self.data_loader.synthetic_data_paths),
                               sample_num)
        test_samples = np.expand_dims(np.stack(
            [imread(path) for path in \
                self.data_loader.synthetic_data_paths[idxs]]
        ), -1)

        def train_refiner(push_buffer=False):
            feed_dict = {
                self.model.synthetic_batch_size: self.data_loader.batch_size,
            }
            res = self.model.train_refiner(self.sess,
                                           feed_dict,
                                           self._summary_writer,
                                           with_output=True)
            self._summary_writer = self._get_summary_writer(res)

            if push_buffer:
                self.history_buffer.push(res['output'])

            if res['step'] % self.log_step == 0:
                feed_dict = {
                    self.model.x: test_samples,
                }
                self._inject_summary('test_refined_images', feed_dict,
                                     res['step'])

                if res['step'] / float(self.log_step) == 1.:
                    self._inject_summary('test_synthetic_images', feed_dict,
                                         res['step'])

        def train_discrim():
            feed_dict = {
                self.model.synthetic_batch_size:
                self.data_loader.batch_size / 2,
                self.model.R_x_history: self.history_buffer.sample(),
                self.model.y: self.data_loader.next(),
            }
            res = self.model.train_discrim(self.sess,
                                           feed_dict,
                                           self._summary_writer,
                                           with_history=True,
                                           with_output=False)
            self._summary_writer = self._get_summary_writer(res)

        for k in trange(self.initial_K_g, desc="Train refiner"):
            train_refiner(push_buffer=k > self.initial_K_g * 0.9)

        for k in trange(self.initial_K_d, desc="Train discrim"):
            train_discrim()

        for step in trange(self.max_step, desc="Train both"):
            for k in xrange(self.K_g):
                train_refiner(push_buffer=True)

            for k in xrange(self.K_d):
                train_discrim()

    def test(self):
        batch_size = self.data_loader.batch_size
        num_epoch = len(self.data_loader.synthetic_data_paths) / batch_size

        for idx in trange(num_epoch, desc="Refine all synthetic images"):
            feed_dict = {
                self.model.synthetic_batch_size: batch_size,
            }
            res = self.model.test_refiner(self.sess,
                                          feed_dict,
                                          None,
                                          with_output=True)

            for image, filename in zip(res['output'], res['filename']):
                basename = os.path.basename(filename).replace(
                    "_cropped", "_refined")
                path = os.path.join(self.config.output_model_dir, basename)
                imwrite(path, image[:, :, 0])

    def _inject_summary(self, tag, feed_dict, step):
        summaries = self.sess.run(self.summary_ops[tag], feed_dict)
        self.summary_writer.add_summary(summaries['summary'], step)

        path = os.path.join(self.config.sample_model_dir,
                            "{}.png".format(step))
        imwrite(
            path,
            img_tile(summaries['output'],
                     tile_shape=self.config.sample_image_grid)[:, :, 0])

    def _get_summary_writer(self, result):
        if result['step'] % self.log_step == 0:
            return self.summary_writer
        else:
            return None
Ejemplo n.º 60
0
    ), (394, 450), (394, 200))
    game.show_stats(w, h, win)
    if ball.y < ball.radius:
        pygame.draw.polygon(
            win,
            (0, 0, 0),
            # Dodaje strzałkę pokazującą gdzie jest piłka w przypadku wylecenia za ekran
            ((int(ball.x), 6), (int(ball.x) - 6, 12), (int(ball.x) - 2, 8),
             (int(ball.x) - 2, 26), (int(ball.x) + 2, 26),
             (int(ball.x) + 2, 8), (int(ball.x) + 6, 12)))
    pygame.display.update()


game = Game()
opponent_connected = False
buffer_player1 = Buffer()
buffer_player1_recv = Buffer()
buffer_player2 = Buffer()
buffer_ball = Buffer()


def thread_updating_data(n, c):
    '''
    Należy cylkiczne wysyłać bufor naszego gracza
    n.send(plik) -> wysyła plik do serwera w odpowiedzi otrzymujemy tablicę
        [buffer_player1 (naszego gracza), buffer_player2, buffer_ball, game, opponent_connected]
    '''


def online_game(server_address):
    n = Network(server_address)