コード例 #1
0
ファイル: server.py プロジェクト: laranea/Stockex_Model
 def __init__(self, port):
     self.socket = zmq.Context().socket(zmq.REP)
     self.socket.bind("tcp://127.0.0.1:" + str(port))
     self.algo = Algo()
     self.stockHandler = self.algo.sdc
     self.sip = self.algo.sip
     self.should_shutdown = False
コード例 #2
0
def get_production_plan():
    """
        Endpoint of the REST API that accepts a POST with a payload as we can 
        find in the 'example_payloads' directory.
        
        Returns a JSON following the same structure as in 'example_response.json'
        with the optimal 'p' power for each powerplant to obtain the value of 'load'.
    """

    # Get informations from the payload
    load = request.json["load"]
    PowerPlant.wind_percent = request.json["fuels"]["wind(%)"]
    prices_per_MWh = {
        "gasfired": request.json["fuels"]["gas(euro/MWh)"],
        "turbojet": request.json["fuels"]["kerosine(euro/MWh)"],
        "windturbine": 0
    }

    # Instanciate powerplant objects
    powerplants = []
    for powerplant in request.json["powerplants"]:
        pp = PowerPlant.create_pp(**powerplant)
        pp.compute_cost_per_MWh(prices_per_MWh)
        powerplants.append(pp)

    # Calculate the unit-commitment
    algo = Algo(powerplants, load)
    unit_commitment = algo.calc_unit_commitment()

    if unit_commitment:
        return jsonify(unit_commitment)
    return "WARNING: There is no possible configuration for the requested load"
コード例 #3
0
def play(data,thief,police,goals):
    #init graph
    graph = Graph(screen,data,thief,police,goals)

    #init algo
    algo = Algo(data[2])

    #main loop
    running = True
    #limit for num of turns
    limit=20
    #gameover var
    gameover = False
    while running:
        screen.fill(BACKGROUND)

        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                #player select where to move for thief
                if not gameover:
                    turn=graph.choose(event.pos)
                    if turn:
                        #police move base on algo here
                        graph.police_move(algo.move(graph.thief,graph.police))
                        limit-=1
            
            elif event.type == pygame.QUIT:
                running = False
            
        
        #check who win
        result = graph.checkWin()
        if result ==1: #player win
            misc.message_to_screen('Player wins!',(255,0,0),0,0,'large')
            m= misc.button("Restart",850,320,150,150,(55,255,255),(0,255,0))
            gameover = True
            if m:
                return
        elif result ==2: #police win
            misc.message_to_screen('Player Lose...',(0,255,0),0,0,'large')
            m1= misc.button("Restart",850,320,150,150,(55,255,255),(0,255,0))
            gameover = True
            if m1:
                return
        elif limit==0:
            misc.message_to_screen('Its a draw!',(0,0,255),0,0,'large')
            m2= misc.button("Restart",850,320,150,150,(55,255,255),(0,255,0))
            gameover = True
            if m2:
                return
        else:
            #draw game boundary
            pygame.draw.rect(screen,(0,0,0),(50,50,700,700),1)
            #display graph
            graph.display()
            #display turns left
            misc.message_to_screen('Turns left: '+str(limit),(0,0,255),310,-200,'medium')
        pygame.display.update()
コード例 #4
0
    def train(self,
              pw,
              params,
              policy,
              critic,
              policy_loss_file,
              critic_loss_file,
              study_name,
              beta=0) -> None:
        """
        The main function for training and evaluating a policy
        Repeats training and evaluation params.nb_cycles times
        Stores the value and policy losses at each cycle
        When the reward is greater than the best reward so far, saves the corresponding policy
        :param pw: a policy wrapper, used to save the best policy into a file
        :param params: the hyper-parameters of the run, specified in arguments.py or in the command line
        :param policy: the trained policy
        :param critic: the corresponding critic (not always used)
        :param policy_loss_file: the file to record successive policy loss values
        :param critic_loss_file: the file to record successive critic loss values
        :param study_name: the name of the studied gradient algorithm
        :param beta: a specific parameter for beta-parametrized values
        :return: nothing
        """
        for cycle in range(params.nb_cycles):
            batch = self.make_monte_carlo_batch(params.nb_trajs, params.render,
                                                policy)

            # Update the policy
            batch2 = batch.copy_batch()
            algo = Algo(study_name, params.critic_estim_method, policy, critic,
                        params.gamma, beta, params.nstep)
            algo.prepare_batch(batch)
            policy_loss = batch.train_policy_td(policy)

            # Update the critic
            assert params.critic_update_method in [
                'batch', 'dataset'
            ], 'unsupported critic update method'
            if params.critic_update_method == "dataset":
                critic_loss = algo.train_critic_from_dataset(batch2, params)
            elif params.critic_update_method == "batch":
                critic_loss = algo.train_critic_from_batch(batch2)
            critic_loss_file.write(str(cycle) + " " + str(critic_loss) + "\n")
            policy_loss_file.write(str(cycle) + " " + str(policy_loss) + "\n")

            # policy evaluation part
            total_reward = self.evaluate_episode(policy,
                                                 params.deterministic_eval)
            # plot_trajectory(batch2, self.env, cycle+1)

            # save best reward agent (no need for averaging if the policy is deterministic)
            if self.best_reward < total_reward:
                self.best_reward = total_reward
                pw.save(self.best_reward)
コード例 #5
0
    def __init__(self):
        pygame.init()
        super().__init__()

        self.win_w, self.win_h = 1600, 900
        self.win = pygame.display.set_mode((self.win_w, self.win_h),
                                           pygame.RESIZABLE)

        self.algo = Algo(self.win)
        self.update_window_size(0)  ##

        ## Movement flags
        self.moving = False
        self.draw_outer_circle = False
コード例 #6
0
	def maintain(self):
		algo = Algo(self)
		while True:
			if not self.requests.empty() and self.count < self.requests_hold:
				self.count += 1
				request = self.requests.get()
				self.remaining_requests.append(request)
			else:
				break
		print ('requests = ', self.remaining_requests)
		robots_states, goods_states = algo.BFS(self.remaining_requests)
		to_json_file(robots_states, r'../results/robots_states.json')
		to_json_file(goods_states, r'../results/goods_states.json')
		#These features (multithread support) will be developed in the future
		'''
		while True:
			if not self.requests.empty():
				if self.count < self.requests_hold: #Need synchronized
					self.count += 1
					request = self.requests.get()
					#Multi-thread o day?
					bfs_thread = Thread(target = algo.BFS, args = (request))
					bfs_thread.start() 
					self.count -= done_requests
				else:
					#Sleep somethings
					time.sleep(0.1)
			else:
				#Sleep somethings
				break
				time.sleep(0.05)
		'''
		
				
					
					
					
					
					
					
					
					
					
					
		
