Пример #1
0
    def setMachine(self, mac, ip=None, localApps=False):
        mac = mac.strip()
        if len(mac.split(':')) <> 6:
            return False

        try:    ip = ip.strip()
        except: pass
        
        c = Cfg()
        c.read(self.__configLTSPFile)
        if not c.has_section(mac):
            c.add_section(mac)

        if ip is not None:
            from rede import Rede
            s = self.getServer()
            masc = s[2]
            rede = s[4]
            del s
            
            try:
                i = Rede(ip, masc)
            except:
                return False

            if i.getRedeAsStr() <> rede:
                return False
            
            c.set(mac, 'ip', ip)
        c.set(mac, 'localapps', str(localApps))
        c.write( open(self.__configLTSPFile,'w') )
        del c
        
        return True
Пример #2
0
    def setRange(self, start, end):
        s = self.getServer()
        masc = s[2]
        rede = s[4]
        
        from rede import Rede
        try:
            i = Rede(start, masc)
            f = Rede(end  , masc)
        except:
            return False

        if i.getRedeAsStr() <> rede:
            return False
        if f.getRedeAsStr() <> rede:
            return False
        
        section = 'range'
        c = Cfg()
        c.read(self.__configLTSPFile)
        if not c.has_section( section ):
            c.add_section( section )
        
        c.set(section, 'start', start)
        c.set(section, 'end'  , end)
        c.write( open(self.__configLTSPFile,'w')  )
        del c

        return True
Пример #3
0
    def setServer(self, ip, masc, iface):
        from rede import Rede
        r = Rede(ip, masc)
        M = self.getMachines()
        
        c = Cfg()
        if not c.has_section('server'):
            c.add_section('server')
        c.set('server', 'ip', r.getIPAsStr())
        c.set('server', 'masc', r.getMascAsStr())
        c.set('server', 'rede', r.getRedeAsStr())
        c.set('server', 'broadcast', r.getBroadcastAsStr())
        c.set('server', 'iface', iface)
        c.write( open(self.__configLTSPFile,'w')  )
        
        for i in M:
            self.setMachine(i[0], i[1], i[2])

        del c
Пример #4
0
 def _create_neural_network(self, qtd_camadas=2):
     self.rede = Rede(1 + (self.qtd_inimigos * 3))
     [self.rede.add_camada(10) for i in range(qtd_camadas)]
     self.rede.add_camada(3)
Пример #5
0
class Jogo:
    def __init__(self, qtd_inimigos=3, width=WIDTH, height=HEIGHT):
        pygame.init()
        self.display = Display(width=WIDTH, height=HEIGHT)
        self.clock = pygame.time.Clock()
        self.jogador = BlocoPrincipal(self.display, width=width, height=height)
        self.qtd_inimigos = qtd_inimigos
        self.enemys = []
        self.score = 0

    def _check_event(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return True
            elif event.type == pygame.KEYDOWN:
                self.jogador.move(event.key)
        return False

    def init(self, color_fill=GREY):
        fechou = False
        self._create_neural_network()
        while not fechou:
            self.display.fill(color_fill)
            self._create_enemy()
            self._enemys_fall()
            self._check_fall_enemy()
            fechou = self._check_colision() or fechou
            fechou = self._check_event() or fechou
            self.jogador.set_bloco()
            self.display.write_score(self.score)
            self._get_move_rna()
            self.display.update()
            self.clock.tick(60)

        pygame.quit()

    def _create_enemy(self, qtd=None, width=WIDTH, height=HEIGHT):

        qtd = qtd or self.qtd_inimigos

        while len(self.enemys) < qtd:
            x = randint(0, WIDTH - WIDTH_BLOCO)
            self.enemys.append(
                BlocoInimigo(x=x,
                             speed=random() * 10,
                             display=self.display,
                             width=width,
                             height=height))

    def _enemys_fall(self):
        [self.enemys[i].fall() for i in range(len(self.enemys))]

    def _check_fall_enemy(self):
        for idx, enemy in enumerate(self.enemys):
            if enemy.y >= (HEIGHT + HEIGHT_BLOCO):
                del self.enemys[idx]
                self.score += 1
                self._create_enemy()

    def _check_colision(self):
        for enemy in self.enemys:
            if enemy.y >= HEIGHT - HEIGHT_BLOCO:
                x = enemy.x
                x_high = x + (HEIGHT_BLOCO)
                x_low = x - (HEIGHT_BLOCO)
                if x_low <= self.jogador.x <= x_high:
                    return True
        return False

    def _create_neural_network(self, qtd_camadas=2):
        self.rede = Rede(1 + (self.qtd_inimigos * 3))
        [self.rede.add_camada(10) for i in range(qtd_camadas)]
        self.rede.add_camada(3)

    def _get_move_rna(self):
        inputs = self._get_inputs()
        print(inputs)
        predicts = self.rede.predict(inputs)
        max_predict = max(predicts)
        if max_predict == predicts[0]:
            self.jogador._move_left()
        if max_predict == predicts[1]:
            self.jogador._move_right()
        if max_predict == predicts[2]:
            pass

    def _get_inputs(self):
        inputs = []
        inputs += [min_max_scaler(0, WIDTH, enemy.x) for enemy in self.enemys]
        inputs += [min_max_scaler(0, HEIGHT, enemy.y) for enemy in self.enemys]
        inputs += [min_max_scaler(0, 10, enemy.speed) for enemy in self.enemys]
        inputs += [min_max_scaler(0, WIDTH, self.jogador.x)]
        return inputs
Пример #6
0
    def cria_redes(self):
        qtd_inputs = 1 + (self.qtd_inimigos * 3)
        rede = Rede(base)
        rede.add_camada(5)
        rede.add_camada(3)

        base = rede.sum_shapes

        lim_inf = [-2 for i in range(base)]
        lim_sup = [2 for i in range(base)]
        base = [10 for i in range(base)]

        p1 = Populacao(10, base, lim_inf, lim_sup, prob_mutacao=1)
        p1.gera_populacao_inicial()

        np.array(rede.camadas[0]).reshape(rede.shapes[0])

        aux = 0
        ind = p1[0].real
        ind2 = p1[1].real
        camadas = []
        camadas2 = []

        for idx, prod in enumerate(rede.prod_shapes):
            camadas.append(
                np.array(ind[aux:aux + prod]).reshape(rede.shapes[idx]))
            camadas2.append(
                np.array(ind2[aux:aux + prod]).reshape(rede.shapes[idx]))
            aux += prod

        np.append(camadas[0][:, 1].reshape(-1, 1),
                  camadas2[0][:, 2].reshape(-1, 1),
                  axis=1)

        novas_camadas = []
        for i in range(len(camadas)):
            camada1 = camadas[i]
            camada2 = camadas2[i]
            nova_camada = []
            for j in range(camada1.shape[1]):
                neuronio1 = np.array(camada1[:, j]).reshape(-1, 1)
                neuronio2 = np.array(camada2[:, j]).reshape(-1, 1)
                if random() < 0.5:
                    if len(nova_camada) == 0:
                        nova_camada = neuronio1
                    else:
                        nova_camada = np.append(nova_camada, neuronio1, axis=1)
                else:
                    if len(nova_camada) == 0:
                        nova_camada = neuronio2
                    else:
                        nova_camada = np.append(nova_camada, neuronio2, axis=1)
            novas_camadas.append(nova_camada)
            print('''
        Camada1:
        {}

        Camada2:
        {}

        Nova Camada:
        {}      
            '''.format(camada1, camada2, nova_camada))