def __init__(self, mode=Mode.DEMO, behavior=Behavior.ONLY_SAFE):
     self.done = False
     self.behavior = behavior
     self.network = Network(mode)
     self.mode = mode
     if self.mode == Mode.DEMO:
         self.observation_space = 8
     else:
         self.observation_space = 68
Beispiel #2
0
def edmondsKarp(net):
	flow = Network(net.n)
	residual = net.substract(flow)
	minc, path = findShortestExtendingPath(residual)
	while minc:
		flow.append(minc, path)
		residual = net.substract(flow)
		minc, path = findShortestExtendingPath(residual)
	return flow
Beispiel #3
0
def fordFulkerson(net):
    flow = Network(net.n)
    residual = net.substract(flow)
    minc, path = findExtendingPath(residual)
    while minc:
        flow.append(minc, path)
        residual = net.substract(flow)
        minc, path = findExtendingPath(residual)
    return flow
Beispiel #4
0
    def __init__(self, config, session=None):
        self.config = config
        self.save = config.bool("save", True)
        self.task = config.string("task", "train")
        self.dataset = config.string("dataset").lower()
        self.num_epochs = config.int("num_epochs", 1000)
        self.session = self._create_session(session)
        self.global_step = tf.Variable(0, name='global_step', trainable=False)
        self.iou_threshold = config.float("iou_threshold", 0.85)
        self.avg_clicks = {}

        # TODO
        need_train = True
        if need_train:
            self.train_data = load_eval_dataset(config, "train")
            freeze_batchnorm = config.bool("freeze_batchnorm", False)
            print("creating trainnet...", file=log.v1)
            self.train_network = Network(self.config,
                                         self.train_data,
                                         is_trainnet=True,
                                         freeze_batchnorm=freeze_batchnorm,
                                         name="trainnet")
        else:
            self.train_data = None
            self.train_network = None

        need_val = self.task != "train_no_val"
        if need_val:
            self.valid_data = load_eval_dataset(config, "valid")
            print("creating testnet...", file=log.v1)
            self.test_network = Network(config,
                                        self.valid_data,
                                        is_trainnet=False,
                                        freeze_batchnorm=True,
                                        name="testnet")
        else:
            self.valid_data = None
            self.test_network = None
        self.trainer = Trainer(config, self.train_network, self.test_network,
                               self.global_step, self.session)
        self.saver = Saver(config, self.session)

        tf.global_variables_initializer().run()
        tf.local_variables_initializer().run()

        self.start_epoch = self.saver.try_load_weights()
        self.session.graph.finalize()
        self.recursive_rounds = config.int("recursive_rounds", 20)
        self.avg_clicks = {}
        self.locality = config.bool("locality", False)
        self.locality_threshold = config.int("locality_threshold", 100)
        self.monotonicity = config.bool("monotonicity", False)

        if self.locality:
            print("Using locality postprocessing...", log.v1)
        if self.monotonicity:
            print("Using monotonicity postprocessing...", log.v1)
Beispiel #5
0
    def test_single_layer_network(self):

        network = Network()

        network.add_layer(Normalize())

        indata = np.array([2, 7, 3])
        outdata = network.compute(indata)
        self.assertTrue(np.allclose(outdata, [-0.4, 0.6, -0.2]))
Beispiel #6
0
def dinic(net):
	flow = Network(net.n)
	residual = net.substract(flow)
	extended, layerNet = getLayerNet(residual)
	while extended:
	 	blocking = findBlockingFlow(layerNet)
		flow.appendFlow(blocking)
		residual = net.substract(flow)
		extended, layerNet = getLayerNet(residual)
	return flow
Beispiel #7
0
def getLayerNet(net):
	layers = Network(net.n)
	current = [0]
	visited = current
	nextLayer = []
	while current and net.n - 1 not in visited:
		for v in current:
			for w in xrange(net.n):
				if net.c[v][w] and w not in visited:
					nextLayer.append(w)
					layers.setFlow(v, w, net.c[v][w])
		current = nextLayer
		visited = visited + nextLayer
		nextLayer = []
	return net.n -1 in visited, layers
