Ejemplo n.º 1
0
	def __init__(self, mode, playground):
		self.simulation = Simulation(self, mode, playground)
		self.camera = Camera(self, 70)
		self.mode = mode
		self.playground = playground
		# Players
		#----------------------------- 1st player -----------------------------
		self.players = []
		if mode == "NE" or mode == "vsNN":
			self.players.append(Player(self, NN_StrategyA.NN_StrategyA(), "left"))			
		else:
			self.players.append(Player(self, StrategyC.StrategyC(), "left"))
			# self.players.append(Player(self, MovementTest.MovementTest(), "left"))


		#----------------------------- 2nd player -----------------------------
		if not playground:
			if mode == "vsAI" or mode == "vsNN":
				self.players.append(Player(self, StrategyD.StrategyD(), "right"))
				# self.players.append(Player(self, MovementTest.MovementTest(), "right"))

			elif mode == "NE":
				self.players.append(Player(self, NN_StrategyA.NN_StrategyA(), "right"))
			else:
				self.players.append(Player(self, StrategyD.StrategyD(), "right"))

			self.players[0].opponent = self.players[1]
			self.players[1].opponent = self.players[0]
		
		# States
		self.gameDone = False
		self.gameTime = 0
		self.gameSpeed = 1
		self.scoreChangeAt = 0
		self.prevScore = [0, 0]		

		self.bestScorePlayer = -1
		self.winner = -1

		self.stepTime = MIN_STEP_TIME
		self.mousePosition = None
		self.leftMouseDown = False
		self.middleMouseDown = False
		self.rightMouseDown = False

		# Statistic data
		self.onSide = 0
		self.maxShotSpeed = [0, 0]
		self.puckControl = [0, 0]
		self.shotOnGoals = [0, 0]
		self.lostPucks = [0, 0]
Ejemplo n.º 2
0
def run(index):
    EnvironmentParameters.playround_with = 600
    EnvironmentParameters.playround_height = 600
    EnvironmentParameters.food_count = index // 4
    EnvironmentParameters.landmark_count = index

    SimulationParameters.ant_count = 25
    SimulationParameters.move_speed = 1
    SimulationParameters.move_angle = 15 / 180 * np.pi
    SimulationParameters.traceback_angle = 5 / 180 * np.pi
    SimulationParameters.vision_range = 25

    e = Environment().generate_environment(
        [],
        [(250, 0)],
        [(100, 0), (-100, 0), (0, 100), (0, -100), (200, 200), (200, -200), (-200, -200), (-200, 200)])
    s = Simulation(e)

    return s.simulateAll()
Ejemplo n.º 3
0
    def _simulate(args=None):
        # Read control panel values
        SimulationParameters.ant_count = count_scale.get()
        SimulationParameters.move_speed = speed_scale.get() / 100
        SimulationParameters.move_angle = angle_scale.get() / 180 * np.pi
        SimulationParameters.traceback_angle = traceback_angle_scale.get() / 180 * np.pi
        SimulationParameters.vision_range = vision_scale.get()

        plt.clf()  # clears previous graph
        plt.axis("off")
        plt.tight_layout(0)

        e = Environment().generate_random_environment()
        s = Simulation(e)
        result = s.simulateAll()

        if result is None:
            return

        drawPaths(canvas, result.getAll())
        drawEnvironment(canvas, e)
        canvas.draw()
Ejemplo n.º 4
0
def createSimulationWindow():
    simulationWindow = Toplevel(app)

    grid = CellGrid(simulationWindow, int(heightEntry.get()), int(widthEntry.get()), 15)
    grid.pack()

    simulation = Simulation()

    readyButton = Button(simulationWindow, text=" Setup Environment ", bg='#e46d69', state=NORMAL,
                         command=lambda: [prepare_simulation(simulation,grid),
                                          switchButtonState(simulateButton), switchButtonState(autoSimulateButton), switchButtonState(readyButton) ])
    readyButton.pack()

    simulateButton = Button(simulationWindow, text=" Next Step ", bg='#e46d69', state=DISABLED,
                            command=lambda: simulate(simulation, grid))
    simulateButton.pack()

    autoSimulateButton = Button(simulationWindow, text=" Start Simulation ", bg='#e46d69', state=DISABLED,
                            command=lambda: [auto_simulate(simulation, grid), switchButtonState(simulateButton)])
    autoSimulateButton.pack()
