def run_game(): # 初始化游戏并创建一个屏幕对象 all_setting = AllSettings() stats = Gamestats(all_setting) pygame.init() # 初始化pygame # 创建屏幕 screen = pygame.display.set_mode((all_setting.screen_width, all_setting.screen_height)) pygame.display.set_caption("Alien Invation") # 创建飞船对象 ship = Ship(screen, all_setting) # 创建一个用于存储子弹,外星人,外星人子弹的编组 bullets = Group() aliens = Group() enemy_bullets = Group() shield = Shield(ship, screen) # gf.creat_fleet(all_setting, screen, ship, aliens) play_button = Button(all_setting, screen, "Play") score_get = Scoreboard(all_setting, screen, stats) # 开始游戏主循环 while True: gf.check_events(all_setting, screen, ship, aliens, bullets, stats, play_button, score_get, enemy_bullets) if stats.game_active: ship.update() shield.set_pos() gf.update_bullets(bullets, aliens, all_setting, screen, ship, stats, score_get, enemy_bullets) gf.update_aliens(all_setting, aliens, ship, stats, screen, bullets, score_get, shield, enemy_bullets) gf.update_enemy_bullets(enemy_bullets, all_setting) gf.update_screen(all_setting, screen, ship, aliens, bullets, play_button, stats, score_get, shield, enemy_bullets)
def createShields(self): self.tb.sorting('shit') name = self.ui.lineEdit_shield_template.text() prfx = self.ui.lineEdit_shield_prefix.text() ot_x = self.ui.spinBox_ots_shit_X.value() ot_y = self.ui.spinBox_ots_shit_Y.value() list = self.tb.getTable('shit') shit = Shield(name, self.tf, self.mf, ot_x, ot_y, prfx) shit.createProgs(list) self.statusBar().showMessage('Программы Щитов созданы!') mes = Mess() mes.MesProgComplete('Программы готовы', 'Программы Щитов созданы!')
def post(self): methods = self.request.get('core') url = self.request.get('url') pers = self.request.get('custom_hash') remoteip = self.request.remote_addr """ challenge = self.request.get('recaptcha_challenge_field') response = self.request.get('recaptcha_response_field') remoteip = environ['REMOTE_ADDR'] cResponse = captcha.submit(challenge,response,"6LfAO78SAAAAANHiQxEObRo36llHrkQa3kauMaD3",remoteip) list_url="Errore" """ #if cResponse.is_valid: shield = Shield(remoteip) if shield.getShieldSentence()==False: try : if url[0:7]!="http://": url ="http://"+url #result = urlfetch.fetch(url=url,method=urlfetch.GET,follow_redirects=False) #if result.status_code == 200: if methods=="classic": res = formats.DecodeJson(core.short(url)) list_url = res.getMessage() elif methods=="custom": res = formats.DecodeJson(core.short(url,pers)) list_url = res.getMessage() else : res = "non trovato" list_url = res #elif result.status_code in (300,301,302,303,307): # res = "redirect" # list_url = res except : res = "Indirizzo non valido" list_url = res else: #list_url = cResponse.error_code list_url = "Errore Numero tentativi orari superati" template_values = { 'urls': list_url, } path = os.path.join(os.path.dirname(__file__), 'templates/result.html') self.response.out.write(template.render(path, template_values))
def load(self, file_name): with open(file_name, 'r') as read_json: the_entire_file = read_json.read() the_entire_dict = json.loads(the_entire_file) for shield_name in the_entire_dict['shields']: the_shield_dict = the_entire_dict['shields'][shield_name] new_shield = Shield(shield_name, the_shield_dict['type'], the_shield_dict['size'], the_shield_dict['weight'], the_shield_dict['strength']) #prevents double loads, same name overwriting if shield_name not in self.shields: # shield_name is the string, new_shield is the Shield object! self.shields[shield_name] = new_shield for weapon_name in the_entire_dict['weapons']: the_weapon_dict = the_entire_dict['weapons'][weapon_name] new_weapon = Weapon(weapon_name, the_weapon_dict['type'], the_weapon_dict['reach'], the_weapon_dict['weight'], the_weapon_dict['strength']) #prevents double loads, same name overwriting if weapon_name not in self.weapons: # shield_name is the string, new_shield is the Shield object! self.weapons[weapon_name] = new_weapon print(self.weapons) print(self.shields)
def __init__(self): ## sets dimension of the screen and the background pygame.init() self.w = 900 self.h = 600 self.screen = pygame.display.set_mode((self.w, self.h), pygame.FULLSCREEN) self.background = pygame.image.load("background.png") ## creates all the objects and lists needed for the game self.player = Player(self.w, self.h, self.background) self.shooter = Shooter(self.player.x, self.player.y, self.player.d) self.shield = Shield(self.player.x, self.player.y, self.player.d) self.generator = Generator(self.player.x, self.player.y, self.player.d) self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation) self.enemies = [Enemy(self.w, self.h)] self.enemies_2 = [] self.counter_enemies_2_dead = 0 self.collectible = Collectible(self.w, self.h) self.ray = Ray(self.w, self.h) ## loads energy image and sets its default value self.energy_5 = pygame.image.load("energy_5.png") self.energy = 100 ## sets all default values self.points = 0 self.killed_big_enemies = 0 self.killed_small_enemies = 0 self.rays_collected = 0 self.collectibles_collected = 0 ## initializes fonts and creates the first text font = pygame.font.SysFont("comic", 64) self.font = pygame.font.SysFont("arial", 10) text = font.render("Click to play", True, (255, 255, 255)) ## loads sound self.sound = pygame.mixer.Sound("blip.wav") ## sets timer and default timer variables self.clock = pygame.time.Clock() self.timer = 0 self.seconds = 0 self.minutes = 0 ## manages beginning screen and first inputs beginning = True while beginning: self.screen.fill((0, 0, 0)) self.screen.blit(text, ((self.w / 2) - 150, (self.h / 2) - 64)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.display.quit() if event.type == pygame.MOUSEBUTTONUP: beginning = False self.screen.blit(self.background, (0, 0)) self.screen.blit(self.player.images[1], (self.player.x, self.player.y)) self.screen.blit(self.shooter.image, (self.shooter.x, self.shooter.y)) self.running() pygame.display.update()
def generate5(score): dx = random.random() if dx < 0.9: dx -= 1.0 dy = random.random() if dy < 0.9: dy -= 1.0 mag = 1 + score / 100 dx *= mag dy *= mag side = random.randint(1, 4) if side == 1: # left x = 0 y = random.uniform(0, get_canvas_height()) if dx < 0: dx = -dx elif side == 2: # bottom x = random.uniform(0, get_canvas_width()) y = 0 if dy < 0: dy = -dy elif side == 3: # right x = get_canvas_width() y = random.uniform(0, get_canvas_height()) if dx > 0: dx = -dx else: # side == 4: # up x = random.uniform(0, get_canvas_width()) y = get_canvas_height() if dy > 0: dy = -dy g = Shield((x, y), (dx, dy)) gfw.world.add(gfw.layer.shield, g)
def load_shields(self): sprites = SpriteSheet("shields").load_strip((0, 0, 87, 84), 4) return [ Shield([sprites[i]], (.20 * (i + 1) * SCREEN_WIDTH, SCREEN_HEIGHT * 0.75)) for i in range(len(sprites)) ]
def testShip(): alf = Shield("Standard Shield", 500, 12) w1 = Weapon("Standard Issue Phaser Array", 132, "Directed Energy") w2 = Weapon("Standard Issue Photon Torpedoes", 179, "Antimatter") ship = Ship("VOY", "UFP", 2000, alf, [w1, w2], 200) ship.addToCargoHold(ship.weapons[0]) ship.addToCargoHold(ship.weapons[1]) return ship
def _initEmptyBoard(self): """ :return: the initialized board """ board = [[Square(None, i, j) for j in range(5)] for i in range(5)] board[0][0] = Square(Note(False), 0, 0) board[0][1] = Square(Governance(False), 0, 1) board[0][2] = Square(Relay(False), 0, 2) board[0][3] = Square(Shield(False), 0, 3) board[0][4] = Square(Drive(False), 0, 4) board[1][4] = Square(Preview(False), 1, 4) board[3][0] = Square(Preview(True), 3, 0) board[4][0] = Square(Drive(True), 4, 0) board[4][1] = Square(Shield(True), 4, 1) board[4][2] = Square(Relay(True), 4, 2) board[4][3] = Square(Governance(True), 4, 3) board[4][4] = Square(Note(True), 4, 4) return board
def __init__(self, starting_position, universe): self.position_ = starting_position self.universe_ = universe self.font_size_ = 22 self.id_ = self.create_ID() self.max_health_ = 100 self.selected_weapon_index_ = 0 self.shield_ = Shield(self.position_, 100) self.size_ = 20 self.text_antialias_ = 1 self.ui_font_ = pygame.font.SysFont("monospace", self.font_size_) self.weapon_label_x_spacing_ = 5 self.weapon_label_y_spacing_ = 10 self.weapons_ = [Rifle(self.universe_), HomingMissiles(self.universe_)] self.current_weapon_ = self.weapons_[self.selected_weapon_index_] self.health_ = self.max_health_
def game(): pygame.init() play_intro() grid = loadtxt('maze.txt', dtype=str) rows, cols = grid.shape width, height = (24, 24) screen_size = (width*cols, height*rows) screen = pygame.display.set_mode(screen_size, 0, 32) block_list = [] for col in range(cols): for row in range(rows): value = grid[row][col] if value == 'X': pos = (col*width, row*height) block_list.append(Brick((width, height), pos)) elif value == 'S': pos = (col*width, row*height) block_list.append(Shield((width, height), pos)) elif value == '-' or value == '|' or value == 'N': pos = (col * width, row * height) block_list.append(Pill((width, height), pos)) elif value == 'P': pos = (col * width, row * height) block_list.append(Powerpill((width, height), pos)) elif value == 'Y': pos = (col * width, row * height) block_list.append(Pacman((width, height), pos)) elif value == 'b': pos = (col * width, row * height) block_list.append(Blinky((width, height), pos)) elif value == 'i': pos = (col * width, row * height) block_list.append(Inky((width, height), pos)) elif value == 'p': pos = (col * width, row * height) block_list.append(Pinky((width, height), pos)) elif value == 'c': pos = (col * width, row * height) block_list.append(Clyde((width, height), pos)) background = pygame.Surface(screen_size).convert() background.fill((0, 0, 0)) while True: for event in pygame.event.get(): if event.type == QUIT: exit() # pacman.move() # for tile in tiles: # pacman.collide(tile) screen.blit(background, (0, 0)) for blocks in block_list: blocks.draw(screen) # pacman.draw(screen) pygame.display.update()
def __init__(self, x=0, y=0, vx=0, vy=0): super().__init__(x, y, vx, vy) self.oldVel = self.vel self.input = {} self.input['left'] = False self.input['right'] = False self.input['up'] = False self.input['down'] = False self.input['shot'] = False self.input['dash'] = False self.wasMoving = False self.active = True self.bullets = [] self.radius = Player.RADIUS self.cooldownS = 0 #Player.SHOT_COOLDOWN self.dashCool = 0 #Player.DASH_COOLDOWN self.mouseDir = Vector(1, 0) self.attackMode = Player.AttackMode.SHOOTING self.shield = Shield(self.pos, self.radius * 1.2) self.melee = Melee(self.pos, self.radius * 1.8)
def create_shield(self): shield_name = input( '\tWhat is the name of the shield we are creating? ') shield_type = input( '\tWhat is the type of the shield we are creating? ') size = int(input('\tWhat is the shield size? ')) weight = int(input('\tWhat is the shield weight? ')) strength = int(input('\tWhat is the shield strength? ')) if shield_name not in self.shields: self.shields[shield_name] = Shield(shield_name, shield_type, size, weight, strength) print('\t The shield %s has been created.' % shield_name)
def dcmotor (learning_method, number_of_rollouts, simulation_steps, learning_eposides, critic_structure, actor_structure, train_dir,\ nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100): A = np.matrix([[0.98965, 1.4747e-08], [7.4506e-09, 0]]) B = np.matrix([[128], [0]]) #intial state space s_min = np.array([[-1.0], [-1.0]]) s_max = np.array([[1.0], [1.0]]) #sample an initial condition for system x0 = np.matrix([ [random.uniform(s_min[0, 0], s_max[0, 0])], [random.uniform(s_min[1, 0], s_max[1, 0])], ]) print("Sampled initial state is:\n {}".format(x0)) Q = np.matrix("1 0 ; 0 1") R = np.matrix(".0005") x_min = np.array([[-1.5], [-1.5]]) x_max = np.array([[1.5], [1.5]]) u_min = np.array([[-1.]]) u_max = np.array([[1.]]) env = Environment(A, B, u_min, u_max, s_min, s_max, x_min, x_max, Q, R) args = { 'actor_lr': 0.000001, 'critic_lr': 0.00001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 100, 'max_episodes': learning_eposides, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir + "model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 5000 } actor = DDPG(env, args) #################### Shield ################# model_path = os.path.split(args['model_path'])[0] + '/' linear_func_model_name = 'K.model' model_path = model_path + linear_func_model_name + '.npy' shield = Shield(env, actor, model_path, force_learning=retrain_shield) shield.train_shield(learning_method, number_of_rollouts, simulation_steps, explore_mag=0.0004, step_size=0.0005) if shield_test: shield.test_shield(test_episodes, 5000, mode="single")
def cap_attack(bots, cap): global money for b in bots: if cap.range >= math.sqrt( (cap.rect.centerx - b.rect.centerx)**2 + (cap.rect.centery - b.rect.centery)**2 ) and cap.rect.x != cap.x and not cap.moving and cap.shield == None: cap.shield = Shield(cap.rect.x, cap.rect.y) cap.direction = direction(cap, b) if cap.shield != None: money += cap.shield.throw(cap, bots, DISPLAYSURF) cap.change_image()
def prep_shield_count(self): """Show how many shields are left""" # Position the shield_image below the ship_image self.shield_image = Group() shield = Shield(self.ai_settings, self.screen, size='mini') shield.rect.x = 20 shield.rect.y = 20 + self.ship.height self.shield_image.add(shield) # Display the shield count image self.display_shield_count() #Position the shield count image self.shield_count_rect = self.shield_count.get_rect() self.shield_count_rect.y = shield.rect.y + 10 self.shield_count_rect.left = shield.rect.right + 10
def test_shield(): shield = Shield ("192.168.1.1") assert shield.getShieldSentence() == False shield2 = Shield("192.168.1.1") assert shield2.getShieldSentence() == False shield3 =Shield("192.168.1.1") shield4 = Shield("192.168.1.1") shield5=Shield("192.168.1.1") shield6=Shield("192.168.1.1") shield7=Shield("192.168.1.1") assert shield7.getShieldSentence() == True
def cooling (learning_method, number_of_rollouts, simulation_steps, learning_eposides, critic_structure, actor_structure, train_dir,\ nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100): A = np.matrix([ [1.01,0.01,0], [0.01,1.01,0.01], [0.0,0.01,1.01]]) B = np.matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) #intial state space s_min = np.array([[ 1.6],[ 1.6], [1.6]]) s_max = np.array([[ 3.2],[ 3.2], [3.2]]) Q = np.eye(3) R = np.eye(3) x_min = np.array([[-3.2],[-3.2],[-3.2]]) x_max = np.array([[3.2],[3.2],[3.2]]) u_min = np.array([[-1.],[-1.],[-1.]]) u_max = np.array([[ 1.],[ 1.],[ 1.]]) env = Environment(A, B, u_min, u_max, s_min, s_max, x_min, x_max, Q, R, bad_reward=-1000) args = { 'actor_lr': 0.001, 'critic_lr': 0.01, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 100, 'max_episodes': learning_eposides, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir+"model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 5000} actor = DDPG(env, args) #################### Shield ################# model_path = os.path.split(args['model_path'])[0]+'/' linear_func_model_name = 'K.model' model_path = model_path+linear_func_model_name+'.npy' names = {0:"cart position, meters", 1:"cart velocity", 2:"pendulum angle, radians", 3:"pendulum angle velocity"} shield = Shield(env, actor, model_path, force_learning=retrain_shield) shield.train_shield(learning_method, number_of_rollouts, simulation_steps, names=names, explore_mag = 0.02, step_size = 0.0025) if shield_test: shield.test_shield(test_episodes, 5000, mode="single")
def readFile(screen, blocks, shield, powerpills, intersections): file = open("images/otherpacmanportalmaze.txt", "r") contents = file.read() line = '' all_lines = [] for chars in contents: if chars != '\n': line += chars else: all_lines.append(line) line = '' i = 0 j = 0 intersection_num = 0 for rows in all_lines: for chars in rows: if chars == 'X': new = Blocks(screen) new.rect.x, new.rect.y = 13 * i, 13 * j blocks.add(new) elif chars == 'd': thepowerpill = Powerpills(screen) thepowerpill.rect.x, thepowerpill.rect.y = 13 * i, 13 * j powerpills.add(thepowerpill) elif chars == 'b': thepowerpill = Powerpills(screen, 'big') thepowerpill.rect.x, thepowerpill.rect.y = 13 * i, 13 * j powerpills.add(thepowerpill) elif chars == 'i': intersection = Intersections(screen, intersection_num) intersection_num += 1 intersection.rect.x, intersection.rect.y = 13 * i, 13 * j intersections.add(intersection) elif chars == 'o': theshield = Shield(screen) theshield.rect.x, theshield.rect.y = 13 * i, 13 * j shield.add(theshield) i += 1 i = 0 j += 1
def cruise (learning_method, number_of_rollouts, simulation_steps,learning_eposides, critic_structure, actor_structure, train_dir,\ nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100): A = np.matrix([[0.99501]]) B = np.matrix([[0.0078125]]) #intial state space s_min = np.array([[-1.0]]) s_max = np.array([[ 1.0]]) Q = np.matrix("1") R = np.matrix(".0005") x_min = np.array([[-1.5]]) x_max = np.array([[ 1.5]]) u_min = np.array([[-10.]]) u_max = np.array([[ 10.]]) env = Environment(A, B, u_min, u_max, s_min, s_max, x_min, x_max, Q, R) args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 100, 'max_episodes': learning_eposides, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir+"model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 5000} actor = DDPG(env, args) #################### Shield ################# model_path = os.path.split(args['model_path'])[0]+'/' linear_func_model_name = 'K.model' model_path = model_path+linear_func_model_name+'.npy' shield = Shield(env, actor, model_path, force_learning=retrain_shield, debug=False) shield.train_shield(learning_method, number_of_rollouts, simulation_steps, eq_err=0, explore_mag = 1.0, step_size = 1.0) if shield_test: shield.test_shield(test_episodes, 5000, mode="single")
def readFile(screen, blocks, shield, powerpills, portal): file = open("images/otherpacmanportalmaze.txt", "r") contents = file.read() line = '' all_lines = [] for chars in contents: if chars != '\n': line += chars else: all_lines.append(line) line = '' i = 0 j = 0 for rows in all_lines: for chars in rows: if chars == 'X': new = Blocks(screen) new.rect.x, new.rect.y = 13 * i, 13 * j blocks.add(new) elif chars == 'd': thepowerpill = Powerpills(screen) thepowerpill.rect.x, thepowerpill.rect.y = 13 * i, 13 * j powerpills.add(thepowerpill) elif chars == 'o': theshield = Shield(screen) theshield.rect.x, theshield.rect.y = 13 * i, 13 * j shield.add(theshield) # This is where the horizontal portal is supposed to be elif chars == 'h': pass # Vertical portal? elif chars == 'P': theportal = Portal(screen) theportal.rect.x, theportal.rect.y = 13 * i, 13 * j portal.add(theportal) pass i += 1 i = 0 j += 1
def create_shields(cls, *groups): """Create the shields which block bullets. Arguments: groups (pygame.sprite.Group): The groups the shields will be in. Returns: list [Shield]: The shields that were created. """ shields = [] shield_gap = int((DISPLAY.width - NUM_SHIELDS * SHIELD.width) / (NUM_SHIELDS + 1)) start = shield_gap end = DISPLAY.width - SHIELD.width step = SHIELD.width + shield_gap for pos_x in range(start, end, step): shields.append(Shield((pos_x, SHIELD_HEIGHT), *groups)) return shields
def readFile(screen, blocks, shield, pellets, powerpellets): file = open("maze.txt", "r") contents = file.read() line = '' all_lines = [] for chars in contents: if chars != '\n': line += chars else: all_lines.append(line) line = '' i = 0 j = 0 for rows in all_lines: for chars in rows: if chars == 'X': new = Bricks(screen) new.rect.x, new.rect.y = 13 * i, 13 * j blocks.add(new) elif chars == 'P': thepowerpellets = PowerPellets(screen) thepowerpellets.rect.x, thepowerpellets.rect.y = 13 * i, 13 * j powerpellets.add(thepowerpellets) elif chars == '-': theshield = Shield(screen) theshield.rect.x, theshield.rect.y = 13 * i, 13 * j shield.add(theshield) elif chars == 'o': thepellets = Pellets(screen) thepellets.rect.x, thepellets.rect.y = 13 * i, 13 * j pellets.add(thepellets) elif chars == 'T': pass i += 1 i = 0 j += 1
def create_shield(ai_settings, screen, stats, shield): """Add one shield to group Shield""" if (stats.alien_kills % ai_settings.shield_drop_point == 0): if (len(shield) < 1): new_shield = Shield(ai_settings, screen) shield.add(new_shield)
def __init__(self): ##THESE LINES JUST HOLD HARDCODED LISTS AND DICTS SO TO FIND OTHER ATTRS SCROLL DOWN FOR THE NEXT UNINDENTED COMMENT #list of classes with ships in them for the player to play #use the players faction as a DICT key to view the list of playable ships self.playerShips = { "UFP": [ #Could add the warship voyager Ship("Enterprise A", "UFP", 1000, Shield("Standard Issue Shield", 150, 12), [ Weapon("Standard Phasers", 100, "Energy"), Weapon("Photon Torpedo", 150, "Antimatter") ], 100), Ship("Enterprise B", "UFP", 1400, Shield("Standard Issue Shield", 300, 24), [ Weapon("Standard Phasers", 140, "Energy"), Weapon("Photon Torpedo", 190, "Antimatter") ], 125), Ship("Enterprise C", "UFP", 1750, Shield("Standard Issue Shield", 450, 36), [ Weapon("Standard Phasers", 180, "Energy"), Weapon("Photon Torpedo", 230, "Antimatter") ], 200), Ship("Enterprise D", "UFP", 2000, Shield("Standard Issue Shield", 600, 48), [ Weapon("Standard Phasers", 215, "Energy"), Weapon("Photon Torpedo", 255, "Antimatter") ], 220), Ship("Enterprise E", "UFP", 3200, Shield("Standard Issue Shield", 900, 60), [ Weapon("Standard Phasers", 280, "Energy"), Weapon("Photon Torpedo", 320, "Antimatter"), Weapon("Quantum Torpedo", 570, "Special") ], 260), Ship("Voyager", "UFP", 2200, Shield("Standard Issues Shield", 650, 32), [ Weapon("Standard Phasers", 245, "Energy"), Weapon("Photon Torpedo", 260, "Antimatter") ], 200), Ship("Defiant", "UFP", 3000, Shield("Standard Issues Shield", 900, 60), [ Weapon("Pulse Phasers", 250, "Energy"), Weapon("Photon Torpedo", 320, "Antimatter"), Weapon("Quantum Torpedo", 570, "Special") ], 200), Ship("Polaris", "UFP", 4000, Shield("Standard Issues Shield", 1100, 60), [ Weapon("Standard Phasers", 280, "Energy"), Weapon("Photon Torpedo", 350, "Antimatter"), Weapon("Quantum Torpedo", 630, "Special") ], 250) ], "Cardassian": [ Ship("Galor Class", "Cardassian", 1800, Shield("Standard Shield", 550, 22), [ Weapon("Phaser Array", 200, "Energy"), Weapon("Antimatter Torpedo", 245, "Antimatter") ], 200), Ship("Hideki Class", "Cardassian", 1400, Shield("Standard Shield", 250, 15), [ Weapon("Phaser Array", 150, "Energy"), Weapon("Antimatter Torpedo", 230, "Antimatter") ], 140), Ship("Kutet Class", "Cardassian", 2200, Shield("Standard Shield", 900, 30), [ Weapon("Phasr Array", 340, "Energy"), Weapon("Antimatter Torpedo", 270, "Antimatter") ], 250), ], "Klingon": [ Ship("B'rel", "Klingon", 1200, Shield("Standard Shield", 250, 26), [ Weapon("Disruptors", 135, "Energy"), Weapon("Photon Torpedo", 245, "Antimatter") ], 100), Ship("Vor'cha", "Klingon", 1600, Shield("Standard Shield", 400, 26), [ Weapon("Disruptors", 180, "Energy"), Weapon("Photon Torpedo", 300, "Antimatter") ], 150), Ship("K'vort", "Klingon", 2000, Shield("Standard Shield", 550, 32), [ Weapon("Disruptors", 215, "Energy"), Weapon("Photon Torpedo", 330, "Antimatter") ], 175), Ship("Negh'Var", "Klingon", 2400, Shield("Standard Shield", 700, 38), [ Weapon("Disruptors", 260, "Energy"), Weapon("Photon Torpedo", 360, "Antimatter") ], 200), Ship("Sword of Kahless", "Klingon", 3000, Shield("Standard Shield", 900, 50), [ Weapon("Disruptors", 300, "Energy"), Weapon("Photon Torpedo", 400, "Antimatter") ], 250) ], "Romulan": [ Ship("D7 Class", "Romulan", 1250, Shield("Standard Shield", 300, 20), [ Weapon("Tri-Phasic Emitters", 180, "Energy"), Weapon("Plasma Torpedo", 245, "Plasma") ], 110), Ship("Valdore", "Romulan", 2000, Shield("Standard Shield", 500, 38), [ Weapon("Tri-Phasic Emitters", 225, "Energy"), Weapon("Plasma Torpedo", 300, "Plasma") ], 250), Ship("D'deridex", "Romulan", 2500, Shield("Standard Shield", 800, 56), [ Weapon("Tri-Phasic Emitters", 270, "Energy"), Weapon("Plasma Torpedo", 360, "Plasma") ], 250), Ship("Scimitar", "Romulan", 3600, Shield("Standard Shield", 1100, 73), [ Weapon("Tri-Phasic Emitters", 330, "Energy"), Weapon("Plasma Torpedo", 400, "Plasma"), Weapon("Thalaron Pulse", 700, "Special") ], 250) ], "Dominion": [ Ship("Jem'Hadar Cruiser", "Dominion", 1250, Shield("Polaron Shield", 300, 20), [ Weapon("Phased Polaron Beam", 180, "Polaron"), Weapon("Polaron Torpedo", 245, "Polaron") ], 110), Ship("Chimera Battlecruiser", "Dominion", 2200, Shield("Polaron Shield", 700, 30), [ Weapon("Phased Polaron Beam", 250, "Polaron"), Weapon("Polaron Torpedo", 260, "Polaron") ], 110), Ship("Hydra Dreadnought", "Dominion", 3000, Shield("Polaron Shield", 700, 40), [ Weapon("Phased Polaron Beam", 320, "Polaron"), Weapon("Polaron Torpedo", 280, "Polaron") ], 110) ], "Borg": [ Ship("Borg Sphere", "Borg", 2500, Shield("No Shield", 0, 0), [ Weapon("Borg Cutting Beam", 300, "Energy"), Weapon("Shield Drain Torpedo", 400, "Drain") ], 100), Ship("Borg Cube", "Borg", 5000, Shield("No Shield", 0, 0), [ Weapon("Borg Cutting Beam", 700, "Energy"), Weapon("Shield Drain Torpedo", 800, "Drain") ], 150), Ship("Borg Diamond", "Borg", 3300, Shield("No Shield", 0, 0), [ Weapon("Borg Cutting Beam", 500, "Energy"), Weapon("Shield Drain Torpedo", 600, "Drain") ], 125), Ship("Progenitor", "Borg", 92000, Shield("No Shield", 0, 0), [ Weapon("Borg Cutting Beam", 20000, "Energy"), Weapon("Ragnarok Torpedo", 50000, "Dark Matter"), Weapon("Reaper Beam", 90000, "Polaron") ], 500) ] #Reaper beam is not actually Polaron based, just needs it here to bypass shields } #The Keys in this list also double up as the playable factions #as well as the keys in playerShips self.alliances = { "UFP": "UFP;Bajoran;", "Klingon": "Klingon;", "Cardassian": "Cardassian;Dominion;", "Dominion": "Dominion;Cardassian;Breen;", "Romulan": "", "Borg": "" } #These will be possible factions for other ships self.factions = [ "UFP", "Klingon", "Bajoran", "Cardassian", "Dominion", "Breen", "Hirogen", "Borg", "Romulan", "Orion Pirates", "Nausicaans", "Aaamazzarite", "Acamarian", "Aenar", "Akritirian", "Allasomorph", "Antaran", "Berellian", "Ferengi", "Horta", "Kazon", "Ocampa", "Species 8472", "Tholian", "Tellarite", "Viidians" ] ## THIS IS WHERE OTHER ATTRS WILL BE STORED SUCH AS THE PLAYER AND MAP OBJECTS self.player = self.setupPlayer() # sets up the player object self.setupGame() # sets up the game difficulty and creates the gameMap self.lastAction = None # a variable to hold the last action the player performed self.activeShips = { } #holds dict of active Enemy objects as {name STR:OBJ(Enemy)} self.enemyThreads = []
def car(learning_method, number_of_rollouts, simulation_steps, learning_episodes, actor_structure, critic_structure, train_dir, nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100, retrain_nn=False, safe_training=False, shields=1, episode_len=100, penalty_ratio=0.1): # Set up the actual model def f(x, u): return np.matrix([[x[1, 0]], [0.001 * u[0, 0] - 0.0025 * np.cos(3 * x[0, 0])]]) def f_to_str(K): kstr = K_to_str(K) f = [] f.append("x[2]") f.append("0.001*{} - 0.0025 * cos(3 * x[1])".format(kstr[0])) return f def rewardf(x, Q, u, R): return x[0, 0] - 0.6 def testf(x, u): return x[0, 0] < -np.pi / 3 def terminalf(x): return x[0, 0] >= 0.6 x_min = np.array([[-1.2], [-0.007]]) x_max = np.array([[0.7], [0.007]]) s_min = np.array([[-0.5], [0.0]]) s_max = np.array([[-0.5], [0.0]]) u_min = np.array([[-1.0]]) u_max = np.array([[1.0]]) # Set up a linearized model # We'll use splits at -0.1 to 0.1 around each peak (since the dynamics # use cos(3x), the peaks are at multiples of pi / 3). breaks = [-np.pi / 3 - 0.2, -np.pi / 3 + 0.2, -0.2, 0.2, np.pi / 3 - 0.2] break_breaks = [5, 5, 5] mins = [ -np.cos(-np.pi - 0.6), -np.cos(-np.pi + 0.6), -1, -1, np.cos(np.pi - 0.6) ] maxes = [1, 1, -np.cos(-0.6), -np.cos(0.6), 1] lower_As = [] upper_As = [] B = np.array([[0.0], [0.001]]) for i in range(len(breaks) - 1): max_m = (maxes[i + 1] - maxes[i]) / (breaks[i + 1] - breaks[i]) min_m = (mins[i + 1] - mins[i]) / (breaks[i + 1] - breaks[i]) # lA * x + B * u <= x' <= uA * x + B * u lower_As.append(np.matrix([[0.0, 1.0], [0.0025 * min_m, 0.0]])) upper_As.append(np.matrix([[0.0, 1.0], [0.0025 * max_m, 0.0]])) Bs = [B] * len(lower_As) # We consider unsafe behavior to be moving over the left side of the hill uA = np.matrix([[1.0, 0.0]]) ub = np.matrix([[-np.pi / 3]]) env = PolySysEnvironment(f, f_to_str, rewardf, testf, None, 2, 1, None, None, s_min, s_max, x_min=x_min, x_max=x_max, u_min=u_min, u_max=u_max, timestep=1.0, terminalf=terminalf, unsafe_A=uA, unsafe_b=ub, approx=True, breaks=breaks, break_breaks=break_breaks, lower_As=lower_As, lower_Bs=Bs, upper_As=upper_As, upper_Bs=Bs) if retrain_nn: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': episode_len, 'max_episodes': learning_episodes, 'minibatch_size': 64, 'random_seed': 6554, # 6553 'tau': 0.005, 'model_path': train_dir + "retrained_model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 500 } else: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': episode_len, 'max_episodes': 0, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir + "model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 500 } Ks = [np.matrix([[0.0, 0.0]])] invs = [(np.matrix([[1, 0], [-1, 0], [0, 1], [0, -1]]), np.matrix([[0.8], [0.8], [0.07], [0.07]]))] covers = [(invs[0][0], invs[0][1], np.matrix([[-0.8], [-0.07]]), np.matrix([[0.8], [0.07]]))] bound = 30 initial_shield = Shield(env, K_list=Ks, inv_list=invs, cover_list=covers, bound=bound) actor, shield = DDPG(env, args, safe_training=safe_training, shields=shields, initial_shield=initial_shield, penalty_ratio=penalty_ratio) if shield_test: shield.test_shield(actor, test_episodes, 5000) actor.sess.close()
def obstacle(learning_method, number_of_rollouts, simulation_steps, learning_episodes, actor_structure, critic_structure, train_dir, nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100, retrain_nn=False, safe_training=False, shields=1, episode_len=100, penalty_ratio=0.1): A = 0.2 * np.matrix([[0.0, 0.0, 10.0, 0.0, 0.0], [0.0, 0.0, 0.0, 10.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]]) B = 0.5 * np.matrix([[0.0, 0.0], [0.0, 0.0], [10.0, 0.0], [0.0, 10.0], [0.0, 0.0]]) #s_min = np.array([[-0.1], [-0.1], [-0.1], [-0.1], [1]]) #s_max = np.array([[0.1], [0.1], [0.1], [0.1], [1]]) s_min = np.array([[0.0], [0.0], [0.0], [0.0], [1.0]]) s_max = np.array([[0.0], [0.0], [0.0], [0.0], [1.0]]) x_goal = 3.0 y_goal = 0.0 x_min = np.array([[-10.0], [-10.0], [-20.0], [-20.0], [0.0]]) x_max = np.array([[ 10.0], [ 10.0], [ 20.0], [ 20.0], [2.0]]) def rewardf(x, u): return -(abs(x[0,0] - x_goal) + abs(x[1,0] - y_goal)) def terminalf(x): return x[0,0] >= x_goal and x[1,0] >= y_goal u_min = np.array([[-2.0], [-2.0]]) u_max = np.array([[ 5.0], [ 5.0]]) # There is a box at [0, 2] to [1, 3] which is unsafe unsafe_A = [np.matrix([[ 1.0, 0.0, 0.0, 0.0, 0.0], [-1.0, 0.0, 0.0, 0.0, 0.0], [ 0.0, 1.0, 0.0, 0.0, 0.0], [ 0.0, -1.0, 0.0, 0.0, 0.0]])] unsafe_b = [np.matrix([[1.0], [0.0], [3.0], [-2.0]])] env = Environment(A, B, u_min, u_max, s_min, s_max, x_min, x_max, None, None, continuous=True, rewardf=rewardf, terminalf=terminalf, unsafe_A=unsafe_A, unsafe_b=unsafe_b) if retrain_nn: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': episode_len, 'max_episodes': learning_episodes, # originally 1000 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir+"retrained_model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 5000} else: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': episode_len, 'max_episodes': learning_episodes, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir+"model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 5000} Ks = [np.matrix([[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]]), np.matrix([[0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0]])] invs = [(np.matrix([[-1.0, 0.0, 0.0, 0.0, 0.0], [ 0.0, 0.0, -1.0, 0.0, 0.0]]), np.matrix([[-1.5], [1.25]])), (np.matrix([[0.0, 1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0, 0.0]]), np.matrix([[1.5], [1.25]]))] covers = [(invs[0][0], invs[0][1], np.matrix([[1.5, -0.5, -0.3, -0.3, 1.0]]), np.matrix([[x_goal + 0.5, y_goal + 0.5, 2.0, 2.0, 1.0]])), (invs[1][0], invs[1][1], np.matrix([[-0.5, -0.5, -0.3, -0.3, 1.0]]), np.matrix([[x_goal + 0.1, 0.5, 2.0, 1.0, 1.0]]))] bound = 8 initial_shield = Shield(env, K_list=Ks, inv_list=invs, cover_list=covers, bound=bound) actor, shield = DDPG(env, args, safe_training=safe_training, shields=shields, initial_shield=initial_shield, penalty_ratio=penalty_ratio, bound=bound) if shield_test: shield.test_shield(actor, test_episodes, 5000) actor.sess.close()
alf = Enemy("{} ship".format(factionChoice),factionChoice,hull,s,[w1,w2]) return alf #----------TESTS----------# #from enemy import Enemy #from ship import Ship, voy ##nx = Enemy("Enterprise NX","UFP",1500,voy.shield,voy.weapons) ## weyWep1 = Weapon("Phased Polaron Beam",213,"Polaron") weyWep2 = Weapon("Polaron Torpedo",297,"Polaron") weyShield = Shield("Polaron Shield",1000,12) weyoun = Enemy("Hydra Battlecruiser","Dominion",4000,weyShield,[weyWep1,weyWep2]) ## ## ##def fightNX(): ## voy.raiseShields() ## while voy.alive and nx.alive: ## choice = random.randint(0,1) ## nx.receiveDamage(voy.weapons[choice]) ## voy.receiveDamage(nx.weapons[choice]) ## nx.updateShields() ## ## if voy.alive: ## print("Voy won") ## else: ## print("NX won")
def biology (learning_method, number_of_rollouts, simulation_steps, learning_eposides, critic_structure, actor_structure, train_dir,\ nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100, retrain_nn=False): # 10-dimension and 1-input system and 1-disturbance system ds = 3 us = 2 #Dynamics that are defined as a continuous function! def f (x, u): #random disturbance #d = random.uniform(0, 20) delta = np.zeros((ds, 1), float) delta[0,0] = -0.01*x[0,0] - x[1,0]*(x[0,0]+4.5) + u[0,0] delta[1,0] = -0.025*x[1,0] + 0.000013*x[2,0] delta[2,0] = -0.093*(x[2,0] + 15) + (1/12)*u[1,0] return delta #Closed loop system dynamics to text def f_to_str(K): kstr = K_to_str(K) f = [] f.append("-0.01*x[1] - x[2]*(x[1]+4.5) + {}".format(kstr[0])) f.append("-0.025*x[2] + 0.000013*x[3]") f.append("-0.093*(x[3] + 15) + (1/12)*{}".format(kstr[1])) return f h = 0.01 # amount of Gaussian noise in dynamics eq_err = 1e-2 #intial state space s_min = np.array([[-2],[-0],[-0.1]]) s_max = np.array([[ 2],[ 0],[ 0.1]]) Q = np.zeros((ds,ds), float) R = np.zeros((us,us), float) np.fill_diagonal(Q, 1) np.fill_diagonal(R, 1) #user defined unsafety condition def unsafe_eval(x): if (x[0,0] >= 5): return True return False def unsafe_string(): return ["x[1] - 5"] def rewardf(x, Q, u, R): reward = 0 reward += -np.dot(x.T,Q.dot(x))-np.dot(u.T,R.dot(u)) if (unsafe_eval(x)): reward -= 100 return reward def testf(x, u): if (unsafe_eval(x)): print x return -1 return 0 u_min = np.array([[-50.], [-50]]) u_max = np.array([[ 50.], [ 50]]) env = PolySysEnvironment(f, f_to_str,rewardf, testf, unsafe_string, ds, us, Q, R, s_min, s_max, u_max=u_max, u_min=u_min, timestep=h) ############ Train and Test NN model ############ if retrain_nn: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 100, 'max_episodes': 1000, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir+"retrained_model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 1000} else: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 100, 'max_episodes': learning_eposides, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir+"model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 1000} actor = DDPG(env, args=args) #################### Shield ################# model_path = os.path.split(args['model_path'])[0]+'/' linear_func_model_name = 'K.model' model_path = model_path+linear_func_model_name+'.npy' shield = Shield(env, actor, model_path=model_path, force_learning=retrain_shield) shield.train_polysys_shield(learning_method, number_of_rollouts, simulation_steps, eq_err=eq_err, explore_mag = 0.4, step_size = 0.5, aggressive=True, without_nn_guide=True, enable_jit=True) if shield_test: shield.test_shield(test_episodes, 1000, mode="single") actor.sess.close()
class Game(object): def __init__(self): ## sets dimension of the screen and the background pygame.init() self.w = 900 self.h = 600 self.screen = pygame.display.set_mode((self.w, self.h), pygame.FULLSCREEN) self.background = pygame.image.load("background.png") ## creates all the objects and lists needed for the game self.player = Player(self.w, self.h, self.background) self.shooter = Shooter(self.player.x, self.player.y, self.player.d) self.shield = Shield(self.player.x, self.player.y, self.player.d) self.generator = Generator(self.player.x, self.player.y, self.player.d) self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation) self.enemies = [Enemy(self.w, self.h)] self.enemies_2 = [] self.counter_enemies_2_dead = 0 self.collectible = Collectible(self.w, self.h) self.ray = Ray(self.w, self.h) ## loads energy image and sets its default value self.energy_5 = pygame.image.load("energy_5.png") self.energy = 100 ## sets all default values self.points = 0 self.killed_big_enemies = 0 self.killed_small_enemies = 0 self.rays_collected = 0 self.collectibles_collected = 0 ## initializes fonts and creates the first text font = pygame.font.SysFont("comic", 64) self.font = pygame.font.SysFont("arial", 10) text = font.render("Click to play", True, (255, 255, 255)) ## loads sound self.sound = pygame.mixer.Sound("blip.wav") ## sets timer and default timer variables self.clock = pygame.time.Clock() self.timer = 0 self.seconds = 0 self.minutes = 0 ## manages beginning screen and first inputs beginning = True while beginning: self.screen.fill((0, 0, 0)) self.screen.blit(text, ((self.w / 2) - 150, (self.h / 2) - 64)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.display.quit() if event.type == pygame.MOUSEBUTTONUP: beginning = False self.screen.blit(self.background, (0, 0)) self.screen.blit(self.player.images[1], (self.player.x, self.player.y)) self.screen.blit(self.shooter.image, (self.shooter.x, self.shooter.y)) self.running() pygame.display.update() def running(self): while True: ## manages time time_passed = self.clock.tick(30) time_seconds = time_passed / 1000.0 self.timer += 1 self.seconds += 0.03 if self.seconds >= 60: self.minutes += 1 self.seconds = 0 ## gets all the inputs and calls function related to each input for event in pygame.event.get(): if event.type == pygame.KEYUP: if event.key == pygame.K_ESCAPE: pygame.display.quit() if event.key == pygame.K_a: self.shield.index -= 1 if self.shield.index == -5: self.shield.index = -1 elif event.key == pygame.K_j: self.shield.index += 1 if self.shield.index == 4: self.shield.index = 0 elif event.key == pygame.K_l: self.generator.index += 1 if self.generator.index == 4: self.generator.index = 0 elif event.key == pygame.K_s: self.generator.index -= 1 if self.generator.index == -5: self.generator.index = -1 key = pygame.key.get_pressed() if key[pygame.K_UP]: self.player.moves_up(time_seconds) self.player.index = 0 if key[pygame.K_DOWN]: self.player.moves_down(time_seconds) self.player.index = 1 if key[pygame.K_LEFT]: self.player.moves_left(time_seconds) self.player.index = 2 if key[pygame.K_RIGHT]: self.player.moves_right(time_seconds) self.player.index = 3 ## blits the background self.screen.blit(self.background, (0, 0)) ## manages energy and prints it if self.energy >= 100: self.energy = 100 if self.energy <= 10: self.sound.play() if self.energy <= 0: self.collectible.finished = True for n in xrange(0, self.energy / 5): self.screen.blit(self.energy_5, (self.w - 35 - 10 * n, 4)) ## manages the text (points and time) text = self.font.render((str(self.minutes) + ":" + str(self.seconds)), True, (255, 255, 255)) self.screen.blit(text, (10, 10)) text = self.font.render(("Points: " + str(self.points)), True, (255, 255, 255)) self.screen.blit(text, (440, 10)) ## manages collectibles self.collectible.blit_check(self.background, self.timer) if self.collectible.blit: self.screen.blit(self.collectible.image, (self.collectible.x, self.collectible.y)) self.collectible_rect = pygame.Rect(self.collectible.x, self.collectible.y, 20, 20) ## manages player and collision with collectible self.screen.blit(self.player.images[self.player.index], (self.player.x, self.player.y)) self.player_rect = pygame.Rect(self.player.x, self.player.y, 40, 40) if self.collectible.blit: if self.player_rect.colliderect(self.collectible_rect): i = self.collectible.index self.collectible = Collectible(self.w, self.h) self.collectible.index = i self.points += 100 self.collectibles_collected += 1 ## manages bullet, checks hits with walls self.bullet.moves(self.shooter.x, self.shooter.y) if self.bullet.rotate: self.bullet.rotates(time_seconds) self.screen.blit(self.bullet.rotated_image, self.bullet.image_draw_pos) self.bullet.check_shot() if self.bullet.shot: self.energy -= 5 if self.bullet.shot: self.screen.blit(self.bullet.rotated_image, (self.bullet.x, self.bullet.y)) self.bullet.hits_borders(self.background) if self.bullet.hits_bg: self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation) self.bullet_rect = pygame.Rect(self.bullet.x, self.bullet.y, 4, 10) ## manages generator self.generator.moves(self.player.x, self.player.y, self.player.d) self.screen.blit(self.generator.images[self.generator.index], (self.generator.x, self.generator.y)) generator_rect = pygame.Rect(self.generator.x, self.generator.y, self.generator.w, self.generator.h) ## manages shooter self.shooter.moves(self.player.x, self.player.y, self.player.d) self.shooter.rotates(time_seconds) self.screen.blit(self.shooter.rotated_image, self.shooter.image_draw_pos) ## manages shield self.shield.moves(self.player.x, self.player.y, self.player.d) self.screen.blit(self.shield.images[self.shield.index], (self.shield.x, self.shield.y)) shield_rect = pygame.Rect(self.shield.x, self.shield.y, self.shield.w, self.shield.h) ## manages big enemies one by one, checks collisions with bullets, shield and tank for n in xrange(0, len(self.enemies)): enemy = self.enemies[n] enemy.moves(time_seconds) enemy.bounces(self.background) if enemy.error: self.enemies[n] = Enemy(self.w, self.h) self.screen.blit(enemy.image, (enemy.x, enemy.y)) enemy_rect = pygame.Rect(enemy.x, enemy.y, enemy.d, enemy.d) if enemy_rect.colliderect(self.bullet_rect): self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation) self.enemies_2.append(Enemy_2(enemy.x, enemy.y)) self.enemies_2.append(Enemy_2(enemy.x, enemy.y)) self.enemies_2.append(Enemy_2(enemy.x, enemy.y)) enemy.alive = False self.points += 10 self.killed_big_enemies += 1 elif enemy_rect.colliderect(shield_rect): enemy.reverse() self.energy -= 5 elif enemy_rect.colliderect(self.player_rect): enemy.reverse() self.energy -= 10 if (self.timer == 30 * 5 or self.timer == 30 * 10) and len(self.enemies) <= 2: self.enemies.append(Enemy(self.w, self.h)) ## temporary list to manage the elimination of some enemies l = [] for n in xrange(0, len(self.enemies)): enemy = self.enemies[n] if enemy.alive: l.append(self.enemies[n]) self.enemies = l ## manages small enemies one by one, checks collision with bullets, shield and tank for n in xrange(0, len(self.enemies_2)): enemy_2 = self.enemies_2[n] enemy_2.moves(time_seconds) enemy_2.bounces(self.background) if enemy.error: self.enemies_2[n] = Enemy_2(self.w, self.h) self.screen.blit(enemy_2.image, (enemy_2.x, enemy_2.y)) enemy_2_rect = pygame.Rect(enemy_2.x, enemy_2.y, enemy_2.d, enemy_2.d) if enemy_2_rect.colliderect(self.player_rect): enemy_2.reverse() self.energy -= 10 elif enemy_2_rect.colliderect(shield_rect): enemy_2.reverse() self.energy -= 5 elif enemy_2_rect.colliderect(self.bullet_rect): self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation) enemy_2.alive = False self.counter_enemies_2_dead += 1 self.points += 10 self.killed_small_enemies += 1 ## temporary list to manage the elimination of some enemies l = [] for n in xrange(0, len(self.enemies_2)): enemy_2 = self.enemies_2[n] if enemy_2.alive: l.append(self.enemies_2[n]) self.enemies_2 = l if self.counter_enemies_2_dead == 3: self.counter_enemies_2_dead = 0 self.enemies.append(Enemy(self.w, self.h)) ## manages rays of energy and collision with generator and tank and manages life time self.ray.moves(time_seconds) self.ray.bounces(self.background) self.screen.blit(self.ray.image, (self.ray.x, self.ray.y)) ray_rect = pygame.Rect(self.ray.x, self.ray.y, self.ray.d, self.ray.d) if ray_rect.colliderect(generator_rect): self.ray.check_caught() if self.ray.caught: self.ray = Ray(self.w, self.h) self.energy += 20 self.points += 10 self.rays_collected += 1 else: self.ray.reverse() if ray_rect.colliderect(self.player_rect): self.ray.reverse() if ( self.timer >= 30 * 15 and self.timer <= 30 * 15.01 or self.timer >= 30 * 30 and self.timer <= 30 * 30.01 or self.timer >= 30 * 45 and self.timer <= 30 * 45.01 or self.timer >= 30 * 60 and self.timer <= 30 * 60.01 or self.timer >= 30 * 75 and self.timer <= 30 * 75.01 or self.timer >= 30 * 90 and self.timer <= 30 * 90.01 or self.timer >= 30 * 105 and self.timer <= 30 * 105.01 or self.timer >= 30 * 120 and self.timer <= 30 * 120.01 ): self.ray = Ray(self.w, self.h) ## manages the end of the loop if self.collectible.finished == True: pygame.display.quit() self.end() pygame.display.update() def end(self): ## creates a whole new screen, calculates points and blits results pygame.init() screen = pygame.display.set_mode((600, 300), 0, 32) screen.fill((0, 0, 0)) points = self.seconds + (self.minutes * 60) + self.energy + self.points if ( self.killed_big_enemies >= 3 and self.killed_small_enemies >= 3 and self.rays_collected >= 3 and self.collectibles_collected >= 4 ): msg = "You did great!!!" else: msg = "You could have done better! ;)" end = True while end: font = pygame.font.SysFont("comic", 40) text = font.render(("Total points: " + str(points)), True, (255, 255, 255)) text1 = font.render(msg, True, (255, 255, 255)) screen.blit(text, (100, 100)) screen.blit(text1, (100, 200)) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.display.quit() end = False pygame.display.update()
def lanekeep (learning_method, number_of_rollouts, simulation_steps, learning_eposides, actor_structure, critic_structure, train_dir,\ nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100, retrain_nn=False): v0 = 27.7 cf = 133000 cr = 98800 M = 1650 b = 1.59 a = 1.11 Iz = 2315.3 ds = 4 us = 2 disturbance_x_min = np.array([[0],[0],[-0.035],[0]]) disturbance_x_max = np.array([[0],[0],[ 0.035],[0]]) #Dynamics that are defined as a continuous function! def f (x, u): rd = random.uniform(-0.6, 0.6) delta = np.zeros((ds, 1), float) delta[0,0] = 1*x[1,0] + v0*x[2,0] + random.uniform(disturbance_x_min[0], disturbance_x_max[0]) #lateral displacement delta[1,0] = (-1*(cf+cr)/(M*v0))*x[1,0] + ((b*cr-a*cf)/(M*v0)-v0)*x[3,0] + (cf/M)*u[0,0] + random.uniform(disturbance_x_min[1], disturbance_x_max[1]) #lateral velocity delta[2,0] = x[3,0] + random.uniform(disturbance_x_min[2], disturbance_x_max[2]) #error yaw angle delta[3,0] = ((b*cr-a*cf)/(Iz*v0))*x[1,0] + (-1*(a*a*cf + b*b*cr)/(Iz*v0))*x[3,0] + (a*cf/Iz)*u[1,0] + random.uniform(disturbance_x_min[3], disturbance_x_max[3]) #yaw rate return delta #Closed loop system dynamics to text def f_to_str(K): kstr = K_to_str(K) f = [] f.append("1*x[2] + 27.7*x[3] + d[1]") f.append("(-1*(133000+98800)/(1650*27.7))*x[2] + ((1.59*98800-1.11*133000)/(1650*27.7)-27.7)*x[4] + (133000/1650)*{} + d[2]".format(kstr[0])) f.append("x[4] + d[3]") f.append("((1.59*98800-1.11*133000)/(2315.3*27.7))*x[2] + (-1*(1.11*1.11*133000 + 1.59*1.59*98800)/(2315.3*27.7))*x[4] + (1.11*133000/2315.3)*{} + d[4]".format(kstr[1])) return f h = 0.01 # amount of Gaussian noise in dynamics eq_err = 1e-2 #intial state space s_min = np.array([[ -0.1],[ -0.1], [-0.1], [ -0.1]]) s_max = np.array([[ 0.1],[ 0.1], [ 0.1], [ 0.1]]) Q = np.matrix("1 0 0 0; 0 1 0 0 ; 0 0 1 0; 0 0 0 1") R = np.matrix(".0005 0; 0 .0005") #user defined unsafety condition def unsafe_eval(x): if (x[0,0] > 0.9 or x[0, 0] < -0.9): # keep a safe distance from the car in front of you return True return False def unsafe_string(): return ["-(x[1]- -0.9)*(0.9-x[1])"] def rewardf(x, Q, u, R): reward = 0 reward += -np.dot(x.T,Q.dot(x))-np.dot(u.T,R.dot(u)) if (unsafe_eval(x)): reward -= 1e-3 return reward def testf(x, u): if (unsafe_eval(x)): return -1 return 0 # Use sheild to directly learn a linear controller u_min = np.array([[-1]]) u_max = np.array([[1]]) env = PolySysEnvironment(f, f_to_str,rewardf, testf, unsafe_string, ds, us, Q, R, s_min, s_max, u_max=u_max, u_min = u_min, disturbance_x_min=disturbance_x_min, disturbance_x_max=disturbance_x_max, timestep=h) if retrain_nn: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 1000, 'max_episodes': 1000, 'minibatch_size': 64, 'random_seed': 2903, 'tau': 0.005, 'model_path': train_dir+"retrained_model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 1000} else: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 1000, 'max_episodes': learning_eposides, 'minibatch_size': 64, 'random_seed': 2903, 'tau': 0.005, 'model_path': train_dir+"model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 1000} actor = DDPG(env, args) model_path = os.path.split(args['model_path'])[0]+'/' linear_func_model_name = 'K.model' model_path = model_path+linear_func_model_name+'.npy' shield = Shield(env, actor, model_path=model_path, force_learning=retrain_shield) shield.train_polysys_shield(learning_method, number_of_rollouts, simulation_steps, eq_err=eq_err, explore_mag=0.4, step_size=0.5, without_nn_guide=True, aggressive=True) if shield_test: shield.test_shield(test_episodes, 1000, mode="single")
from shield import Shield from config import Time from bullet import Mandabullet, Dragonbullet import time import math board = Board(40, 500) scene = Scene(board) scene.make(board) manda = Manda(3, 35, board, scene) dragon = Dragon(485, 33, board, scene) manda.place_on_map() stime = time.time() mbullet = Mandabullet(board, scene, dragon) dbullet = Dragonbullet(board, scene, manda) timec = Time((board.get_width() - 170) / 2, 2, 0.5) shield = Shield() etime = 0 def gameover(message): os.system('clear') print("Score:" + str(manda.get_coins())) print(message) quit() def movemanda(): def alarmhandler(signum, frame): raise AlarmException def user_input(timeout=0.05):
def pendulum(learning_eposides, critic_structure, actor_structure, train_dir, learning_method, number_of_rollouts, simulation_steps,\ nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100, retrain_nn=False): m = 1.17 l = 1. g = 10. #Dynamics that are continuous A = np.matrix([[0., 1.], [g / l, 0.]]) B = np.matrix([[0.], [1. / (m * l**2.)]]) #intial state space s_min = np.array([[-0.35], [-0.35]]) s_max = np.array([[0.35], [0.35]]) #reward function Q = np.matrix([[1., 0.], [0., 1.]]) R = np.matrix([[.005]]) #safety constraint x_min = np.array([[-0.5], [-0.5]]) x_max = np.array([[0.5], [0.5]]) u_min = np.array([[-15.]]) u_max = np.array([[15.]]) env = Environment(A, B, u_min, u_max, s_min, s_max, x_min, x_max, Q, R, continuous=True) if retrain_nn: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 500, 'max_episodes': 1000, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir + "retrained_model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 3800 } else: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 500, 'max_episodes': learning_eposides, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir + "model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 3800 } actor = DDPG(env, args) #################### Shield ################# model_path = os.path.split(args['model_path'])[0] + '/' linear_func_model_name = 'K.model' model_path = model_path + linear_func_model_name + '.npy' def rewardf(x, Q, u, R): return np.matrix([[env.reward(x, u)]]) shield = Shield(env, actor, model_path, force_learning=retrain_shield, debug=False) shield.train_shield(learning_method, number_of_rollouts, simulation_steps, rewardf=rewardf, eq_err=1e-2, explore_mag=0.3, step_size=0.3) if shield_test: shield.test_shield(test_episodes, 3800, mode="single") actor.sess.close()
def magneticpointer (learning_method, number_of_rollouts, simulation_steps,learning_eposides, critic_structure, actor_structure, train_dir,\ nn_test=False, retrain_shield=False, shield_test=False, test_episodes=100, retrain_nn=False): A = np.matrix([[2.6629, -1.1644, 0.66598], [2, 0, 0], [0, 0.5, 0]]) B = np.matrix([[0.25], [0], [0]]) #intial state space s_min = np.array([[-1.0], [-1.0], [-1.0]]) s_max = np.array([[1.0], [1.0], [1.0]]) Q = np.matrix("1 0 0 ; 0 1 0; 0 0 1") R = np.matrix("1") x_min = np.array([[-3.5], [-3.5], [-3.5]]) x_max = np.array([[3.5], [3.5], [3.5]]) u_min = np.array([[-15.]]) u_max = np.array([[15.]]) env = Environment(A, B, u_min, u_max, s_min, s_max, x_min, x_max, Q, R) if retrain_nn: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 100, 'max_episodes': 1000, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir + "retrained_model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 1000 } else: args = { 'actor_lr': 0.0001, 'critic_lr': 0.001, 'actor_structure': actor_structure, 'critic_structure': critic_structure, 'buffer_size': 1000000, 'gamma': 0.99, 'max_episode_len': 100, 'max_episodes': learning_eposides, 'minibatch_size': 64, 'random_seed': 6553, 'tau': 0.005, 'model_path': train_dir + "model.chkp", 'enable_test': nn_test, 'test_episodes': test_episodes, 'test_episodes_len': 1000 } actor = DDPG(env, args) #################### Shield ################# model_path = os.path.split(args['model_path'])[0] + '/' linear_func_model_name = 'K.model' model_path = model_path + linear_func_model_name + '.npy' def rewardf(x, Q, u, R): return np.matrix([env.reward(x, u)]) shield = Shield(env, actor, model_path, force_learning=retrain_shield, debug=False) shield.train_shield(learning_method, number_of_rollouts, simulation_steps, rewardf=rewardf, eq_err=0, explore_mag=0.04, step_size=0.05, without_nn_guide=True) if shield_test: shield.test_shield(test_episodes, 1000, mode="single")