Beispiel #8
0
    def __init__(self, config, session=None):
        self.config = config
        self.save = config.bool("save", True)
        self.task = config.string("task", "train")
        self.dataset = config.string("dataset").lower()
        self.num_epochs = config.int("num_epochs", 1000)
        self.session = self._create_session(session)
        self.global_step = tf.Variable(0, name='global_step', trainable=False)

        need_train = True  # TODO should be self.task != "eval", but right now testnet needs to reuse variables from train
        if need_train:
            self.train_data = load_dataset(config, "train", self.session,
                                           self.dataset)
            freeze_batchnorm = config.bool("freeze_batchnorm", False)
            print("creating trainnet...", file=log.v1)
            self.train_network = Network(self.config,
                                         self.train_data,
                                         is_trainnet=True,
                                         freeze_batchnorm=freeze_batchnorm,
                                         name="trainnet")
        else:
            self.train_data = None
            self.train_network = None

        need_val = self.task != "train_no_val"
        if need_val:
            self.valid_data = load_dataset(config, "val", self.session,
                                           self.dataset)
            print("creating testnet...", file=log.v1)
            reuse_variables = None if need_train else False
            self.test_network = Network(config,
                                        self.valid_data,
                                        is_trainnet=False,
                                        freeze_batchnorm=True,
                                        name="testnet",
                                        reuse_variables=reuse_variables)
        else:
            self.valid_data = None
            self.test_network = None
        self.trainer = Trainer(config, self.train_network, self.test_network,
                               self.global_step, self.session)
        self.saver = Saver(config, self.session)

        tf.global_variables_initializer().run()
        tf.local_variables_initializer().run()

        self.start_epoch = self.saver.try_load_weights()
        self.session.graph.finalize()
Beispiel #9
0
class Environment:
    network = Network()
    action_space = 2
    observation_space = 64
    info = ""

    def __init__(self, behavior=Behavior.ONLY_SAFE):
        self.done = False
        self.behavior = behavior

    def reset(self):
        self.done = False

    def step(self, action):
        if (action != 0) & (action != 1):
            print("Wrong action")
            return -1, -1, False, ""

        note = self.network.step()

        if (action == 1) & (note.kind == Kind.DANGER):
            reward = 1
        elif (action == 1) & (note.kind == Kind.SAFE):
            reward = -1
        else:
            reward = 0

        observation = note.message
        done = self.done
        info = note
        return observation, reward, done, info
Beispiel #10
0
    def test_multi_layer_network(self):

        network = Network()
        network.add_layer(Convolve([[0.1, 0], [0, 0.1]], keepdims=False))
        network.add_layer(Relu())
        network.add_layer(Flatten())
        network.add_layer(Softmax())

        indata = np.array([[5, 2, 7, 10], [1, 8, 3, 4]])

        outdata = network.compute(indata)

        self.assertTrue(np.allclose(outdata, [0.440905, 0.19811, 0.36098]))
Beispiel #11
0
    def __init__(self):

        # Call Screen constructor
        Screen.__init__(self)

        # Make connection to the server
        self.n = Network()
        self.n.connect()

        # Retrieve list of games from server
        self.buttonListSubset = []
        self.buttonList = []
        self.buttonListSlice = [0, 4]
        self.updateGameList()

        # Page navigator to go to next or previous page of active games
        self.pageNavigator = PageNavigator()
Beispiel #12
0
def main():
    parser = argparse.ArgumentParser(description="arguments of Neuron Network Optimizer")
    parser.add_argument('-n', '--network', default="data/ResNet-50-deploy.prototxt")
    parser.add_argument('-m', '--model', default='data/ResNet-50-model.caffemodel')
    args = parser.parse_args()
    network_file = args.network
    json_file = ProtoReader.read_proto(network_file)

    model_file = args.model
    model = parse_caffemodel(model_file)

    network = Network()
    network.construct_from_json(json_file)
    network.init_caffe_model(model)

    in_data = network.input
    in_data.set_content(np.zeros(in_data.get_shape()))
    for layer in network.layers:
        print 'start execute layer %s' % layer.name
        layer.execute()
        layer.print_info()
        print ""
    idx = len(network.layers) - 1
    print "output:"
    print network.layers[idx].top[0].get_content()
class Environment:
    action_space = 2            # Action = 0 - Detected malicious activity
    info = ""                   # Action = 1 - Normal activity

    def __init__(self, mode=Mode.DEMO, behavior=Behavior.ONLY_SAFE):
        self.done = False
        self.behavior = behavior
        self.network = Network(mode)
        self.mode = mode
        if self.mode == Mode.DEMO:
            self.observation_space = 8
        else:
            self.observation_space = 68

    def reset(self):
        self.network.reset()
        self.done = False
        return self.network.step().message

    def step(self, action):
        if (action != 0) & (action != 1):
            print("Wrong action")
            return -1, -1, False, ""
        note = self.network.step(self.behavior)
        if (action == 1) & (note.kind == Kind.DANGER):
            reward = 1
        elif (action == 1) & (note.kind == Kind.SAFE):
            reward = -1
        elif (action == 0) & (note.kind == Kind.SAFE):
            reward = 1
        else:
            reward = -1
        observation = note.message
        done = self.done
        if self.mode == Mode.HYBRID:
            info = note.protocol
        else:
            info = ""
        return observation, reward, done, info