コード例 #7
0
                    Photo(i, photo_params[2:], photo_params[0]))

    def write_output_file(self, slides, score):
        """
        Writes the output file ./outputs/<file_prefix>_<score>_<timestamp>.out
        
        :param slides: Slides list
        :param score:  The score after the simulation
        """
        output_file_path = "outputs/" + self.file_prefix + "_" + str(
            score) + "_" + str(time.time()) + ".out"
        file = open(output_file_path, "w")
        print("write_output_file : " + output_file_path)

        file.write(str(len(slides)) + "\n")

        for slide in slides:
            file.write(str(slide.photo1.id))
            if slide.photo2 is not None:
                file.write(" " + str(slide.photo2.id))
            file.write("\n")

        file.close()


# initialize Main instance
main = Main(sys.argv[1])

# initialize Algo instance
Algo(main)
コード例 #8
0
    def backtest_algo2(self, number, dates, algo):
        #level number corresponds to an index
        wins = [0] * 11
        losses = [0] * 11

        #creates processes
        num_processes = 16
        start = int(number * (len(dates) / num_processes))
        end = int((number + 1) * (len(dates) / num_processes))

        print()
        print(number)
        print(start)
        print(end)
        print()

        #this is actually a do-while loop
        for x in range(start, end):
            print("At " + str(dates[x]) + " END: " + str(dates[end - 1]))

            games = self.universal.get_games(dates[x])

            for y in range(0, len(games)):

                #team might not exist anymore, so don't count that game
                if len(games[y]['team1']) != 0 and len(games[y]['team2']) != 0:

                    data1 = self.universal.load_data(games[y]['team1'],
                                                     games[y]['date'])
                    data2 = self.universal.load_data(games[y]['team2'],
                                                     games[y]['date'])
                    returned1 = self.odds_calculator.analyze2(
                        games[y]['team1'], games[y]['team2'], data1, "away")
                    returned2 = self.odds_calculator.analyze2(
                        games[y]['team2'], games[y]['team1'], data2, "home")

                    algorithm = Algo(self.league)
                    #sets algo to backtest
                    algorithm.algorithm[self.league] = algo
                    if self.algo_version == "Algo_V1":
                        algo_data = algorithm.calculate(
                            games[y]['date'], returned1, returned2)
                    elif self.algo_version == "Algo_V2":
                        algo_data = algorithm.calculate_V2(
                            games[y]['date'], returned1, returned2)
                    total = algo_data['total']

                    if self.algo_version == "Algo_V1":
                        #categorizes odds
                        levels = []
                        levels.append([0, 3])
                        levels.append([3, 6])
                        levels.append([6, 9])
                        levels.append([9, 12])
                        levels.append([12, 15])
                        levels.append([15, 18])
                        levels.append([18, 21])
                        levels.append([21, 24])
                        levels.append([24, 27])
                        levels.append([27, 100])
                    elif self.algo_version == "Algo_V2":
                        #categorizes odds
                        levels = []
                        levels.append([50, 55])
                        levels.append([55, 60])
                        levels.append([60, 65])
                        levels.append([65, 70])
                        levels.append([70, 75])
                        levels.append([75, 80])
                        levels.append([80, 85])
                        levels.append([85, 90])
                        levels.append([90, 95])
                        levels.append([95, 100])

                    level = 0
                    for z in range(0, len(levels)):
                        if (total >= levels[z][0] and total < levels[z][1]
                            ) or (total * -1 >= levels[z][0]
                                  and total * -1 < levels[z][1]):
                            level = z + 1

                    # #0 is team1, and 1 is team2
                    # projected_team=0
                    # if self.league=="nba":
                    # 	#if team1 is projected to win
                    # 	if total>0:
                    # 		projected_team=0
                    # 	#go with home team
                    # 	elif total<=0:
                    # 		projected_team=1
                    # else:
                    # 	#if team1 is projected to win
                    # 	if total>0:
                    # 		projected_team=0
                    # 	#go with home team
                    # 	elif total<=0:
                    # 		projected_team=1

                    #0 is team1, and 1 is team2
                    #if team1 is projected to win
                    if total > 0:
                        projected_team = 0
                    #go with home team
                    elif total <= 0:
                        projected_team = 1

                    #0 is team1, and 1 is team2
                    winning_team = 0
                    if games[y]['game_scores'][0] > games[y]['game_scores'][1]:
                        winning_team = 0
                    else:
                        winning_team = 1

                    #if algo was right
                    if projected_team == winning_team:
                        wins[level] += 1
                    else:
                        losses[level] += 1

        temp = []

        for x in range(0, len(wins)):
            temp.append([losses[x], wins[x]])

        algo_list = str(algo).replace("[", "").replace("]",
                                                       "").replace(" ", "")
        path = "./" + str(self.league) + "/analyze/backtests/" + str(
            algo_list) + "_temp" + str(number) + ".csv"
        self.universal.save_to_csv(path, temp)