Ejemplo n.º 5
0
    return miner


def get_miners(random_percents, num_blocks_per_diffculty_adjustment, total_expected_time_per_block):
    miners = []
    for miner_i, percent in enumerate(random_percents):
        miners.append(get_miner(num_blocks_per_diffculty_adjustment, total_expected_time_per_block, percent, miner_i))
    return miners


def add_peers_to_miners(miners):
    for i, miner1 in enumerate(miners):
        for j, miner2 in enumerate(miners):
            if i == j:
                continue
            miner1.add_peer(miner2)


if __name__ == '__main__':
    num_miners = 10
    total_hashes_per_second = 50_000
    num_blocks_per_diffculty_adjustment = 2016
    total_expected_time_per_block = 200
    num_rounds = total_expected_time_per_block * 5_000  # 50 blocks
    num_games = 1
    random_percents = get_random_percents()
    miners = get_miners(random_percents, num_blocks_per_diffculty_adjustment, total_expected_time_per_block)
    add_peers_to_miners(miners)
    simul = Simulation(miners, num_rounds, num_games)
    simul.run(block_reward=0)
Ejemplo n.º 6
0
class Game():
	def __init__(self, mode, playground):
		self.simulation = Simulation(self, mode, playground)
		self.camera = Camera(self, 70)
		self.mode = mode
		self.playground = playground
		# Players
		#----------------------------- 1st player -----------------------------
		self.players = []
		if mode == "NE" or mode == "vsNN":
			self.players.append(Player(self, NN_StrategyA.NN_StrategyA(), "left"))			
		else:
			self.players.append(Player(self, StrategyC.StrategyC(), "left"))
			# self.players.append(Player(self, MovementTest.MovementTest(), "left"))


		#----------------------------- 2nd player -----------------------------
		if not playground:
			if mode == "vsAI" or mode == "vsNN":
				self.players.append(Player(self, StrategyD.StrategyD(), "right"))
				# self.players.append(Player(self, MovementTest.MovementTest(), "right"))

			elif mode == "NE":
				self.players.append(Player(self, NN_StrategyA.NN_StrategyA(), "right"))
			else:
				self.players.append(Player(self, StrategyD.StrategyD(), "right"))

			self.players[0].opponent = self.players[1]
			self.players[1].opponent = self.players[0]
		
		# States
		self.gameDone = False
		self.gameTime = 0
		self.gameSpeed = 1
		self.scoreChangeAt = 0
		self.prevScore = [0, 0]		

		self.bestScorePlayer = -1
		self.winner = -1

		self.stepTime = MIN_STEP_TIME
		self.mousePosition = None
		self.leftMouseDown = False
		self.middleMouseDown = False
		self.rightMouseDown = False

		# Statistic data
		self.onSide = 0
		self.maxShotSpeed = [0, 0]
		self.puckControl = [0, 0]
		self.shotOnGoals = [0, 0]
		self.lostPucks = [0, 0]

	def update(self):
		stepTime = self.stepTime
		mousePos = self.mousePosition
		self.gameTime += stepTime

		if not self.gameDone:
			if self.middleMouseDown: self.simulation.middleMouseDown(mousePos)
			if self.rightMouseDown: self.simulation.rightMouseDown(mousePos)

			self.simulation.step(stepTime)
			self.camera.update()

			for i in range(len(self.players)):
				self.players[i].updatePosition(self.simulation.strikers[i].position)
				if self.camera.newData:
					self.players[i].cameraInput(self.camera.puckPosition)

				self.players[i].update(stepTime)

				if self.leftMouseDown: 
					player = not self.playground
					mouseVec = self.players[player].getRelativePos(Vector2((p2uX(mousePos[0]), p2uY(mousePos[1]))))
					self.players[player].strategy.setDesiredPosition(mouseVec)

				self.simulation.strikers[i].desiredVelocity = self.players[i].getDesiredVelocity()

			self.camera.newData = False



			self.checkData(stepTime)
			self.checkGoal()
			# self.checkEnd()

		return self

	def checkData(self, stepTime):	
		puck = self.players[0].strategy.puck
		puckPos = puck.position
		if puckPos.x > FIELD_WIDTH - (STRIKER_AREA_WIDTH) and not self.onSide == 1:
			self.onSide = 1
			self.checkShot(-1)
			self.lostPucks[0] += 1
			print(self.lostPucks)
			
		elif puckPos.x < STRIKER_AREA_WIDTH and not self.onSide == -1:
			self.onSide = -1
			self.checkShot(1)
			self.lostPucks[1] += 1
		elif abs(puckPos.x - FIELD_WIDTH/2) < (FIELD_WIDTH - 2*(STRIKER_AREA_WIDTH))/2:
			self.onSide = 0

		if not self.onSide == 0:
			self.puckControl[max(0, self.onSide)] += stepTime


		
		

	def checkShot(self, dir):
		print("Puck Control:", self.puckControl)

		puck = self.players[0].strategy.puck
		if puck.speedMagnitude > 700 and abs(puck.vector.y) < .9:
			# print("good shot, dir:", dir)
			if len(puck.trajectory) > 0 and abs(puck.trajectory[-1].end.y) < GOAL_SPAN/2 * .9:
				self.shotOnGoals[max(0, dir)] += 1
				print("Shots on goal: ", self.shotOnGoals)
			if self.maxShotSpeed[max(0, dir)] < puck.speedMagnitude < 10000 :
				self.maxShotSpeed[max(0, dir)] = puck.speedMagnitude
				# print("Max shot speed:", self.maxShotSpeed)

	def checkGoal(self):
		if not self.playground:
			if self.simulation.puck.position.x < 0:
				self.players[1].goals += 1
				self.players[1].score += POINTS_PER_GOAL
				self.simulation.spawnPuck()
				# Scheduler().schedule(self.simulation.spawnPuck, 1)
			if self.simulation.puck.position.x > FIELD_WIDTH:
				self.players[0].goals += 1
				self.players[0].score += POINTS_PER_GOAL
				self.simulation.spawnPuck()
				# Scheduler().schedule(self.simulation.spawnPuck, 1)

	def checkEnd(self):
		goals = [self.players[0].goals, self.players[1].goals]
		score = [self.players[0].score, self.players[1].score]
		
		if not(score == self.prevScore) and self.mode == "NE":
			self.scoreChangeAt = self.gameTime
			self.prevScore = score
		
		if self.mode == "NE" and self.gameTime - self.scoreChangeAt > 20:
			self.gameDone = True

		if self.mode == "NE" and self.gameTime >= TIME_LIMIT:
			self.gameDone = True			

		for player in self.players:
			if player.goals >= GOAL_LIMIT:
				self.gameDone = True

		if self.gameDone:
			goals = [self.players[0].goals, self.players[1].goals]
			score = [self.players[0].score, self.players[1].score]
			
			if goals[0] == goals[1]:
				self.winner = 2
			else:
				self.winner = goals.index(max(goals))

			self.bestScorePlayer = score.index(max(score))
				
Ejemplo n.º 7
0
def main():
    try:
        simulation = Simulation()
    except SimulationException as e:
        print(e)
        print("Critical error occured, see above, exiting simulation..")