Beispiel #14
0
def findBlockingFlow(net):
	blocking = Network(net.n)	# start with empty flow
	v = 0						# start from source
	path = [0]
	stepable = any([net.c[v][w] for w in xrange(net.n)])

	while v != 0 or stepable:
		if stepable:
			# advance
			w = 0
			while not net.c[v][w]:
				w += 1
			path.append(w)
			v = w

			if w == net.n - 1:
				# augment
				path_edges = zip(path[:-1], path[1:])
				minc = sys.maxint
				for v, w in path_edges:
					minc = min(minc, net.c[v][w])
				
				for v, w in path_edges:
					blocking.c[v][w] += minc
					blocking.c[w][v] = -blocking.c[v][w]
					net.c[v][w] -= minc

				v = 0 # look for the new path
				path = [0]
		else:
			# retreat
			w = path.pop()
			v = path[-1]
			net.c[v][w] = 0
		
		stepable = any([net.c[v][w] for w in xrange(net.n)])

	return blocking
Beispiel #15
0
class GameScreen(Screen):
    def __init__(
        self,
        username,
        isHost,
        gameName=None,
        gameKey=None,
        maxPlayers=None,
        gameMode=None,
        winningScore=None,
    ):
        Screen.__init__(self)

        # Set up connection to server
        self.n = Network()
        self.n.connect()

        # Tell the server to create/join the game and return player id
        if isHost:
            self.player = self.n.getPlayer(
                f"host/{gameName}/{maxPlayers}/{gameMode}/{winningScore}")
        else:
            self.player = self.n.getPlayer(f"join/{gameKey}")

        # Get the game from the server
        self.game = self.n.send("get")

        # Check if the game could be received from the server
        connectionAttempts = 0
        MAX_ATTMEPTS = 10000
        while self.game is None and connectionAttempts < MAX_ATTMEPTS:
            self.game = self.n.send("get")
            connectionAttempts += 1

        # Only initialize game screen objects if server connectino was succesful
        if self.game is not None:

            # Set player username
            self.game.players[self.player].username = username
            self.n.send("username: "******"S"

            self.scoreButton = Button(
                SCORE_BUTTON_WIDTH,
                SCORE_BUTTON_HEIGHT,
                SCORE_BUTTON_X,
                SCORE_BUTTON_Y,
                SCORE_BUTTON_COLOR,
                SCORE_BUTTON_TEXT_COLOR,
                SCORE_BUTTON_TEXT,
            )

            # Ten and Under Button
            TEN_AND_UNDER_BUTTON_WIDTH = 100
            TEN_AND_UNDER_BUTTON_HEIGHT = 100
            TEN_AND_UNDER_BUTTON_PADDING = 10
            TEN_AND_UNDER_BUTTON_X = (self.screen.get_width() /
                                      2) - (TEN_AND_UNDER_BUTTON_WIDTH / 2)
            TEN_AND_UNDER_BUTTON_Y = (
                (self.screen.get_height() / 2) -
                (TEN_AND_UNDER_BUTTON_HEIGHT / 2)) - (
                    TEN_AND_UNDER_BUTTON_HEIGHT + TEN_AND_UNDER_BUTTON_PADDING)
            TEN_AND_UNDER_BUTTON_COLOR = (255, 0, 0)
            TEN_AND_UNDER_BUTTON_TEXT_COLOR = (0, 0, 0)
            TEN_AND_UNDER_BUTTON_TEXT = "TEN\nAND\nUNDER"

            self.tenAndUnderButton = Button(
                TEN_AND_UNDER_BUTTON_WIDTH,
                TEN_AND_UNDER_BUTTON_HEIGHT,
                TEN_AND_UNDER_BUTTON_X,
                TEN_AND_UNDER_BUTTON_Y,
                TEN_AND_UNDER_BUTTON_COLOR,
                TEN_AND_UNDER_BUTTON_TEXT_COLOR,
                TEN_AND_UNDER_BUTTON_TEXT,
            )

            # List of all the buttons
            self.buttonList.append(self.scoreButton)
            self.buttonList.append(self.tenAndUnderButton)

            # Score screen
            self.scoreScreen = ScoreScreen()

            # Win Screen
            self.winScreen = WinScreen()

            self.showGameScreen = False
            self.showScoreScreen = False
            self.showWinScreen = False
            self.gameReady = False

    def run(self):

        # Check if the connection to the server was successful
        if self.game is None:
            return GameState.SERVER_ERROR

        # Game loop
        while True:
            self.clock.tick(60)

            # Attempt to get the game from the server
            try:

                self.game = self.n.send("get")

                # If a player left the game (numPlayers decreased),
                # disconnect from server and return to the title screen
                if self.game.numPlayers != len(self.game.players):
                    self.n.disconnect()
                    return GameState.DISCONNECT

                # Check to see if game is ready to start
                if (self.game.numPlayers >= self.game.maxPlayers
                        and self.showScoreScreen == False
                        and self.showWinScreen == False):
                    self.gameReady = True
                    self.showGameScreen = True

                # Update main pile
                self.main_pile = get_main_pile(self.game.mainPile)

                # Set trump image
                if self.game.trump is not None and self.trump_image != self.game.trump:
                    self.trump_image = Trump(self.game.trump)
                else:
                    self.trump_image = None

                # Update hand
                if self.game.players[self.player].ready:
                    self.test_hand = get_hand(
                        self.game.players[self.player].playerHand)
                    self.n.send("not ready")

            except:
                print("Couldn't get game")

            for event in pygame.event.get():

                # Check for quit event
                if event.type == QUIT:
                    return GameState.QUIT

                # Check for click event
                if event.type == pygame.MOUSEBUTTONUP and self.gameReady:
                    if event.button == 1:  # the right mouse button
                        for card in self.test_hand:

                            # Only play card if it is this player's turn
                            if (card.rect.collidepoint(event.pos) and
                                    self.game.players[self.player].playerTurn
                                    and
                                    self.game.players[self.player].playerBid
                                    is not None
                                    and self.game.didPlayersBid() == True
                                    and card.isPlayable(
                                        self.game,
                                        self.test_hand.hasCurrentSuit(
                                            self.game.currentSuit),
                                    )):

                                # Set small delay to ensure that the card played does
                                # not get counted as part of the previous trick
                                # TODO: This is hacky, look into another way to fix this
                                pygame.time.delay(200)

                                # Remove the card from the player's hand to be played in the main pile
                                self.test_hand.remove(card)

                                # Set not ready
                                if len(self.test_hand) == 0:
                                    self.n.send("not ready")

                                # Send card to server
                                self.n.send("card: " + str(card.value) + " " +
                                            card.suit)

                        # Check if a bid button was clicked
                        if (self.game.players[self.player].playerBidTurn
                                and self.game.biddingStage):
                            for button in self.bid_screen.buttonList:
                                if button.isClicked(event.pos):
                                    self.n.send("bid: " + button.text)

                        # Check if score button was clicked
                        if (self.scoreButton.isClicked(event.pos)
                                and self.showGameScreen == True
                                and self.showWinScreen == False):
                            self.showGameScreen = False
                            self.showScoreScreen = True
                        elif (self.scoreButton.isClicked(event.pos)
                              and self.showScoreScreen == True
                              and self.showWinScreen == False):
                            self.showGameScreen = True
                            self.showScoreScreen = False

                        # Check if ten and under button was clicked
                        if self.hasTenAndUnder():
                            if (self.tenAndUnderButton.isClicked(event.pos)
                                    and self.showGameScreen == True):

                                # Alert the server that player is turning in ten and under
                                self.n.send("tenAndUnder")

                                # Update the game for the client
                                self.game = self.n.send("get")

                                # Get the new player hand from the updated game
                                self.test_hand = get_hand(
                                    self.game.players[self.player].playerHand)

                        # Check if buttons on win screen were clicked and handle input
                        if self.showWinScreen == True:
                            self.winScreen.handleInput(event.pos)

                # Check if mouse is hovering over button
                self.isMouseHoveringOverButtons()

            # Check if the game is over
            if self.game.isGameOver() and self.showWinScreen == False:
                self.showWinScreen = True
                self.showGameScreen = False
                self.showScoreScreen = False

            self.draw()

    def draw(self):

        if self.showGameScreen == True and self.showWinScreen == False:

            # Blit the background of the screen
            self.screen.blit(self.background, (0, 0))

            # Draw the hand on the screen
            self.test_hand.draw(self.screen)

            # Draw the main pile to the screen
            self.main_pile.draw(self.screen)

            # Draw the trump image to the screen
            if self.trump_image is not None:
                self.trump_image.draw(self.screen)

            # Draw player turn indicator image when it is player's turn
            if self.game.players[self.player].playerTurn:
                self.playerTurnIndicator.draw(self.screen)

            # Draw the bid screen
            if self.game.biddingStage and self.game.players[
                    self.player].playerBidTurn:
                self.bid_screen.draw(self.screen, self.game)

            # Draw score button
            self.scoreButton.draw(self.screen)

            # Draw the ten and under button
            if self.hasTenAndUnder():
                self.tenAndUnderButton.draw(self.screen)

            # Draw the username list
            self.username_list.draw(self.game, self.screen)

            pygame.display.flip()

            # Time delay to keep the last card on screen in trick
            if (self.game.mainPile.size() % len(self.game.players) == 0
                    and self.game.mainPile.size() != 0
                    and not self.game.isPlayersReady()):
                pygame.time.delay(2000)
                self.n.send("ready")

        if self.showScoreScreen == True and self.showWinScreen == False:

            # Blit everything to the screen
            self.screen.blit(self.background, (0, 0))

            # Draw score button
            self.scoreButton.draw(self.screen)

            self.scoreScreen.draw(self.game, self.screen)

            pygame.display.flip()

        if self.showWinScreen == True:

            # Blit everything to the screen
            self.screen.blit(self.background, (0, 0))

            # Draw the win screen
            self.winScreen.draw(self.game, self.screen)

            pygame.display.flip()

        if self.gameReady == False:

            # Blit everything to the screen
            self.screen.blit(self.background, (0, 0))

            self.displayWaitMessage()

            pygame.display.flip()

    def hasTenAndUnder(self):
        """Player has a ten and under that meets the criteria to turn in"""

        return (
            # Players are actively bidding
            self.game.isBiddingStage() and
            # Player's turn to bid
            self.game.players[self.player].playerBidTurn and
            # Player has a ten and under
            self.test_hand.hasTenAndUnder() and (
                # Player is not the last bidder
                (self.game.getNumberOfBids() < (self.game.numPlayers - 1)) or
                # Player is the last bidder and someone has bid already
                ((self.game.getNumberOfBids() == (self.game.numPlayers - 1))
                 and (self.game.getHighestBid() > 0))))

    def displayWaitMessage(self):

        # Draw text to screen
        textColor = (0, 0, 0)
        text = "Waiting For More Players..."
        textWidth, textHeight = Resources.FONT_THIRTY.size(text)
        text = Resources.FONT_THIRTY.render(text, 1, textColor)

        self.screen.blit(
            text,
            (
                (Resources.SCREEN_WIDTH / 2) - (textWidth / 2),
                (Resources.SCREEN_HEIGHT / 2) - (textHeight / 2),
            ),
        )
Beispiel #16
0
    def __init__(
        self,
        username,
        isHost,
        gameName=None,
        gameKey=None,
        maxPlayers=None,
        gameMode=None,
        winningScore=None,
    ):
        Screen.__init__(self)

        # Set up connection to server
        self.n = Network()
        self.n.connect()

        # Tell the server to create/join the game and return player id
        if isHost:
            self.player = self.n.getPlayer(
                f"host/{gameName}/{maxPlayers}/{gameMode}/{winningScore}")
        else:
            self.player = self.n.getPlayer(f"join/{gameKey}")

        # Get the game from the server
        self.game = self.n.send("get")

        # Check if the game could be received from the server
        connectionAttempts = 0
        MAX_ATTMEPTS = 10000
        while self.game is None and connectionAttempts < MAX_ATTMEPTS:
            self.game = self.n.send("get")
            connectionAttempts += 1

        # Only initialize game screen objects if server connectino was succesful
        if self.game is not None:

            # Set player username
            self.game.players[self.player].username = username
            self.n.send("username: "******"S"

            self.scoreButton = Button(
                SCORE_BUTTON_WIDTH,
                SCORE_BUTTON_HEIGHT,
                SCORE_BUTTON_X,
                SCORE_BUTTON_Y,
                SCORE_BUTTON_COLOR,
                SCORE_BUTTON_TEXT_COLOR,
                SCORE_BUTTON_TEXT,
            )

            # Ten and Under Button
            TEN_AND_UNDER_BUTTON_WIDTH = 100
            TEN_AND_UNDER_BUTTON_HEIGHT = 100
            TEN_AND_UNDER_BUTTON_PADDING = 10
            TEN_AND_UNDER_BUTTON_X = (self.screen.get_width() /
                                      2) - (TEN_AND_UNDER_BUTTON_WIDTH / 2)
            TEN_AND_UNDER_BUTTON_Y = (
                (self.screen.get_height() / 2) -
                (TEN_AND_UNDER_BUTTON_HEIGHT / 2)) - (
                    TEN_AND_UNDER_BUTTON_HEIGHT + TEN_AND_UNDER_BUTTON_PADDING)
            TEN_AND_UNDER_BUTTON_COLOR = (255, 0, 0)
            TEN_AND_UNDER_BUTTON_TEXT_COLOR = (0, 0, 0)
            TEN_AND_UNDER_BUTTON_TEXT = "TEN\nAND\nUNDER"

            self.tenAndUnderButton = Button(
                TEN_AND_UNDER_BUTTON_WIDTH,
                TEN_AND_UNDER_BUTTON_HEIGHT,
                TEN_AND_UNDER_BUTTON_X,
                TEN_AND_UNDER_BUTTON_Y,
                TEN_AND_UNDER_BUTTON_COLOR,
                TEN_AND_UNDER_BUTTON_TEXT_COLOR,
                TEN_AND_UNDER_BUTTON_TEXT,
            )

            # List of all the buttons
            self.buttonList.append(self.scoreButton)
            self.buttonList.append(self.tenAndUnderButton)

            # Score screen
            self.scoreScreen = ScoreScreen()

            # Win Screen
            self.winScreen = WinScreen()

            self.showGameScreen = False
            self.showScoreScreen = False
            self.showWinScreen = False
            self.gameReady = False
Beispiel #17
0
    def __init__(self, config, session=None):
        self.config = config
        self.save = config.bool("save", True)
        self.task = config.string("task", "train")
        self.dataset = config.string("dataset").lower()
        self.dataset_val = config.string("dataset_val", "").lower()
        if self.dataset_val == "":
            self.dataset_val = self.dataset
        self.num_epochs = config.int("num_epochs", 1000)
        self.session = self._create_session(session)
        self.global_step = tf.Variable(0, name='global_step', trainable=False)
        self.build_networks = config.bool("build_networks", True)

        need_train = self.task not in ("eval", "forward_detections_recurrent",
                                       "forward_tracking",
                                       "forward_clustering",
                                       "forward_refine_bboxes",
                                       "forward_crop_detection",
                                       "forward_mot_detection_rescore",
                                       "forward_mots_detection_rescore",
                                       "forward_mot_detection_linking",
                                       "forward_refine_detection_bboxes")
        if need_train:
            self.train_data = load_dataset(config, "train", self.session,
                                           self.dataset)
            freeze_batchnorm = config.bool("freeze_batchnorm", False)
            print("creating trainnet...", file=log.v1)
            if self.build_networks:
                self.train_network = Network(self.config,
                                             self.train_data,
                                             is_trainnet=True,
                                             freeze_batchnorm=freeze_batchnorm,
                                             name="trainnet")
            else:
                self.train_network = None
        else:
            self.train_data = None
            self.train_network = None

        need_val = self.task != "train_no_val"
        if need_val:
            self.valid_data = load_dataset(config, "val", self.session,
                                           self.dataset_val)
            print("creating testnet...", file=log.v1)
            reuse_variables = None if need_train else False
            if self.build_networks:
                self.test_network = Network(config,
                                            self.valid_data,
                                            is_trainnet=False,
                                            freeze_batchnorm=True,
                                            name="testnet",
                                            reuse_variables=reuse_variables)
            else:
                self.test_network = None
        else:
            self.valid_data = None
            self.test_network = None
        self.trainer = Trainer(config, self.train_network, self.test_network,
                               self.global_step, self.session)
        self.saver = Saver(config, self.session)

        tf.global_variables_initializer().run()
        tf.local_variables_initializer().run()

        self.start_epoch = self.saver.try_load_weights()
        self.session.graph.finalize()
class NetworkController(object):
    data = None
    distancesInLocation = None
    distancesBetweenLocation = None
    locationsCount = None

    trainData = None
    trainPoints = None

    generateLocationsArray = None
    generateParametersArray = None
    firstPoints = None

    mostPopularStartLocations = None

    lastLocations = None
    lastLocationsWindow = None
    histCount = None

    Network = Network()

    window = None

    def getWindowSize(self):
        return self.window

    def __init__(self):
        self.data = None
        self.locationsCount = None
        self.mostPopularStartLocations = None

        self.levyCoeff = 4

    def maxTimeCalc(self, data):
        max = data[0][0].time
        min = data[0][0].time
        for arr in data:
            for a in arr:
                if a.time > max:
                    max = a.time
                if a.time < min:
                    min = a.time

        return max, min

    def maxPauseTimeCalc(self, data):
        max = data[0][0].pauseTime
        min = data[0][0].pauseTime
        for arr in data:
            for a in arr:
                if a.pauseTime > max:
                    max = a.pauseTime
                if a.pauseTime < min:
                    min = a.pauseTime

        return max, min

    def setTrainData(self, data, locations, window):
        self.trainPoints = data
        self.locationsCount = len(locations)
        self.distancesInLocation = []
        self.distancesBetweenLocation = []
        self.firstPoints = []
        self.trainStartTimes = []
        self.lastLocations = []
        self.lastLocationsWindow = window
        self.window = window

        self.mostPopularStartLocations = []

        d = []
        num = 0
        for i in data:
            countMostPopular = 0
            locs = [0] * self.locationsCount
            d1 = []
            a = [0] * self.locationsCount
            self.distancesInLocation.append(a.copy())
            self.distancesBetweenLocation.append(a.copy())

            self.firstPoints.append([])

            if not len(self.firstPoints[num]) == self.window:
                self.firstPoints[num].append(i[0].departurePoint)

            for j in i:
                dep = Location.FindLocation(j.departurePoint, locations)
                dest = Location.FindLocation(j.destination, locations)
                d1.append(LocationJump(dep, dest, j.time, j.pauseTime))

                if not len(self.firstPoints[num]) == self.window:
                    self.firstPoints[num].append(j.destination)
                if dep.Number == dest.Number:
                    self.distancesInLocation[len(
                        self.distancesInLocation
                    ) - 1][dep.Number] = self.distancesInLocation[len(
                        self.distancesInLocation
                    ) - 1][
                        dep.
                        Number] + PathPoint.distanceInMetersBetweenEarthCoordinates(
                            j.departurePoint.latitude,
                            j.departurePoint.longitude, j.destination.latitude,
                            j.destination.longitude)
                else:
                    pass
            d.append(d1)
            num = num + 1
        self.data = d

        self.maxTime, self.minTime = self.maxTimeCalc(data)
        self.maxPauseTime, self.minPauseTime = self.maxPauseTimeCalc(data)

        self.trainData = []

        self.generateLocationsArray = []
        self.generateParametersArray = []

        num = 0

        for arr in self.data:
            if (len(arr) == 0):
                continue
            self.generateLocationsArray.append([])
            self.generateParametersArray.append([])

            x = []
            a = [0] * self.locationsCount
            a[arr[0].departure.Number] = 1
            x.append(a)

            if not len(self.generateLocationsArray[num]) == self.window:
                self.generateLocationsArray[num].append(a)

            t = []

            for i in range(len(arr)):
                a = [0] * self.locationsCount
                a[arr[i].destination.Number] = 1
                x.append(a)
                if not len(self.generateLocationsArray[num]) == self.window:
                    self.generateLocationsArray[num].append(a)
                a = [
                    Levy.levyCalculate(arr[i].time, self.minTime,
                                       self.maxTime / self.levyCoeff),
                    Levy.levyCalculate(arr[i].pauseTime, self.minPauseTime,
                                       self.maxPauseTime / self.levyCoeff)
                ]
                t.append(a)

                if not len(
                        self.generateParametersArray[num]) == self.window - 1:
                    self.generateParametersArray[num].append(a)

            self.trainData.append([x, t])
            num = num + 1

        Plotter.setTrainIntervals(self.trainData, self.distancesInLocation,
                                  self.trainPoints, self.locationsCount,
                                  self.minTime, self.maxTime,
                                  self.minPauseTime, self.maxPauseTime,
                                  self.levyCoeff)

        self.histCount = len(
            Plotter.trainIntervals.tracesLength) + 1 - 1 + len(
                Plotter.trainIntervals.pauseTimes) - 1

    def plotTrain(self):
        Plotter.plotTrained(self.trainData, self.distancesInLocation,
                            self.trainPoints, self.locationsCount,
                            self.minTime, self.maxTime, self.minPauseTime,
                            self.maxPauseTime, self.levyCoeff)

    def plotCharacteristics(self, data):
        Plotter.plot(data, self.locationsCount, self.minTime, self.maxTime,
                     self.minPauseTime, self.maxPauseTime, self.levyCoeff)

    def getTrainData(self):
        return self.data

    def getLocationsCount(self):
        return self.locationsCount

    def getFeaturesCount(self):
        return 4
        #TODO

    def generate(self, locations, parameters):
        count = [0] * len(self.trainData)
        for i in range(len(self.trainData)):
            count[i] = len(self.trainData[i]
                           [0]) - self.window  ###############################
        result = self.Network.generate(locations, parameters, count,
                                       self.firstPoints, self.minPauseTime,
                                       self.maxPauseTime, self.levyCoeff)
        self.plotCharacteristics(result)
Beispiel #19
0
if __name__ == "__main__":
    # AVD Checking...
    dev = Device()
    while not dev.check_device():
        print "waiting for device connection..."
        sleep(3)

    print "AVD Connected Successfully..."
    print

    # System Utility Initialization...
    proc_service = Procs()
    mem_service = MemInfo()
    log_service = Meow()
    network_service = Network()

    log_service.thread_up()

    with open(record_path, "r") as handle:
        for line in handle:
            if line.strip() == "":
                continue
            p, r = line.strip().split(", ")
            pre_status[p] = True if r == "T" else False

    f = Formatter()

    # Set Up Apk
    pp = pprint.PrettyPrinter(indent=4)
    samples_info = {}
Beispiel #20
0
class JoinScreen(Screen):
    def __init__(self):

        # Call Screen constructor
        Screen.__init__(self)

        # Make connection to the server
        self.n = Network()
        self.n.connect()

        # Retrieve list of games from server
        self.buttonListSubset = []
        self.buttonList = []
        self.buttonListSlice = [0, 4]
        self.updateGameList()

        # Page navigator to go to next or previous page of active games
        self.pageNavigator = PageNavigator()

    def updateGameList(self):

        gameList = self.n.send("gameList")

        self.buttonList = []

        # Game buttons
        i = 0
        BUTTON_WIDTH = 400
        BUTTON_HEIGHT = 100
        BUTTON_PADDING = 5

        # Loop through active games in the game list
        for game in gameList.games:

            # Only create a button for games that have open spots
            if game.maxPlayers != game.numPlayers:
                button = ActiveGameButton(
                    game,
                    BUTTON_WIDTH,
                    BUTTON_HEIGHT,
                    (Resources.SCREEN_WIDTH / 2) - (BUTTON_WIDTH / 2),
                    (0 + BUTTON_HEIGHT + BUTTON_PADDING) * i,
                    (255, 255, 255),
                    (0, 0, 0),
                )
                self.buttonList.append(button)
                i += 1

        # Only display 4 games per page
        if len(self.buttonList) > 4:
            self.buttonListSubset = self.buttonList[
                self.buttonListSlice[0]:self.buttonListSlice[1]]
        else:
            self.buttonListSubset = self.buttonList

        # Update the positions of all the buttons
        self.updateButtonPositions()

        # Check if mouse is hovering over buttons
        self.isMouseHoveringOverButtons()

    def updateButtonPositions(self):

        i = 0
        BUTTON_WIDTH = 400
        BUTTON_HEIGHT = 100
        BUTTON_PADDING = 5
        for button in self.buttonListSubset:
            button.setPos(
                (Resources.SCREEN_WIDTH / 2) - (BUTTON_WIDTH / 2),
                (0 + BUTTON_HEIGHT + BUTTON_PADDING) * i,
            )
            i += 1

    def run(self):

        # Game loop
        while True:
            self.clock.tick(60)

            for event in pygame.event.get():

                # Check for quit event
                if event.type == QUIT:
                    return GameState.QUIT, None, None, None

                # Check for click event
                if event.type == pygame.MOUSEBUTTONUP:
                    if event.button == 1:  # the right mouse button

                        # Check if game button was clicked
                        for button in self.buttonListSubset:
                            if button.isClicked(event.pos):
                                self.n.disconnect()
                                return (
                                    GameState.NEWGAME,
                                    False,
                                    button.gameName,
                                    button.gameId,
                                )

                    if self.pageNavigator.isLeftClicked(event.pos):
                        if 0 <= self.buttonListSlice[0] - 1 < len(
                                self.buttonList):
                            self.buttonListSlice[
                                0] = self.buttonListSlice[0] - 1
                            self.buttonListSlice[
                                1] = self.buttonListSlice[1] - 1

                    if self.pageNavigator.isRightClicked(event.pos):
                        if 0 <= self.buttonListSlice[1] < len(self.buttonList):
                            self.buttonListSlice[
                                1] = self.buttonListSlice[1] + 1
                            self.buttonListSlice[
                                0] = self.buttonListSlice[0] + 1

            # Update game list
            self.updateGameList()

            # Draw everything to the screen
            self.draw()

    def draw(self):

        # Blit the background of the screen
        self.screen.blit(self.background, (0, 0))

        for button in self.buttonListSubset:
            button.draw(self.screen)

        # if there are no active games display error message
        if len(self.buttonList) == 0:
            self.displayError()

        # Draw the page navigator
        self.pageNavigator.draw(self.screen)

        pygame.display.flip()

    def displayError(self):

        # Draw text to screen
        textColor = (0, 0, 0)
        text = "No Active Games"
        textWidth, textHeight = Resources.FONT_TWENTY_FIVE.size(text)
        text = Resources.FONT_TWENTY_FIVE.render(text, 1, textColor)

        self.screen.blit(
            text,
            (
                (Resources.SCREEN_WIDTH / 2) - (textWidth / 2),
                (Resources.SCREEN_HEIGHT / 2) - (textHeight / 2),
            ),
        )