コード例 #9
0
    def backtest_csv_output(self, start_date, end_date):

        #breaks up date
        temp = start_date.split("-")
        month = int(temp[0])
        day = int(temp[1])
        year = int(temp[2])

        cur_date = start_date

        content = []
        #this is actually a do-while loop
        while True:
            games = self.universal.get_games(cur_date)
            print("date: " + cur_date)

            for game in games:
                print("  Game: " + str(game))

            to_save = []
            to_save.append([
                cur_date, "Away", "Home", "Algo points", "Proj winner",
                "Winner", "", "", "Score"
            ])
            for x in range(0, len(games)):

                #team might not exist anymore, so don't count that game
                if len(games[x]['team1']) != 0 and len(games[x]['team2']) != 0:

                    data1 = self.universal.load_data(games[x]['team1'],
                                                     games[x]['date'])
                    data2 = self.universal.load_data(games[x]['team2'],
                                                     games[x]['date'])
                    # print("Teams: "+str(games[x]['team1'])+" | "+str(games[x]['team2']))
                    # print("date: "+str(games[x]['date']))
                    # print(data1)
                    # print(data1[0]['dates'][0])
                    # print(data1[0]['dates'][-1])
                    returned1 = self.odds_calculator.analyze2(
                        games[x]['team1'], games[x]['team2'], data1, "away")
                    returned2 = self.odds_calculator.analyze2(
                        games[x]['team2'], games[x]['team1'], data2, "home")

                    # print(returned1)
                    # print()
                    # print(returned2)
                    # print()

                    algo = Algo(self.league)
                    if self.algo_version == "Algo_V1":
                        algo_data = algo.calculate(games[x]['date'], returned1,
                                                   returned2)
                    elif self.algo_version == "Algo_V2":
                        algo_data = algo.calculate_V2(games[x]['date'],
                                                      returned1, returned2)
                    total = algo_data['total']

                    to_add = []
                    to_add.append("")
                    to_add.append(games[x]['team1'][0])
                    to_add.append(games[x]['team2'][0])
                    to_add.append(total)

                    if self.algo_version == "Algo_V1":
                        #categorizes odds (points)
                        levels = []
                        levels.append([0, 3])
                        levels.append([3, 6])
                        levels.append([6, 9])
                        levels.append([9, 12])
                        levels.append([12, 15])
                        levels.append([15, 18])
                        levels.append([18, 21])
                        levels.append([21, 24])
                        levels.append([24, 27])
                        levels.append([27, 100])
                    elif self.algo_version == "Algo_V2":
                        #categorizes odds (percentage)
                        levels = []
                        levels.append([50, 55])
                        levels.append([55, 60])
                        levels.append([60, 65])
                        levels.append([65, 70])
                        levels.append([70, 75])
                        levels.append([75, 80])
                        levels.append([80, 85])
                        levels.append([85, 90])
                        levels.append([90, 95])
                        levels.append([95, 100])

                    level = 0
                    for y in range(0, len(levels)):
                        if (total >= levels[y][0] and total < levels[y][1]
                            ) or (total * -1 >= levels[y][0]
                                  and total * -1 < levels[y][1]):
                            level = y + 1

                    #appends projected team
                    if total > 0:
                        to_add.append(games[x]['team1'][0])
                    elif total <= 0:
                        to_add.append(games[x]['team2'][0])

                    #appends winning team
                    if games[x]['game_scores'][0] > games[x]['game_scores'][1]:
                        to_add.append(games[x]['team1'][0])
                    else:
                        to_add.append(games[x]['team2'][0])

                    #appends score
                    score = str(games[x]['game_scores'][0]) + "-" + str(
                        games[x]['game_scores'][1])
                    to_add.append(score)

                    # #appends algo data
                    # if to_add[-2]==to_add[-3]:
                    # 	to_add.append("")
                    # 	if self.algo_version=="Algo_V1":
                    # 		to_add.append(str(level))
                    # 	elif self.algo_version=="Algo_V2":
                    # 		to_add.append(total)
                    # elif to_add[-3]!="":
                    # 	if self.algo_version=="Algo_V1":
                    # 		to_add.append(str(level))
                    # 	elif self.algo_version=="Algo_V2":
                    # 		to_add.append(total)
                    # 	to_add.append("")
                    # else:
                    # 	to_add.append("")
                    # 	to_add.append("")

                    #appends algo data
                    if to_add[-2] == to_add[-3]:
                        to_add.append("")
                        to_add.append(str(level))
                    elif to_add[-3] != "":
                        to_add.append(str(level))
                        to_add.append("")
                    else:
                        to_add.append("")
                        to_add.append("")

                    #appends betting odds
                    if self.algo_version == "Algo_V1":
                        odds_calculator = Odds_Calculator(self.league)
                        odds = odds_calculator.get_odds(total)
                    elif self.algo_version == "Algo_V2":
                        odds = abs(total)

                    favorable_odds = (100 / (100 - abs(odds)) - 1) * 100
                    underdog_odds = (100 / (100 - abs(odds)) - 1) * 100
                    if total > 0:
                        to_add.append("-" + str(favorable_odds))
                        to_add.append("+" + str(underdog_odds))
                    else:
                        to_add.append("+" + str(underdog_odds))
                        to_add.append("-" + str(favorable_odds))

                    to_save.append(to_add)

            #space between data
            to_save.append(["", "", "", "", "", "", "", "", ""])

            #only saves day's games if there were actually games on that day
            if len(to_save) > 2:
                content.append(to_save)

            #breaks loop to act like do-while
            if cur_date == end_date:
                break

            day += 1
            if day > 31:
                month += 1
                day = 1

            #doesn't increment year since the season's year doesn't change
            if month > 12:
                month = 1
                day = 1

            #increments season at the end of the season to sometime in the middle
            if self.league == "nba":
                if "4-1-" in cur_date:
                    year += 1
                    month = 2
                    day = 1
            elif self.league == "nhl":
                if "4-1-" in cur_date:
                    year += 1
                    month = 2
                    day = 1
            elif self.league == "mlb":
                if "10-1-" in cur_date:
                    year += 1
                    month = 7
                    day = 1

            cur_date = str(month) + "-" + str(day) + "-" + str(year)

        #has most recent games first
        content.reverse()

        to_save = []
        for x in range(0, len(content)):
            for y in range(0, len(content[x])):
                to_save.append(content[x][y])

        if start_date != end_date:
            self.universal.save_to_csv(
                "./" + str(self.league) + "/analyze/" + str(self.league) +
                "_" + str(self.algo_version) + "_" + str(start_date) + "_" +
                str(end_date) + "_analysis.csv", to_save)
        else:
            self.universal.save_to_csv(
                "./" + str(self.league) + "/analyze/" + str(self.league) +
                "_" + str(self.algo_version) + "_" + str(end_date) +
                "_analysis.csv", to_save)
コード例 #10
0
    def backtest_odds(self, start_date, end_date):

        #breaks up date
        temp = start_date.split("-")
        month = int(temp[0])
        day = int(temp[1])
        year = int(temp[2])

        cur_date = start_date

        content = []
        #this is actually a do-while loop
        while True:
            games = self.universal.get_games(cur_date)
            print("date: " + cur_date)

            for game in games:
                print("  Game: " + str(game))

            # - Strategy 0.0: Bet on algo's projected winner, no matter the odds.
            # - Strategy 0.1: Bet on oddsmaker's projected winner, no matter the odds.
            # All below strategies incorporate placing a bet if the algorithm projects a team to win more often than the oddsmaker projects
            # - Strategy 1: Default strategy.
            # - Strategy 2: Placing a bet if that team is also the algo's favorite.
            # - Strategy 3: Placing a bet if that team is the algo's favorite, and the oddsmaker's underdog.
            # - Strategy 4: Placing a bet if the difference between the algorithm's projected odds and the oddsmaker's odds is also >= 45

            to_save = []
            strat00 = {"total_bet": 0, "total_win": 0}
            strat01 = {"total_bet": 0, "total_win": 0}
            strat1 = {"total_bet": 0, "total_win": 0}
            strat2 = {"total_bet": 0, "total_win": 0}
            strat3 = {"total_bet": 0, "total_win": 0}
            strat4 = {"total_bet": 0, "total_win": 0}
            for x in range(0, len(games)):

                #team might not exist anymore, so don't count that game
                if len(games[x]['team1']) != 0 and len(games[x]['team2']) != 0:

                    data1 = self.universal.load_data(games[x]['team1'],
                                                     games[x]['date'])
                    data2 = self.universal.load_data(games[x]['team2'],
                                                     games[x]['date'])

                    returned1 = self.odds_calculator.analyze2(
                        games[x]['team1'], games[x]['team2'], data1, "away")
                    returned2 = self.odds_calculator.analyze2(
                        games[x]['team2'], games[x]['team1'], data2, "home")

                    # print(returned1)
                    # print()
                    # print(returned2)
                    # print()

                    algo = Algo(self.league)
                    algo_data = algo.calculate_V2(games[x]['date'], returned1,
                                                  returned2)
                    total = algo_data['total']

                    # to_return={}
                    # to_return['record_points']=             odds['records']
                    # to_return['home_away_points']=          odds['home_away']
                    # to_return['home_away_10_games_points']= odds['home_away_10_games']
                    # to_return['last_10_games_points']=      odds['last_10_games']
                    # to_return['avg_points']=                odds['avg_points']
                    # to_return['avg_points_10_games']=       odds['avg_points_10_games']
                    # # to_return['win_streak']=                win_streak
                    # to_return['win_streak_home_away']=      odds['win_streak_home_away']
                    # to_return['total']=                     self.universal.convert_number(average)

                    odds = abs(total)

                    favorable_odds = round((100 / (100 - abs(odds)) - 1) * 100)
                    underdog_odds = round((100 / (100 - abs(odds)) - 1) * 100)

                    # print(str(year)+" | "+str(games[x]['team1'])+" | "+str(games[x]['team2'])+" | "+str(games[x]['game_scores']))

                    oddsportal_odds = self.universal.get_odds_game(
                        year, games[x]['team1'], games[x]['team2'],
                        games[x]['game_scores'])

                    if oddsportal_odds[0] != 0:
                        to_add = []
                        #date
                        to_add.append(cur_date)
                        #away
                        to_add.append(games[x]['team1'][1])
                        #home
                        to_add.append(games[x]['team2'][1])
                        #Algo Proj
                        to_add.append(str(total) + "%")
                        #Away proj
                        away_proj = 0
                        if total < 0:
                            away_proj = favorable_odds
                        else:
                            away_proj = underdog_odds * -1
                        to_add.append(away_proj)
                        #Home proj
                        home_proj = 0
                        if total > 0:
                            home_proj = favorable_odds
                        else:
                            home_proj = underdog_odds * -1
                        to_add.append(home_proj)
                        #Away odds
                        to_add.append(oddsportal_odds[0])
                        #Home odds
                        to_add.append(oddsportal_odds[1])
                        #Diff Away
                        away_diff = 0
                        if abs(away_proj - oddsportal_odds[0]) > 200:
                            away_diff = abs(away_proj -
                                            oddsportal_odds[0]) - 200
                        else:
                            away_diff = abs(away_proj - oddsportal_odds[0])
                        to_add.append(away_diff)
                        #Diff Home
                        home_diff = 0
                        if abs(home_proj - oddsportal_odds[1]) > 200:
                            home_diff = abs(home_proj -
                                            oddsportal_odds[1]) - 200
                        else:
                            home_diff = abs(home_proj - oddsportal_odds[1])
                        to_add.append(home_diff)

                        ## Strategy 0.0 ##
                        if away_proj < 0:
                            #Bet
                            to_add.append("$100")
                            strat00['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[0] > 0):
                                to_win = 100 * (oddsportal_odds[0] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[0] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][0] > games[x][
                                    'game_scores'][1]:
                                to_add.append("$" + str(100 + to_win))
                                strat00['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        else:
                            #Bet
                            to_add.append("$100")
                            strat00['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[1] > 0):
                                to_win = 100 * (oddsportal_odds[1] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[1] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][1] > games[x][
                                    'game_scores'][0]:
                                to_add.append("$" + str(100 + to_win))
                                strat00['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")

                        ## Strategy 0.1 ##
                        if oddsportal_odds[0] < 0 and oddsportal_odds[
                                0] < oddsportal_odds[1]:
                            #Bet
                            to_add.append("$100")
                            strat01['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[0] > 0):
                                to_win = 100 * (oddsportal_odds[0] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[0] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][0] > games[x][
                                    'game_scores'][1]:
                                to_add.append("$" + str(100 + to_win))
                                strat01['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        else:
                            #Bet
                            to_add.append("$100")
                            strat01['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[1] > 0):
                                to_win = 100 * (oddsportal_odds[1] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[1] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][1] > games[x][
                                    'game_scores'][0]:
                                to_add.append("$" + str(100 + to_win))
                                strat01['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")

                        ## Strategy 1 ##
                        if oddsportal_odds[0] > away_proj:
                            #Bet
                            to_add.append("$100")
                            strat1['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[0] > 0):
                                to_win = 100 * (oddsportal_odds[0] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[0] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][0] > games[x][
                                    'game_scores'][1]:
                                to_add.append("$" + str(100 + to_win))
                                strat1['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        elif oddsportal_odds[1] > home_proj:
                            #Bet
                            to_add.append("$100")
                            strat1['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[1] > 0):
                                to_win = 100 * (oddsportal_odds[1] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[1] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][1] > games[x][
                                    'game_scores'][0]:
                                to_add.append("$" + str(100 + to_win))
                                strat1['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        else:
                            to_add.append("")
                            to_add.append("")
                            to_add.append("")

                        ## Strategy 2 ##
                        if oddsportal_odds[0] > away_proj and away_proj < 0:
                            #Bet
                            to_add.append("$100")
                            strat2['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[0] > 0):
                                to_win = 100 * (oddsportal_odds[0] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[0] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][0] > games[x][
                                    'game_scores'][1]:
                                to_add.append("$" + str(100 + to_win))
                                strat2['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        elif oddsportal_odds[1] > home_proj and home_proj < 0:
                            #Bet
                            to_add.append("$100")
                            strat2['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[1] > 0):
                                to_win = 100 * (oddsportal_odds[1] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[1] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][1] > games[x][
                                    'game_scores'][0]:
                                to_add.append("$" + str(100 + to_win))
                                strat2['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        else:
                            to_add.append("")
                            to_add.append("")
                            to_add.append("")

                        ## Strategy 3 ##
                        if oddsportal_odds[
                                0] > away_proj and away_proj < 0 and oddsportal_odds[
                                    0] > 0:
                            #Bet
                            to_add.append("$100")
                            strat3['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[0] > 0):
                                to_win = 100 * (oddsportal_odds[0] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[0] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][0] > games[x][
                                    'game_scores'][1]:
                                to_add.append("$" + str(100 + to_win))
                                strat3['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        elif oddsportal_odds[
                                1] > home_proj and home_proj < 0 and oddsportal_odds[
                                    1] > 0:
                            #Bet
                            to_add.append("$100")
                            strat3['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[1] > 0):
                                to_win = 100 * (oddsportal_odds[1] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[1] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][1] > games[x][
                                    'game_scores'][0]:
                                to_add.append("$" + str(100 + to_win))
                                strat3['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        else:
                            to_add.append("")
                            to_add.append("")
                            to_add.append("")

                        ## Strategy 4 ##
                        if self.league == "mlb":
                            diff_amount = 45
                        elif self.league == "nba":
                            diff_amount = 100

                        if oddsportal_odds[
                                0] > away_proj and away_diff >= diff_amount:
                            #Bet
                            to_add.append("$100")
                            strat4['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[0] > 0):
                                to_win = 100 * (oddsportal_odds[0] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[0] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][0] > games[x][
                                    'game_scores'][1]:
                                to_add.append("$" + str(100 + to_win))
                                strat4['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        elif oddsportal_odds[
                                1] > home_proj and home_diff >= diff_amount:
                            #Bet
                            to_add.append("$100")
                            strat4['total_bet'] += 100
                            #To Win
                            to_win = 0
                            if (oddsportal_odds[1] > 0):
                                to_win = 100 * (oddsportal_odds[1] / 100)
                            else:
                                to_win = 100 / (oddsportal_odds[1] * -1 / 100)
                            to_add.append("$" + str(to_win))
                            #Won
                            if games[x]['game_scores'][1] > games[x][
                                    'game_scores'][0]:
                                to_add.append("$" + str(100 + to_win))
                                strat4['total_win'] += (100 + to_win)
                            else:
                                to_add.append("$0")
                        else:
                            to_add.append("")
                            to_add.append("")
                            to_add.append("")

                    else:
                        to_add = []

                    # #appends winning team
                    # if games[x]['game_scores'][0]>games[x]['game_scores'][1]:
                    # 	to_add.append(games[x]['team1'][0])
                    # else:
                    # 	to_add.append(games[x]['team2'][0])

                    # #appends score
                    # score=str(games[x]['game_scores'][0])+"-"+str(games[x]['game_scores'][1])
                    # to_add.append(score)

                    if len(to_add) != 0:
                        to_save.append(to_add)

            # to_save.append(["Date", "Away", "Home", "Algo proj", "Away proj", "Home proj", "Away odds", "Home odds", "Diff away", "Diff home", "Bet", "To win", "Won"])

            #only saves day's games if there were actually games on that day
            if len(to_save) > 2:

                #summary
                strat00_profit = strat00['total_win'] - strat00['total_bet']
                strat00_perc = strat00_profit / strat00['total_bet'] * 100
                strat01_profit = strat01['total_win'] - strat01['total_bet']
                strat01_perc = strat01_profit / strat01['total_bet'] * 100
                strat1_profit = strat1['total_win'] - strat1['total_bet']
                strat1_perc = strat1_profit / strat1['total_bet'] * 100
                strat2_profit = strat2['total_win'] - strat2['total_bet']
                if (strat2['total_bet'] > 0):
                    strat2_perc = strat2_profit / strat2['total_bet'] * 100
                else:
                    strat2_perc = 0
                strat3_profit = strat3['total_win'] - strat3['total_bet']
                if (strat3['total_bet'] > 0):
                    strat3_perc = strat3_profit / strat3['total_bet'] * 100
                else:
                    strat3_perc = 0
                strat4_profit = strat4['total_win'] - strat4['total_bet']
                if (strat4['total_bet'] > 0):
                    strat4_perc = strat4_profit / strat4['total_bet'] * 100
                else:
                    strat4_perc = 0

                #initializes with buffer columns
                summary = ["", "", "", "", "", "", "", "", "", ""]
                summary.append("$" + str(strat00['total_bet']))
                summary.append("$" + str(strat00_profit))
                summary.append(str(strat00_perc) + "%")
                summary.append("$" + str(strat01['total_bet']))
                summary.append("$" + str(strat01_profit))
                summary.append(str(strat01_perc) + "%")
                summary.append("$" + str(strat1['total_bet']))
                summary.append("$" + str(strat1_profit))
                summary.append(str(strat1_perc) + "%")
                summary.append("$" + str(strat2['total_bet']))
                summary.append("$" + str(strat2_profit))
                summary.append(str(strat2_perc) + "%")
                summary.append("$" + str(strat3['total_bet']))
                summary.append("$" + str(strat3_profit))
                summary.append(str(strat3_perc) + "%")
                summary.append("$" + str(strat4['total_bet']))
                summary.append("$" + str(strat4_profit))
                summary.append(str(strat4_perc) + "%")
                to_save.append(summary)

                #space between data
                to_save.append([
                    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
                    "", "", "", "", "", "", "", "", "", ""
                ])

                content.append(to_save)

            #breaks loop to act like do-while
            if cur_date == end_date:
                break

            day += 1
            if day > 31:
                month += 1
                day = 1

            #doesn't increment year since the season's year doesn't change
            if month > 12:
                month = 1
                day = 1

            #increments season at the end of the season to sometime in the middle
            if self.league == "nba":
                # if "4-1-" in cur_date:
                # 	year+=1
                # 	month=2
                # 	day=1
                if "4-1-" in cur_date:
                    year += 1
                    month = 1
                    day = 15
            elif self.league == "nhl":
                if "4-1-" in cur_date:
                    year += 1
                    month = 2
                    day = 1
            elif self.league == "mlb":
                if "10-1-" in cur_date:
                    year += 1
                    month = 7
                    day = 1

            cur_date = str(month) + "-" + str(day) + "-" + str(year)

        # #has most recent games first
        # content.reverse()

        to_save = []
        to_save.append([
            "Date", "Away", "Home", "Algo proj", "Away proj", "Home proj",
            "Away odds", "Home odds", "Diff away", "Diff home", "Bet",
            "To win", "Won", "Bet", "To win", "Won", "Bet", "To win", "Won",
            "Bet", "To win", "Won"
        ])
        for x in range(0, len(content)):
            for y in range(0, len(content[x])):
                to_save.append(content[x][y])

        if start_date != end_date:
            self.universal.save_to_csv(
                "./" + str(self.league) + "/analyze/" + str(self.league) +
                "_Algo_V2_" + str(start_date) + "_" + str(end_date) +
                "_backtest_odds.csv", to_save)
        else:
            self.universal.save_to_csv(
                "./" + str(self.league) + "/analyze/" + str(self.league) +
                "_Algo_V2_" + str(end_date) + "_backtest_odds.csv", to_save)
コード例 #11
0
    def team_comparison(self, algo_version, team1, team2, date, cur_year):

        self.algo = Algo(self.league)

        self.espn_scraper.update_data(team1, cur_year)
        self.espn_scraper.update_data(team2, cur_year)

        data1 = self.universal.load_data(team1, date, cur_year)
        data2 = self.universal.load_data(team2, date, cur_year)

        returned1 = self.analyze2(team1, team2, data1, "away")
        returned2 = self.analyze2(team2, team1, data2, "home")

        # print(str(team1)+" | "+str(team2))
        # print(returned1)
        # print(returned2)
        # print()

        if algo_version == "Algo_V1":
            algo_data = self.algo.calculate(date, returned1, returned2)
        elif algo_version == "Algo_V2":
            algo_data = self.algo.calculate_V2(date, returned1, returned2)

        record_points = algo_data['record_points']
        home_away_points = algo_data['home_away_points']
        home_away_10_games_points = algo_data['home_away_10_games_points']
        last_10_games_points = algo_data['last_10_games_points']
        avg_points = algo_data['avg_points']
        avg_points_10_games = algo_data['avg_points_10_games']
        # win_streak_10_games=       algo_data['win_streak_10_games']
        if self.league == "nhl":
            win_streak_home_away = algo_data['win_streak_home_away']
        total = algo_data['total']

        to_output = []
        to_output.append("")
        to_output.append("Date: " + str(date))
        to_output.append("Away: " + str(team1[1]) + " | Home: " +
                         str(team2[1]))

        if algo_version == "Algo_V1":
            win_streak = algo_data['win_streak']
            win_streak_home_away = algo_data['win_streak_home_away']
            if self.league == "nba":
                to_output.append("Seasonal Record:      " +
                                 str(record_points * 10) + "/10 = " +
                                 str(record_points))
                to_output.append("Home Away:            " +
                                 str(home_away_points * 10) + "/10 = " +
                                 str(home_away_points))
                to_output.append("Home away 10:         " +
                                 str(home_away_10_games_points * 5) + "/5 = " +
                                 str(home_away_10_games_points))
                to_output.append("Last 10 games:        " +
                                 str(last_10_games_points * 5) + "/5 = " +
                                 str(last_10_games_points))
                to_output.append("Avg points:           " +
                                 str(avg_points * 8) + "/8 = " +
                                 str(avg_points))
                to_output.append("Avg points 10:        " +
                                 str(avg_points_10_games * 8) + "/8 = " +
                                 str(avg_points_10_games))
                to_output.append("Win streak:           " +
                                 str(win_streak * 3) + "/3 = " +
                                 str(win_streak))
                to_output.append("Win streak home away: " +
                                 str(win_streak_home_away * 3) + "/3 = " +
                                 str(win_streak_home_away))
            else:
                to_output.append("Seasonal Record:      " +
                                 str(record_points * 5) + "/5 = " +
                                 str(record_points))
                to_output.append("Home Away:            " +
                                 str(home_away_points * 5) + "/5 = " +
                                 str(home_away_points))
                to_output.append("Home away 10:         " +
                                 str(home_away_10_games_points * 5) + "/5 = " +
                                 str(home_away_10_games_points))
                to_output.append("Last 10 games:        " +
                                 str(last_10_games_points * 5) + "/5 = " +
                                 str(last_10_games_points))
                to_output.append("Avg points:           " +
                                 str(avg_points / 2) + "*2 = " +
                                 str(avg_points))
                to_output.append("Avg points 10:        " +
                                 str(avg_points_10_games / 2) + "*2 = " +
                                 str(avg_points_10_games))
                to_output.append("Win streak:           " +
                                 str(win_streak * 3) + "/3 = " +
                                 str(win_streak))
                to_output.append("Win streak home away: " +
                                 str(win_streak_home_away * 3) + "/3 = " +
                                 str(win_streak_home_away))
            to_output.append("--------")
            to_output.append("Total: " + str(total))
            to_output.append("--------")

        elif algo_version == "Algo_V2":
            to_output.append("Seasonal Record:      " + str(record_points) +
                             "%")
            to_output.append("Home Away:            " + str(home_away_points) +
                             "%")
            to_output.append("Home away 10:         " +
                             str(home_away_10_games_points) + "%")
            to_output.append("Last 10 games:        " +
                             str(last_10_games_points) + "%")
            to_output.append("Avg points:           " + str(avg_points) + "%")
            to_output.append("Avg points 10:        " +
                             str(avg_points_10_games) + "%")
            # to_output.append("Win streak:           "+str(win_streak)+"%")
            if self.league == "nhl":
                to_output.append("Win streak home away: " +
                                 str(win_streak_home_away) + "%")
            to_output.append("--------")
            to_output.append("Total: " + str(total) + "%")
            to_output.append("--------")

        #chance of favorable team winning
        if algo_version == "Algo_V1":
            winning_odds = self.get_odds(total)
        elif algo_version == "Algo_V2":
            winning_odds = abs(total)

        to_output.append("Perc chance to win: " + str(winning_odds) + "%")

        favorable_odds = (100 / (100 - winning_odds) - 1) * 100
        underdog_odds = (100 / (100 - winning_odds) - 1) * 100
        to_output.append("Favorable team odds: -" + str(favorable_odds))
        to_output.append("Underdog team odds: +" + str(underdog_odds))

        return to_output
コード例 #12
0
def predict(grammar_file: str, word: str):
    word = word.strip().replace(' ', '')
    grammar = Grammar.from_file(grammar_file)
    algo = Algo()
    algo.fit(grammar)
    return algo.predict(word)
コード例 #13
0
ファイル: tic_tac_toe.py プロジェクト: subhammldlgh/Repo-1
#GUI Version of TIC-TAC-TOE using kivy
import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput

from algo import Algo

my_algo = Algo()
AI_mode = False
player_name = ['X', 'O']
player_sign = ['X', 'O']


#Login Page
class Login(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.cols = 2
        self.add_widget(Label(font_size=40, text="Tic Tac Toe"))
        self.add_widget(Label(text="Subham_Games", font_size=20))
        self.add_widget(Label(text="Player X:", font_size=20))
        self.player_x = TextInput(multiline=False, font_size=20)
        self.add_widget(self.player_x)
        self.add_widget(Label(text="Player O:", font_size=20))
        self.player_o = TextInput(text="Computer",
                                  multiline=False,
                                  font_size=20)
コード例 #14
0
ファイル: simu.py プロジェクト: KohlerHECTOR/PANDROIDE
    def train(self, pw,params, policy, critic, policy_loss_file, critic_loss_file, study_name, beta=0, is_cem=False):
        all_weights=np.zeros((int(params.nb_cycles+1),policy.get_weights_dim(False)))
        all_rewards=np.zeros(params.nb_cycles+1)
        best_reward=-np.inf
        best_weights=np.zeros(policy.get_weights_dim(False))

        all_pops=np.zeros((params.nb_cycles,params.population,policy.get_weights_dim(False)))
        all_pops_scores= np.zeros((params.nb_cycles,params.population))
        # is_kept = np.zeros((params.nb_cycles,params.population))
        list_elite_index=np.zeros((params.nb_cycles,int(params.elites_frac * params.population)))
        fixed=params.fix_layers
        idx_best=0
        if is_cem == False:
            if fixed:
                print(fixed)
                fc1_w, fc1_b, fc2_w, fc2_b = policy.get_weights_pg()
                # print(fc1_w)
                # print(policy.test())

        if is_cem == True:
            all_weights=np.zeros((int(params.nb_cycles+1),policy.get_weights_dim(fixed)))
            best_weights=np.zeros(policy.get_weights_dim(fixed))
            #random init of the neural network.
            #so far, all the layers are initialized with the same gaussian.
            init_weights = np.array(params.sigma*np.random.randn(policy.get_weights_dim(False)))
            #print(np.shape(init_weights))
            #start_weights=np.array(3*np.random.randn(policy.get_weights_dim(False)))
            policy.set_weights(init_weights, False)

            print(fixed)
            #print(params.fix_layers)
            #print(policy.get_weights_dim(params.fix_layers))
            study = params.study_name
            noise=np.diag(np.ones(policy.get_weights_dim(fixed))*params.sigma)
            #print(np.shape(noise))
            #var=np.cov(init_weights[:,-policy.get_weights_dim(fixed):],rowvar=False) + noise
            #mu=init_weights[:,-policy.get_weights_dim(fixed):].mean(axis=0)

            var=np.diag(np.ones(policy.get_weights_dim(fixed))*np.var(init_weights))+noise
            print(np.shape(var))
            mu=init_weights[-policy.get_weights_dim(fixed):]
            all_weights[0]=mu
            all_rewards[0]=self.evaluate_episode(policy, params.deterministic_eval)
            print(np.shape(mu))
            rng = np.random.default_rng()

            #we can draw the last layer from a different gaussian
            #mu=params.sigma_bis*np.random.randn(policy.get_weights_dim(params.fix_layers))
        for cycle in range(params.nb_cycles):
            if is_cem == True:
                rewards = np.zeros(params.population)
                weights=rng.multivariate_normal(mu, var, params.population)
                for p in range(params.population):
                    policy.set_weights(weights[p], fixed)
                    batch=self.make_monte_carlo_batch(params.nb_trajs_cem, params.render, policy, True)
                    rewards[p] = batch.train_policy_cem(policy, params.bests_frac)
                    all_pops[cycle,p]=weights[p]
                    all_pops_scores[cycle,p]=rewards[p]

                elites_nb = int(params.elites_frac * params.population)
                elites_idxs = rewards.argsort()[-elites_nb:]
                list_elite_index[cycle]=elites_idxs
                # for i in elites_idxs:
                #     is_kept[cycle][i]=1

                elites_weights = [weights[i] for i in elites_idxs]
                #update the best weights
                mu = np.array(elites_weights).mean(axis=0)
                var = np.cov(elites_weights,rowvar=False)+noise

                #print(best_weights)
                # policy evaluation part
                policy.set_weights(mu, fixed)

                total_reward = self.evaluate_episode(policy, params.deterministic_eval)

                if total_reward>best_reward:
                    best_weights=mu
                    best_reward=total_reward
                    idx_best=cycle
                all_rewards[cycle+1]=total_reward
                # if total_reward>np.min(top_ten_scores):
                #     temp_min=np.argmin(top_ten_scores)
                #     top_ten_scores[temp_min]=total_reward
                #     top_ten_policies[temp_min]=mu

                # Update the file for the plot
                # reward_file = policy_loss_file
                # reward_file.write(str(cycle) + " " + str(total_reward) + "\n")
                # if (cycle+1)%3==0:
                    # all_weights[int((cycle+1)/3)-1]=mu
                all_weights[cycle+1]=mu

            elif is_cem == False:
                batch = self.make_monte_carlo_batch(params.nb_trajs_pg, params.render, policy)

                # Update the policy
                batch2 = batch.copy_batch()
                algo = Algo(study_name, params.critic_estim_method, policy, critic, params.gamma, beta, params.nstep)
                algo.prepare_batch(batch)
                policy_loss = batch.train_policy_td(policy)
                # if (cycle+1)%3==0:
                #     all_weights[int((cycle+1)/3)-1]=policy.get_weights_as_numpy()
                all_weights[cycle]=policy.get_weights_as_numpy()
                #print(policy_loss)

                # Update the critic
                assert params.critic_update_method in ['batch', 'dataset'], 'unsupported critic update method'
                if params.critic_update_method == "dataset":
                    critic_loss = algo.train_critic_from_dataset(batch2, params)
                elif params.critic_update_method == "batch":
                    critic_loss = algo.train_critic_from_batch(batch2)
                critic_loss_file.write(str(cycle) + " " + str(critic_loss) + "\n")
                policy_loss_file.write(str(cycle) + " " + str(policy_loss) + "\n")
                plot_trajectory(batch2, self.env, cycle+1)

                # policy evaluation part
                if fixed:
                    policy.set_weights_pg(fc1_w, fc1_b, fc2_w, fc2_b)
                total_reward = self.evaluate_episode(policy, params.deterministic_eval)
                all_rewards[cycle]=total_reward
                if total_reward>best_reward:
                    best_weights=policy.get_weights_as_numpy()
                    best_reward=total_reward
                    idx_best=cycle
            print(total_reward)
        # X_embedded = TSNE(n_components=2).fit_transform(all_cem_weights)
        # # print(np.shape(X_embedded))
        # # print(X_embedded)
        # plt.scatter(*zip(*X_embedded))
        # return all_weights,best_weights,all_rewards,idx_best
        return all_weights,all_rewards,all_pops,all_pops_scores,list_elite_index
コード例 #15
0
def main(argv):
    
    #===== Initialization =====#
    
    # Parse arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('settings_file', type=str)
    parser.add_argument('-s', '--sound', action='store_true', help="use sound as a feature in analysis")
    parser.add_argument('-b', '--backup', action='store_true', help="start training on backup data")
    parser.add_argument('-t', '--time_allign', action='store_true', help="collect data at times which are multiples of the granularity")
    args = parser.parse_args(argv[1:])
        
    # Initialize Zway server
    port = 8083
    host = '172.31.16.102'
    zserver = zway.Server(host, port)

    # Read settings from settings file
    try:
        settings_dict = settings.load(args.settings_file)
    except Exception as error:
        print "Error reading settings file.", error
        print " "
        exit(1)

    # Initialize Algo class
    granularity = int(settings_dict['granularity'])
    training_window = int(settings_dict['training_window'])
    training_interval = int(settings_dict['training_interval'])
    ema_alpha = float(settings_dict['ema_alpha'])
    severity_omega = float(settings_dict['severity_omega'])
    severity_lambda = float(settings_dict['severity_lambda'])
    auto_regression = int(settings_dict['auto_regression'])
    num_features = len(zserver.device_IDs())
    
    print "Num features: ", num_features
    
    algo = Algo(granularity, training_window, training_interval, num_features)
    algo.setSeverityParameters(severity_omega, severity_lambda)
    algo.setEMAParameter(ema_alpha)
    
    # Timing procedure
    granularity = settings_dict['granularity'] * 60
    granularity = 1
    goal_time = time.time()
    if args.time_allign:
        goal_time = int(goal_time) + granularity - (int(time.time()) % granularity)

    #===== Analysis =====#
    
    while(True):
    
        # Timing procedures
        while goal_time > time.time():
            time.sleep(0.2)
        goal_time = goal_time + granularity
        
        # Data collection
        features = collect_features(zserver)
        print list(features)
        
        # Data analysis
        target, pred = algo.run(features)
        if (pred != None):
            print target, pred, algo.checkSeverity(target, pred)
            print "theta", algo.w_opt
        else:
            print target, pred
コード例 #16
0
ファイル: simu.py プロジェクト: KohlerHECTOR/PANDROIDE
    def train_pg(self,
                 pw,
                 params,
                 policy,
                 critic,
                 policy_loss_file,
                 critic_loss_file,
                 study_name,
                 beta=0) -> None:
        """
        The main function for training and evaluating a policy
        Repeats training and evaluation params.nb_cycles times
        Stores the value and policy losses at each cycle
        When the reward is greater than the best reward so far, saves the corresponding policy
        :param pw: a policy wrapper, used to save the best policy into a file
        :param params: the hyper-parameters of the run, specified in arguments.py or in the command line
        :param policy: the trained policy
        :param policy_loss_file: the file to record successive policy loss values
        :return: nothing
        """
        # Initialize variables
        self.list_weights = []
        self.best_weights = np.zeros(policy.get_weights_dim())
        self.list_rewards = np.zeros((int(params.nb_cycles)))
        self.best_reward = -1e38
        self.best_weights_idx = 0
        total_reward = self.best_reward
        self.list_weights.append(policy.get_weights())

        if params.start_from_policy:
            starting_weights = get_starting_weights(pw)
            policy.set_weights(starting_weights)

        print("Shape of weights vector is: ", np.shape(self.best_weights))
        initial_score = self.evaluate_episode(policy,
                                              params.deterministic_eval,
                                              params)
        total_reward = initial_score
        pw.save(cycle=0, score=initial_score)
        self.env.write_reward(cycle=0, reward=initial_score)
        with SlowBar('Performing a repetition of PG',
                     max=params.nb_cycles - 1) as bar:
            for cycle in range(1, params.nb_cycles):
                batch = self.make_monte_carlo_batch(params.nb_trajs,
                                                    params.render, policy)
                if params.reinforce:
                    batch.sum_rewards()
                    policy_loss = batch.train_policy_td(policy)
                    # self.env.write_gradients(gradient_angles,cycle)
                    policy_loss_file.write(
                        str(cycle) + " " + str(policy_loss) + "\n")
                    batch = self.make_monte_carlo_batch(
                        params.nb_trajs, params.render, policy)

                else:
                    # Update the policy
                    batch2 = batch.copy_batch()
                    algo = Algo(params.study_name, params.critic_estim_method,
                                policy, critic, params.gamma, beta,
                                params.nstep)
                    algo.prepare_batch(batch)
                    policy_loss = batch.train_policy_td(policy)

                    # Update the critic
                    assert params.critic_update_method in [
                        'batch', 'dataset'
                    ], 'unsupported critic update method'
                    if params.critic_update_method == "dataset":
                        critic_loss = algo.train_critic_from_dataset(
                            batch2, params)
                    elif params.critic_update_method == "batch":
                        critic_loss = algo.train_critic_from_batch(batch2)
                    critic_loss_file.write(
                        str(cycle) + " " + str(critic_loss) + "\n")
                    policy_loss_file.write(
                        str(cycle) + " " + str(policy_loss) + "\n")
                    plot_trajectory(batch2, self.env, cycle + 1)

                # add the new weights to the list of weights
                self.list_weights.append(policy.get_weights())
                distance = np.linalg.norm(self.list_weights[-1] -
                                          self.list_weights[-2])
                self.env.write_distances(cycle, distance)
                self.write_angles_global(cycle)

                # policy evaluation part
                if (cycle % params.eval_freq) == 0:
                    total_reward = self.evaluate_episode(
                        policy, params.deterministic_eval, params)
                    # wrote and store reward
                    self.env.write_reward(cycle, total_reward)
                    self.list_rewards[cycle] = total_reward
                    # plot_trajectory(batch2, self.env, cycle+1)

                # save best reward agent (no need for averaging if the policy is deterministic)
                if self.best_reward < total_reward:
                    self.best_reward = total_reward
                    self.best_weights = self.list_weights[-1]
                    self.best_weights_idx = cycle
                # Save the best policy obtained
                if (cycle % params.save_freq) == 0:
                    pw.save(cycle=cycle, score=total_reward)
                bar.next()

        # pw.rename_best(method="PG",best_cycle=self.best_weights_idx,best_score=self.best_reward)
        print("Best reward: ", self.best_reward)
        print("Best reward iter: ", self.best_weights_idx)
コード例 #17
0
        """
        self._event_engine.unregister(algo)
        if algo.config.launch_type == 'unactive':
            self._event_manage.remove_Config(algo.config)

    def deploy_algo(self):
        pass


if __name__ == '__main__':
    from BaseConfig import BaseConfig
    from BaseAlgo import BaseAlgo
    from algo import Algo
    mainengin = MainEngine()
    mainengin.start()
    algo = Algo(BaseAlgo, BaseConfig)
    mainengin.add_algo(algo)
    mainengin.create_algo_instance('test_event', {
        'id': 5,
        'hello': 'world',
        'haha': 'heihei'
    })

    for i in xrange(100):
        mainengin.create_algo_instance('test_event', {
            'id': 5,
            i * 5: i,
            i * 2: 'saa'
        })

    # mainengin.stop()
コード例 #18
0
 def __init__(self, grammar: Grammar):
     self.grammar = grammar
     self.algo = Algo()
     self.algo.fit(grammar)