コード例 #1
0
def reset_boats():
    global BOATS, CONTROLLERS, WAYPOINT_QUEUE, WAYPOINTS_INDEX, WAYPOINTS_BEFORE_RESET, LAST_COMPLETED_WP_TIME, LAST_TIME, FIRST_TIME, TEXT_BOXES
    BOATS = {"pid": Boat.Boat(design=Designs.AirboatDesign()),
             "q": Boat.Boat(design=Designs.TankDriveDesign())}
    # generate all the random waypoints
    generate_random_waypoints_queue()
    waypoint = WAYPOINT_QUEUE[0]
    px, py = xy_location_to_pixel_location(waypoint[0], waypoint[1])
    LAST_TIME = 0
    FIRST_TIME = ptime.time()
    for k in BOATS:
        boat = BOATS[k]
        WAYPOINTS_INDEX[k] = 0
        LAST_COMPLETED_WP_TIME[k] = 0
        boat.state = np.zeros((6,))
        boat.time = 0
        boat.name = k + " boat"
        NAVIGATION_LINES[k].set_data(pos=np.array([(px, py), xy_location_to_pixel_location(boat.state[0], boat.state[1])], dtype=np.float32))
        TEXT_BOXES["waypoint_symbol"][k].pos = (px, py)
        TEXT_BOXES["waypoint_text"][k].text = "[{:.0f}, {:.0f}]".format(px, py)
        TEXT_BOXES["waypoint_count"][k].text = "#{} of {}".format(WAYPOINTS_INDEX[k] + 1, WAYPOINTS_BEFORE_RESET)
        #boat.strategy = Strategies.DestinationOnly(boat, waypoint, controller_name=CONTROLLERS[k])
        #boat.strategy = Strategies.LineFollower(boat, waypoint, controller_name=CONTROLLERS[k])
        if (k == "pid"):
            boat.strategy = Strategies.PseudoRandomBalancedHeading(boat, fixed_thrust=0.2, angle_divisions=8)
        else:
            boat.strategy = Strategies.DoNothing(boat)

        boat.sourceLocation = boat.state[0:2]
        boat.destinationLocation = waypoint
        boat.calculateQState()  # need to initialize the state for Q learning
コード例 #2
0
def generate_chart_windowed_rebalancing():
    parameters = {}
    parameters['primary'] = raw_input("Enter Stock 1: ")
    parameters['secondary'] = raw_input("Enter Stock 2: ")
    parameters['primary_leverage'] = float(raw_input("Stock 1 Leverage: "))
    parameters['primary_ratio'] = float(
        raw_input("Enter Stock 1 target ratio: "))
    parameters['rebalance_window'] = float(
        raw_input("Enter rebalancing window: "))

    securities = [
        Security(parameters['primary'],
                 leverage=parameters['primary_leverage']),
        parameters['secondary']
    ]
    portfolio = Portfolio(None)
    portfolio.generate_new_portfolio(securities)
    portfolio.set_investments(
        10, {
            parameters['primary']: parameters['primary_ratio'],
            parameters['secondary']: 1 - parameters['primary_ratio']
        })

    params = {}
    params['primary'] = parameters['primary']
    params['secondary'] = parameters['secondary']
    params['rebalance_window'] = parameters['rebalance_window']

    strategy = Strategies.Strategy(Strategies.windowed_rebalancing, params)
    portfolio = execute_simulation(portfolio, strategy)
    GraphingTools.plot_portfolio(portfolio, params)
コード例 #3
0
def evaluate(pop, size, rounds, strategyChosen):

    min = float(0)  # lowest score of population
    max = float(0)  # highest score of population
    scores = numpy.empty(0, float)

    for ind in pop:
        ind.scores = numpy.empty(0, int)
        Strategies.TestStrategy(ind, rounds, strategyChosen)
        indScore = numpy.average(ind.scores)
        scores = numpy.append(scores, indScore)

        # update lowest and highest scores
        if indScore < min:
            min = indScore
        if indScore > max:
            max = indScore

    avg = numpy.average(scores)
    coefficients = countCoefficients(min, max, avg, 2)

    for i in range(size):
        evaluateScaled(pop[i], scores[i], coefficients)
        print(i + 1, "score:", round(scores[i], 2), "fitness:", pop[i].fitness)

    return
コード例 #4
0
ファイル: Simulate.py プロジェクト: Umbriona/GameTheory
def generateAgent(list_models):
    list_neural_agents = []
    for i in list_models:
        if '.h5' in i and 'rng' in i and not 'OpMe' in i:
            tmp = strat.Neural201Agent(name=i, actionSpace=2)
            tmp.loadModel('model')
            list_neural_agents.append(tmp)
    return list_neural_agents
コード例 #5
0
ファイル: Simulate.py プロジェクト: Umbriona/GameTheory
def stability(nReplicates, epochs, gamma, v=False):
    game = Games.PrisonersDilemma('Joshua')
    action_space = game.actionSpace
    pathData = os.path.join('data', 'simulations')

    avgScoreM = np.zeros(epochs * nReplicates)
    avg_op = np.zeros(epochs * nReplicates)
    avg_me = np.zeros(epochs * nReplicates)
    avg_switches_op = np.zeros(epochs * nReplicates)
    avg_switches_me = np.zeros(epochs * nReplicates)
    f = 0
    for i in range(nReplicates):
        player1 = strat.Neural203Agent(name='Neural',
                                       actionSpace=game.actionSpace)
        player1.gamma = gamma
        player3 = strat.TitF2tatAgent(name='TitF2Tat')
        list_of_players = [player1, player3]
        if v:
            print('process id:', os.getpid(), '\tReplicat nr:', i)
        for j in range(epochs * i, epochs * i + epochs):
            for k in list_of_players:

                if k.name == 'Neural':
                    k.prepThread(len(list_of_players) - 1 + f, 10)
                else:
                    k.clearHistory(len(list_of_players) - 1 + f)
            game.tournament(list_of_players, 200, 0.05, False)
            avgScoreM[j] = np.sum(list_of_players[0].lastScore) / (200)
            avg_op[j] = np.sum(list_of_players[0].lastOp) / (200)
            avg_me[j] = np.sum(list_of_players[0].lastMe) / (200)
            avg_switches_op[j] = countSwitches(
                list_of_players[0].lastOp[0]) / (200)
            avg_switches_me[j] = countSwitches(
                list_of_players[0].lastMe[0]) / (200)
            for j in list_of_players:
                if j.name == 'Neural':
                    j.train()
    df = {}
    df['avgScore'] = avgScoreM
    df['avgOp'] = avg_op
    df['avgMe'] = avg_me
    df['avgSOp'] = avg_switches_op
    df['avgSMe'] = avg_switches_me

    df = pd.DataFrame(df)
    df.to_csv(os.path.join(pathData, 'Stability_data_gamma{}'.format(gamma)))
コード例 #6
0
ファイル: StrategyManager.py プロジェクト: sdp-2011/sdp-3
 def checkStrategyReload(self):
     
     Strategies.reloadTactics()
     
     stat = os.stat("./Strategies.py")
     mtime = stat.st_mtime
     if mtime != self.lastStrategyChange:
         self.lastStrategyChange = mtime
         print ">> Reloading Strategies"
         os.system("rm Strategies.pyc")
         reload(Strategies)
         strat_class_name = self.strategyClass.__name__
         del self.strategyClass
         del self.strategy
         self.strategyClass = Strategies.__dict__[strat_class_name]
         self.strategy = self.strategyClass()
         print ">> Building new Strategy of type "+self.strategy.__class__.__name__
         print ">> AI stopping"
         self.running = False
コード例 #7
0
def main(strategy_type, admin_host, admin_port):
    if not isinstance(admin_port, int):
        raise ValueError()

    if strategy_type == "random":
        strategy = Strategies.RandomStrategy()
    elif strategy_type == "look-ahead":
        try:
            with open("strategy.config", "r") as f:
                num_looks_ahead = parse_json(
                    f.read())[0]["value"]["look-ahead"]
            strategy = Strategies.NLooksAheadStrategy(num_looks_ahead)
        except FileNotFoundError:
            print(
                "strategy.config for look-ahead strategy file not found in directory!"
            )
            sys.exit(1)
    elif strategy_type == "smart":
        strategy = Strategies.SmartStrategy()
    elif strategy_type == "greedy":
        strategy = Strategies.GreedyStrategy()
    elif strategy_type == "interactive":
        strategy = Strategies.InteractiveStrategy()
    elif strategy_type == "cheating":
        strategy = Strategies.CheatingStrategy()
    else:
        raise ValueError("Unsupported strategy type!")

    player = SmartPlayer(input("Type your player's name: "), strategy)

    player_driver = PlayerDriver(player, admin_host, admin_port)
    player_driver.start_driver()
コード例 #8
0
ファイル: Train.py プロジェクト: Umbriona/GameTheory
def main():

    os.environ["CUDA_VISIBLE_DEVICES"] = ""
    train_1v1 = False
    #Initiate Players 1 vs 1
    if train_1v1 == True:
        obj_list = print_classes()
        neural_list = [
            strat.Neural201Agent(name='NeuralAgent_' + obj_list[i].name +
                                 '_rng',
                                 actionSpace=2) for i in range(len(obj_list))
        ]
        list_of_players = [[neural_list[i], obj_list[i]]
                           for i in range(len(obj_list))]

        #print(Process(target = train, args = (list_of_players[i], 0.04)))
        list_of_processes = [
            Process(target=train, args=(list_of_players[i], 0.02))
            for i in range(len(list_of_players))
        ]
        thread = 8
        for i in range(len(list_of_processes) // thread + 1):
            for j in range(i * thread,
                           min(i * thread + thread, len(list_of_processes))):
                list_of_processes[j].start()
            for j in range(i * thread,
                           min(i * thread + thread, len(list_of_processes))):
                list_of_processes[j].join()

    else:
        print('Battle Royal!!!!')
        obj_list = print_classes()
        list_of_players = []
        list_of_players.append(
            strat.Neural201Agent(name='NeuralAgent_BattleRoyal',
                                 actionSpace=2))
        list_of_players += obj_list
        train(list_of_players, 0.0, v=True)
    return 0
コード例 #9
0
    def classic_button_function(self):
        self.exit_button_function()

        root = ThemedTk(theme=self.THEME)
        root.title(APPNAME)
        ws = root.winfo_screenwidth() / 2
        hs = root.winfo_screenheight() / 2
        root.geometry('%dx%d+%d+%d' % (580, 614, ws - 290, hs - 307))
        root.resizable(0, 0)


        game = PvAI_Game(Board(), Strategies.Minimax(2, Player.BLACK, Player.WHITE), root, width=592, height=618)
        game.pack(side="top", fill="both", expand="true")
        game.draw_pieces()

        root.mainloop()
コード例 #10
0
def generate_chart_moving_average():
    parameters = {}
    parameters['primary'] = raw_input("Enter Stock 1: ")
    parameters['secondary'] = raw_input("Enter Stock 2: ")
    parameters['primary_leverage'] = float(raw_input("Stock 1 Leverage: "))
    parameters['moving_window'] = int(
        raw_input("Enter moving average window in days: "))
    parameters['sample'] = raw_input('Random Sampling? [True, False]: ')

    securities = [
        Security(parameters['primary'],
                 leverage=parameters['primary_leverage']),
        parameters['secondary']
    ]
    portfolio = Portfolio(None)
    portfolio.generate_new_portfolio(securities)
    portfolio.set_investments(0, {
        parameters['primary']: 1,
        parameters['secondary']: 0
    })
    portfolio.cash = 10000

    if parameters['sample'] == 'True':
        sampler = RandomSampler(
            portfolio.holdings[parameters['primary']].security)
        portfolio.holdings[
            parameters['primary']].security.data = sampler.sample

    moving_average = Security(parameters['primary'])
    moving_average_base = portfolio.holdings[parameters['primary']].security
    moving_average.set_data(
        parameters['primary'], portfolio.holdings[
            parameters['primary']].security.generate_moving_average(
                parameters['moving_window']))

    params = {}
    params['primary'] = parameters['primary']
    params['secondary'] = parameters['secondary']
    params['moving_average'] = moving_average
    params['moving_average_base'] = moving_average_base

    strategy = Strategies.Strategy(Strategies.moving_average_rebalancing,
                                   params)
    portfolio = execute_simulation(portfolio, strategy)
    GraphingTools.plot_portfolio(portfolio, params)
コード例 #11
0
def iterate(event):  # event is unused
    global FIRST_TIME, LAST_TIME, BOATS, CANVAS, TIME_DILATION, LAST_COMPLETED_WP_TIME, FAILED_WAYPOINT_TIMEOUT, WAYPOINTS_INDEX, CONTROLLERS, WAYPOINT_QUEUE
    global TEXT_BOXES, EXPERIENCES, TOTAL_ITERATIONS, NAVIGATION_LINES, TOTAL_BATCHES
    if TOTAL_ITERATIONS < 1:
        FIRST_TIME = ptime.time()  # there is a huge gap in time as the window opens, so we need this manual time reset for the very first iteration
    TOTAL_ITERATIONS += 1
    current_time = TIME_DILATION*(ptime.time() - FIRST_TIME)
    # print "Total iterations = {}, t = {}".format(TOTAL_ITERATIONS, current_time)
    TEXT_BOXES["time"].text = "t = {}".format(format_time_string(current_time, 2))
    # USE ODE TO PROPAGATE BOAT STATE
    times = np.linspace(LAST_TIME, current_time, 100)
    for k in BOATS:
        boat = BOATS[k]
        boat.control()
        # if the boat actually changes action, we should create a Q learning experience
        # (i.e. BEFORE we change actions here, the state before ode is s' in (s, a, r, s')
        # The experience is created in boat.control() right before new actions are selected
        boat.time = current_time
        states = spi.odeint(Boat.ode, boat.state, times, (boat,))
        boat.state = states[-1]
        boat.state[4] = Boat.wrapToPi(boat.state[4])
        px, py = xy_location_to_pixel_location(states[-1][0], states[-1][1])
        heading = Boat.wrapTo2Pi(states[-1][4])
        BOAT_VISUALS[k].new_pose(px, py, heading)
        if boat.strategy.finished or current_time - LAST_COMPLETED_WP_TIME[k] > FAILED_WAYPOINT_TIMEOUT:
            WAYPOINTS_INDEX[k] += 1
            LAST_COMPLETED_WP_TIME[k] = current_time
            if WAYPOINTS_INDEX[k] < len(WAYPOINT_QUEUE):
                waypoint = WAYPOINT_QUEUE[WAYPOINTS_INDEX[k]]
                px, py = xy_location_to_pixel_location(waypoint[0], waypoint[1])
                NAVIGATION_LINES[k].set_data(pos=np.array([(px, py), xy_location_to_pixel_location(boat.state[0], boat.state[1])], dtype=np.float32))
                TEXT_BOXES["waypoint_symbol"][k].pos = (px, py+15)  # py-0.5*fontsize to center the text vertically
                TEXT_BOXES["waypoint_text"][k].text = "[{:.0f}, {:.0f}]".format(px, py)
                TEXT_BOXES["waypoint_count"][k].text = "#{} of {}".format(WAYPOINTS_INDEX[k]+1, WAYPOINTS_BEFORE_RESET)
                #boat.strategy = Strategies.DestinationOnly(boat, waypoint, controller_name=CONTROLLERS[k])
                boat.strategy = Strategies.LineFollower(boat, waypoint, controller_name=CONTROLLERS[k])
                boat.sourceLocation = boat.state[0:2]
                boat.destinationLocation = waypoint
    if not WAYPOINTS_INDEX["pid"] < WAYPOINTS_BEFORE_RESET or not WAYPOINTS_INDEX["q"] < WAYPOINTS_BEFORE_RESET:
        TOTAL_BATCHES += 1
        reset_boats()
    else:
        LAST_TIME = current_time
    CANVAS.update()
コード例 #12
0
 def __init__(self, t=0.0, name="boat", design=Designs.TankDriveDesign()):
     self._t = t  # current time [s]
     self._name = name
     self._state = np.zeros((6, ))
     self._sourceLocation = np.zeros((2, ))  # where the boat started from
     self._destinationLocation = np.zeros(
         (2, ))  # where the boat is going (typically the next waypoint)
     # state: [x y u w th thdot]
     self._thrustSurge = 0.0  # surge thrust [N]
     self._thrustSway = 0.0  # sway thrust (zero for tank drive) [N]
     self._moment = 0.0  # [Nm]
     self._thrustSurgeOLD = 0.0  # used for exponential curve toward new value
     self._thrustSwayOLD = 0.0  # used for exponential curve toward new value
     self._momentOLD = 0.0  # used for exponential curve toward new value
     self._decayConstant = 0.5  # used for exponential curve toward new value, lower means slower
     self._thrustFraction = 0.0
     self._momentFraction = 0.0
     self._strategy = Strategies.DoNothing(self)
     self._design = design
     self._plotData = None  # [x, y] data used to display current actions
     self._controlHz = 10  # the number of times per second the boat is allowed to change its signal, check if strategy is finished, and create Q experiences
     self._lastControlTime = 0
     self._Q = _Q_
     self._Qstate = np.zeros(
         (8, ))  # [u w alpha delta phi alphadot deltadot phidot]
     self._QlastState = np.zeros(
         (8, ))  # the state "s" in the experience (s, a, r, s')
     self._QlastAction = np.zeros(
         (2, )
     )  # the action "a" in the experience (s, a, r, s'), [m0_signal m1_signal]
     # alpha = progress along line between origin and waypoint, normalized by the length of the line
     # delta = distance to the line (projection of boat onto the line)
     # phi = angle of the line with respect to the surge direction in the body frame
     # mX_signal = raw signal value of actuator X
     self._QExperienceQueue = list(
     )  # a list containing the current set of experiences in the order they were created
     self._lastExperienceTime = 0
コード例 #13
0
ファイル: GoTeam.py プロジェクト: hwork/nao-man
    def strategize(self):
        """
        Picks the strategy to run and returns all sorts of infos
        """
        # First we check for testing stuff
        if TEST_DEFENDER:
            return Strategies.sTestDefender(self)
        elif TEST_OFFENDER:
            return Strategies.sTestOffender(self)
        elif TEST_MIDDIE:
            return Strategies.sTestMiddie(self)
        elif TEST_CHASER:
            return Strategies.sTestChaser(self)

        # Now we look at shorthanded strategies
        elif self.numInactiveMates == 1:
            return Strategies.sOneDown(self)
        elif self.numInactiveMates == 2:
            return Strategies.sTwoDown(self)

        # Here we have the strategy stuff
        return Strategies.sSpread(self)
コード例 #14
0
ファイル: Main.py プロジェクト: ottomattas/INFOMAA
# Note: From this script the program can be run.
# You may change everything about this file.

from MatrixSuite import FixedMatrixSuite
import Strategies
import Game
from GrandTable import GrandTable
from ReplicatorDynamic import ReplicatorDynamic
import Nash

# Output some basic things to show how to call the classes.
matrix_suite = FixedMatrixSuite()
print(matrix_suite)
strategies = [Strategies.Aselect(), Strategies.Aselect(), Strategies.Aselect()]
print(strategies)
grand_table = GrandTable(matrix_suite, strategies, 9, 1000)
print(grand_table)

# Example of how to test a strategy:
matrix_suite = FixedMatrixSuite()  # Create a matrix suite

strat = Strategies.Aselect()  # Create the strategy you want to test.

strat.initialize(
    matrix_suite, "row"
)  # Initialise it with the game suite and as either "row" or "col" player.

action = strat.get_action(1)  # Get the next action
print("Strategy plays action:" + action.__repr__())

strat.update(1, action, 1.5,
コード例 #15
0
def generate_dataset(parameters, func):
    if func.__name__ == Strategies.moving_average_rebalancing.__name__:
        securities = [
            Security(parameters['primary'],
                     leverage=parameters['primary_leverage']),
            parameters['secondary']
        ]
        portfolio = Portfolio(None)
        portfolio.generate_new_portfolio(securities)
        portfolio.set_investments(0, {
            parameters['primary']: 1,
            parameters['secondary']: 0
        })
        portfolio.cash = 10000

        if parameters['leveraged_moving_average'] in [True, 'True']:
            moving_average = Security(parameters['primary'],
                                      leverage=parameters['primary_leverage'])
            moving_average_base = Security(
                parameters['primary'], leverage=parameters['primary_leverage'])
        else:
            moving_average = Security(parameters['primary'])
            moving_average_base = Security(parameters['primary'])

        moving_average.set_data(
            parameters['primary'],
            moving_average.generate_moving_average(
                parameters['moving_window']))

        params = {}
        params['primary'] = parameters['primary']
        params['secondary'] = parameters['secondary']
        params['moving_average'] = moving_average
        params['moving_average_base'] = moving_average_base

        strategy = Strategies.Strategy(parameters['rebalancing_function'],
                                       params)
        return portfolio, strategy
    elif func.__name__ == Strategies.windowed_rebalancing.__name__:
        securities = [
            Security(parameters['primary'],
                     leverage=parameters['primary_leverage']),
            parameters['secondary']
        ]
        portfolio = Portfolio(None)
        portfolio.generate_new_portfolio(securities)
        portfolio.set_investments(
            10, {
                parameters['primary']: parameters['primary_ratio'],
                parameters['secondary']: 1 - parameters['primary_ratio']
            })

        params = {}
        params['primary'] = parameters['primary']
        params['secondary'] = parameters['secondary']
        params['rebalance_window'] = parameters['rebalance_window']

        strategy = Strategies.Strategy(parameters['rebalancing_function'],
                                       params)
        return portfolio, strategy
    else:
        raise Exception('Invalid Trading Strategy')
コード例 #16
0
    def custom_button_function(self):
        self.exit_button_function()
        all_movements_list = []

        moves = []
        attacks = []

        custom_window = ThemedTk(theme=self.THEME)
        custom_window.title(APPNAME + ': Define custom movements')
        custom_window.geometry('470x750')
        ws = custom_window.winfo_screenwidth() / 2
        hs = custom_window.winfo_screenheight() / 2
        custom_window.geometry('%dx%d+%d+%d' % (470, 750, ws - 235, hs - 375))
        custom_window.resizable(0, 0)
        
        def add_move():
            try:
                x = int(x_move.get())
                y = int(y_move.get())
            except ValueError:
                return False

            moves.append((x, y))

        def add_attack():
            try:
                x = int(x_attack.get())
                y = int(y_attack.get())
            except ValueError:
                return False

            attacks.append((x, y))

        def add_new_movement():
            new_movement = CustomMovement()

            new_movement.set_name(move_name.get())
            new_movement.set_vacant(vacant_flag.get())

            str_moves = ""
            for move in moves:
                str_moves += '(' + str(move[0]) + ', ' + str(move[1]) + ') ' 
                new_movement.add_custom_movement(move[0], move[1])

            str_attacks = ""
            for attack in attacks:
                str_attacks += '(' + str(attack[0]) + ', ' + str(attack[1]) + ') ' 
                new_movement.add_custom_attack(attack[0], attack[1])

            moves.clear()
            attacks.clear()
            all_movements_list.append(new_movement)
            tv.insert("", 0, text=move_name.get(), values=(str_moves, str_attacks))
        
        move_name = tk.StringVar()
        x_move = tk.StringVar()
        y_move = tk.StringVar()
        x_attack = tk.StringVar()
        y_attack = tk.StringVar()
        vacant_flag = tk.BooleanVar()


        style = ttk.Style(custom_window)
        font = Font(size=FONTSIZE)
        style.configure("TButton", font=font)
     
        subtitle_font = Font(size=60, weight='bold')
        frame = ttk.Frame(custom_window, width=470, height=750)
        frame.grid(row=0, columnspan=3)
        frame.pack(fill="both", expand=1)

        pb_label = ttk.Label(frame, text="Progress:")
        pb_label.grid(row=0, column=0, padx=10, pady=10, sticky=tk.W)

        pb = ttk.Progressbar(frame, orient="horizontal", length=200, mode="determinate")
        pb.grid(row=0, column=1, padx=10, pady=10, sticky=tk.E)
        pb["maximum"] = 8
        pb["value"] = 1

        name_label = ttk.Label(frame, text="Set movement rule name:")
        name_label.grid(row=1, column=0, padx=10, pady=10, sticky=tk.W)

        move_name_entry = ttk.Entry(frame, textvariable=move_name)
        move_name_entry.grid(row=1, column=1, padx=10, pady=10, sticky=tk.W)

        move_x_label = ttk.Label(frame, text="Move on X:")
        move_x_label.grid(row=3, column=0, padx=10, pady=5, sticky=tk.W)

        move_x = ttk.Entry(frame, textvariable=x_move, width=5)
        move_x.grid(row=3, column=1, padx=10, pady=5, sticky=tk.W)

        move_y_label = ttk.Label(frame, text="Move on Y:")
        move_y_label.grid(row=4, column=0, padx=10, pady=5, sticky=tk.W)

        move_y = ttk.Entry(frame, textvariable=y_move, width=5)
        move_y.grid(row=4, column=1, padx=10, pady=5, sticky=tk.W)
        
        add_move_button = ttk.Button(frame, text="Add new move", command=add_move)
        add_move_button.grid(row=5, columnspan=3, padx=10, pady=10)

        attack_x_label = ttk.Label(frame, text="Attack on X:")
        attack_x_label.grid(row=7, column=0, padx=10, pady=5, sticky=tk.W)

        attack_x = ttk.Entry(frame, textvariable=x_attack, width=5)
        attack_x.grid(row=7, column=1, padx=10, pady=5, sticky=tk.W)

        attack_y_label = ttk.Label(frame, text="Attack on Y:")
        attack_y_label.grid(row=8, column=0, padx=10, pady=5, sticky=tk.W)

        attack_y = ttk.Entry(frame, textvariable=y_attack, width=5)
        attack_y.grid(row=8, column=1, padx=10, pady=5, sticky=tk.W)

        add_attack_button = ttk.Button(frame, text="Add new attack", command=add_attack)
        add_attack_button.grid(row=9, columnspan=3, padx=10, pady=10)

        set_vacant_label = ttk.Label(frame, text="Move on vacant squares:")
        set_vacant_label.grid(row=11, column=0, padx=10, pady=10, sticky=tk.W)

        set_vacant_checkbox = ttk.Checkbutton(frame, variable=vacant_flag)
        set_vacant_checkbox.grid(row=11, column=1, padx=10, pady=10, sticky=tk.W)

        add_button = ttk.Button(frame, text="Add new movement", command=add_new_movement)
        add_button.grid(row=12, columnspan=3, padx=10, pady=10)

        
        tv = ttk.Treeview(frame)
        tv['columns'] = ('moves', 'attacks')
        tv.heading("#0", text='Movement', anchor='w')
        tv.column("#0", anchor="w")
        tv.heading('moves', text='Moves')
        tv.column('moves', anchor='center', width=100)
        tv.heading('attacks', text='Attacks')
        tv.column('attacks', anchor="e", width=100)
        tv.grid(rowspan=8, columnspan=3, padx=20, pady=10, sticky = (tk.N, tk.S, tk.W, tk.E))
    
        add_button = ttk.Button(frame, text="Next step", command=custom_window.destroy)
        add_button.grid(row=22, columnspan=3, padx=10, pady=10)
        custom_window.mainloop()
        
        all_movements_list.append(HorizontalMovement())
        all_movements_list.append(LimitedDiagonalMovement())
        all_movements_list.append(LimitedHorizontalMovement())
        all_movements_list.append(LimitedVerticalMovement())
        all_movements_list.append(DiagonalMovement())
        all_movements_list.append(HorizontalMovement())
        all_movements_list.append(VerticalMovement())
        all_movements_list.append(PawnMovement())
        all_movements_list.append(HorseMovement())

        pieces = ['Pawn', 'Horse', 'Bishop', 'Rook', 'Queen', 'King']
        pieces_movement_flags = []

        def submit_check():
            for val in result:
                if val.get():
                    custom_window.destroy()
                    return
            self.popup("Check at least one movement for the piece!")

        cnt = 1
        for piece in pieces:
            custom_window = ThemedTk(theme=self.THEME)
            custom_window.title(APPNAME + ': Customize piece movements')
            ws = custom_window.winfo_screenwidth() / 2
            hs = custom_window.winfo_screenheight() / 2
            custom_window.geometry('%dx%d+%d+%d' % (470, 750, ws - 235, hs - 375))
            custom_window.resizable(0, 0)

            frame = ttk.Frame(custom_window, width=470, height=750)
            frame.grid(row=0, columnspan=3, sticky="nsew")
            frame.pack(fill="both", expand=1)

            pb_label = ttk.Label(frame, text="Progress:", font=subtitle_font)
            pb_label.grid(row=0, column=0, padx=30, pady=10, sticky=tk.W)

            pb = ttk.Progressbar(frame, orient="horizontal", length=200, mode="determinate")
            pb.grid(row=0, column=0, padx=140, pady=10, sticky=tk.W)
            pb["maximum"] = 8
            pb["value"] = 1 + cnt
            
            cnt += 1
            
            piece_label = ttk.Label(frame, text=piece, font=subtitle_font)
            piece_label.grid(row=1, column=0, columnspan=3, padx=30, pady=30, sticky="nsew")
            
            count = 0
            result = []
            for movement in all_movements_list:
                if piece=='King' and 'Limited' in movement.name:
                        result.append(tk.BooleanVar(value=True))
                elif piece=='Bishop' and movement.name=='Diagonal movement':
                        result.append(tk.BooleanVar(value=True))
                elif piece=='Queen' and (movement.name=='Horizontal movement' or movement.name=='Vertical movement' or movement.name=='Diagonal movement'):
                        result.append(tk.BooleanVar(value=True))
                elif piece=='Rook' and (movement.name=='Horizontal movement' or movement.name=='Vertical movement'):
                        result.append(tk.BooleanVar(value=True))
                elif piece =='Horse' and movement.name=='Horse movement':
                        result.append(tk.BooleanVar(value=True))
                elif piece=='Pawn' and movement.name=='Pawn movement':
                        result.append(tk.BooleanVar(value=True))
                else:
                    result.append(tk.BooleanVar(value=False))
                ttk.Checkbutton(frame,
                               text=movement.name,
                               variable=result[count]).grid(row=count+2, column=0, padx=50, pady=10, sticky=tk.W)
                
                
                count += 1

            add_button = ttk.Button(frame, text="Next step", command=submit_check)
            add_button.grid(row=22, columnspan=3, padx=30, pady=30, sticky=tk.S)
            custom_window.mainloop()

            pieces_movement_flags.append(result)
        custom_window = ThemedTk(theme=self.THEME)
        custom_window.title(APPNAME + ': Custom locations')
        ws = custom_window.winfo_screenwidth() / 2
        hs = custom_window.winfo_screenheight() / 2
        custom_window.geometry('%dx%d+%d+%d' % (660, 750, ws - 330, hs - 375))
        custom_window.resizable(0, 0)

        frame = ttk.Frame(custom_window, width=660, height=750)
        frame.pack(fill="both", expand=1)
        frame.grid(row=0, column=0, columnspan=6)
        
        pb_label = ttk.Label(frame, text="Progress:", font=subtitle_font)
        pb_label.grid(row=0, column=0, padx=30, pady=10, sticky=tk.W)

        pb = ttk.Progressbar(frame, orient="horizontal", length=200, mode="determinate")
        pb.grid(row=0, column=1, columnspan=5, padx=30, pady=10, sticky=tk.W)
        pb["maximum"] = 8
        pb["value"] = 8

        pieces_for_game = []

        def add_piece_to_game():
            flag_count = 0
            piece_to_add = Piece(pieces[piece_name.get()], pieces[piece_name.get()][0])
            for flag in pieces_movement_flags[piece_name.get()]:
                if flag.get():
                    piece_to_add.add_movement(all_movements_list[flag_count])

                flag_count += 1

            piece_to_add.set_position(Position(column.get(), row.get()))
            pieces_for_game.append(piece_to_add)
            logs.insert(tk.END, "\nAdded " + piece_to_add.name + " at position " + str(Position(column.get(), row.get())) + ".")

        piece_selection_label = ttk.Label(frame, text='Piece selection', font=subtitle_font)
        piece_selection_label.grid(row=1, column=0, padx=30, pady=30)

        count = 0
        piece_name = tk.IntVar()
        for piece in pieces:
            ttk.Radiobutton(frame,
                           text=piece,
                           variable=piece_name,
                           value=count).grid(row=count+2, column=0, padx=30, pady=10, sticky=tk.W)
            count += 1
        sep = ttk.Separator(frame, orient="vertical")
        sep.grid(column=1, row=1, rowspan=10, pady=20, sticky="ns")

        piece_location_label = ttk.Label(frame, text='Piece location', font=subtitle_font)
        piece_location_label.grid(row=1, column=2, columnspan=2, padx=30, pady=30)

        piece_location_description_label = ttk.Label(frame, text='Select the row\n(first is the closest)')
        piece_location_description_label.grid(row=2, column=2, columnspan=2, padx=10, pady=10)

        row = tk.IntVar(value=1)
        ttk.Radiobutton(frame, text='First row', variable=row, value=1).grid(row=3, column=2, columnspan=2, sticky=tk.W, padx=20)
        ttk.Radiobutton(frame, text='Second row', variable=row, value=2).grid(row=4, column=2, columnspan=2, sticky=tk.W, padx=20)
        piece_location_description_label = ttk.Label(frame, text='Select the column\n(from left to right)')
        piece_location_description_label.grid(row=5, column=2, columnspan=2, padx=10, pady=10)

        column = tk.IntVar(value=1)
        for i in range(4):
            ttk.Radiobutton(frame, text=str(i + 1), variable=column, value=i + 1).grid(row=6+i, column=2, padx=40, pady=10, sticky=tk.W)
            ttk.Radiobutton(frame, text=str(i + 5), variable=column, value=i+5).grid(row=6+i, column=3, padx=40, pady=10, sticky=tk.E)
        sep2 = ttk.Separator(frame, orient="vertical")
        sep2.grid(column=4, row=1, rowspan=12, pady=20, sticky="ns")
        add_button = ttk.Button(frame, text="Add", command=add_piece_to_game, width=20, style="TButton")
        add_button.grid(row=1, rowspan=11, column=5, sticky="nsew", padx=20, pady=240)
       
        logs = tk.Text(frame, height=8)
        logs.grid(row=13, column=0, columnspan=6, rowspan=1, padx=10, pady=5)
        logs.insert(tk.END, "Try adding pieces to the board!")
       
        play_button = ttk.Button(frame, text="Play", command=custom_window.destroy, width=30, style="TButton")
        play_button.grid(row=14, column=0, columnspan=7, sticky="nsew", padx=10, pady=10)
        
        custom_window.mainloop()

        custom_board = Board()
        custom_board.board = [[None for j in range(custom_board.SIZE)] for i in range(custom_board.SIZE)]

        for el in pieces_for_game:
            # x coloana, y randul
            el_pos_x_w = el.position.x - 1
            el_pos_y_w = el.position.y - 1

            el_pos_x_b = 7 - el_pos_x_w

            if el_pos_y_w == 0:
                el_pos_y_b = 7
            else:
                el_pos_y_b = 6

            custom_board.add_piece(el, Position(el_pos_y_w, el_pos_x_w), Player.WHITE)
            custom_board.add_piece(el, Position(el_pos_y_b, el_pos_x_b), Player.BLACK)

        root = ThemedTk(theme=self.THEME)
        root.title(APPNAME)
        ws = root.winfo_screenwidth() / 2
        hs = root.winfo_screenheight() / 2
        root.geometry('%dx%d+%d+%d' % (580, 614, ws - 290, hs - 307))
        root.resizable(0, 0)

        game = PvAI_Game(custom_board, Strategies.MinimaxRandomSample(Player.BLACK, Player.WHITE, 2, 10, 10), root, custom_flag=True, width=592, height=618)
        game.pack(side="top", fill="both", expand="true")
        game.draw_pieces()

        root.mainloop()
コード例 #17
0
import MatrixSuite
from MatrixSuite import FixedMatrixSuite
from MatrixSuite import RandomIntMatrixSuite
from MatrixSuite import RandomFloatMatrixSuite
import Strategies
from Strategies import Strategy
import Game
from GrandTable import GrandTable
from ReplicatorDynamic import ReplicatorDynamic, Proportions
import Nash
from typing import List
'''Setting up strategies and relative proportions'''

strategies = [
    Strategies.Aselect(),
    Strategies.EGreedy(),
    Strategies.UCB(),
    Strategies.Satisficing(),
    Strategies.Softmax(),
    Strategies.FictitiousPlay(),
    Strategies.Bully(),
    Strategies.ProportionalRegretMatching()
]
strategies_ext = [
    Strategies.Aselect(),
    Strategies.EGreedy(),
    Strategies.UCB(),
    Strategies.Satisficing(),
    Strategies.Softmax(),
    Strategies.FictitiousPlay(),
コード例 #18
0
def main():

    #for arg in sys.argv:
    iterations = 20  # default
    if len(sys.argv) == 2:
        iterations = int(sys.argv[1])
    print("Iterations: ")
    print(iterations)

    init()

    #-------------------------------
    # Liste des stratégies dans Strategies.py
    #-------------------------------
    """
    - def strategie_aleatoire_uniforme(nbRestau)
    - def strategie_tetue(numPlayer, nbRestau)
    - def strategie_restau_dans_lordre(numRestau, nbRestau)
    - def strategie_le_plus_proche(posJoueur,goalStates,nbRestau)
    - def strategie_min_taux_rempli(tauxRemplissage, nbRestau)
    - def strategie_max_taux_rempli(tauxRemplissage, nbRestau)


    """

    #-------------------------------
    # Initialisation
    #-------------------------------
    nbLignes = game.spriteBuilder.rowsize
    nbColonnes = game.spriteBuilder.colsize
    print("lignes", nbLignes)
    print("colonnes", nbColonnes)

    players = [o for o in game.layers['joueur']]
    nbPlayers = len(players)

    # on localise tous les états initiaux (loc du joueur)
    initStates = [o.get_rowcol() for o in game.layers['joueur']]
    print("Init states:", initStates)

    # on localise tous les objets  ramassables (les restaurants)
    goalStates = [o.get_rowcol() for o in game.layers['ramassable']]
    print("Goal states:", goalStates)
    nbRestaus = len(goalStates)

    # on localise tous les murs
    wallStates = [w.get_rowcol() for w in game.layers['obstacle']]
    #print ("Wall states:", wallStates)

    # on liste toutes les positions permises
    allowedStates = [(x,y) for x in range(nbLignes) for y in range(nbColonnes)\
                     if (x,y) not in wallStates and (x,y) not in goalStates]
    #print ("allowed states:",len(allowedStates), allowedStates)

    #initialisation pour avoir le numéro des clients dans chaque restaurant
    clientsRestau = {r: [] for r in range(nbRestaus)}

    #taux remplissage de chaque restau
    tauxRestau = [0 for i in range(nbRestaus)]

    #gains des joueurs
    gainsJoueur = [0 for i in range(nbPlayers)]

    #nom de la stratégie utilisée par chaque joueur
    nomJoueurStrategie = []

    posPlayers = initStates
    restau = [0] * nbPlayers
    #-------------------------------
    # Boucle principale de déplacements
    #-------------------------------
    #vitesse d'execution : voir frameskip de gameclass.py (ligne 66)

    for i in range(iterations):

        #--------------------------------------------------------------
        # Placement aleatoire des joueurs sur la carte, en évitant les obstacles
        #--------------------------------------------------------------
        #posPlayers = initStates

        for j in range(nbPlayers):
            x, y = random.choice(allowedStates)
            players[j].set_rowcol(x, y)
            game.mainiteration()
            posPlayers[j] = (x, y)

        #--------------------------------------------------------------
        # Chaque joueur choisit un restaurant
        #--------------------------------------------------------------
        nomJoueurStrategie = []
        #restau=[0]*nbPlayers
        for j in range(nbPlayers):
            s = ""
            if j == 0:
                c = Strategies.strategie_aleatoire_uniforme(nbRestaus)
                s = "Strategie aleatoire uniforme"
            elif j == 1:
                c = Strategies.strategie_aleatoire_uniforme(nbRestaus)
                s = "Strategie aleatoire uniforme"
            elif j == 2:
                c = Strategies.strategie_tetue(j, nbRestaus)
                s = "Strategie tetue"
            elif j == 3:
                c = Strategies.strategie_tetue(j, nbRestaus)
                s = "Strategie tetue"
            elif j == 4:
                c = Strategies.strategie_restau_dans_lordre(
                    restau[j], nbRestaus)
                s = "Strategie restau dans l'ordre"
            elif j == 5:
                c = Strategies.strategie_restau_dans_lordre(
                    restau[j], nbRestaus)
                s = "Strategie restau dans l'ordre"
            elif j == 6:
                c = Strategies.strategie_le_plus_proche(
                    posPlayers[j], goalStates, nbRestaus)
                s = "Strategie restau le plus proche"
            elif j == 7:
                c = Strategies.strategie_le_plus_proche(
                    posPlayers[j], goalStates, nbRestaus)
                s = "Strategie restau le plus proche"
            elif j == 8:
                c = Strategies.strategie_min_taux_rempli(tauxRestau, nbRestaus)
                s = "Strategie restau le moins rempli"
            elif j == 9:
                c = Strategies.strategie_min_taux_rempli(tauxRestau, nbRestaus)
                s = "Strategie restau le moins rempli"

            #print(c)
            nomJoueurStrategie.append(s)
            restau[j] = c
            clientsRestau[c].append(j)

        #--------------------------------------------------------------
        # Chaque joueur se rend au restaurant de son choix avec l'algorithme A*
        #--------------------------------------------------------------

        for j in range(
                nbPlayers):  # on fait bouger chaque joueur séquentiellement
            pos_restau = goalStates[restau[j]]
            chemin_le_plus_court = Astar.aStar(posPlayers[j], pos_restau,
                                               wallStates, 19)

            while posPlayers[j] != pos_restau:
                min_g, min_pos = heapq.heappop(chemin_le_plus_court)
                row, col = min_pos

                players[j].set_rowcol(row, col)
                print("pos :", j, row, col)
                game.mainiteration()

                posPlayers[j] = (row, col)

                # si on est à l'emplacement d'un restaurant, on s'arrête
                if (row, col) == pos_restau:
                    #o = players[j].ramasse(game.layers)
                    game.mainiteration()
                    print("Le joueur ", j, " est à son restaurant.")
                    # goalStates.remove((row,col)) # on enlève ce goalState de la liste
                    break

        #--------------------------------------------------------------
        # Les joueurs obtiennent leur gain, et prennent connaissance des taux de remplissage de chaque restaurant
        #--------------------------------------------------------------

        #un joueur est choisi au hasard parmis ceux dans le restau si plusieurs joueurs se trouvent dans un même restaurant
        for r in range(nbRestaus):
            if len(clientsRestau[r]) == 1:
                num_j = clientsRestau[r][0]
                gainsJoueur[num_j] += 1

            elif len(clientsRestau[r]) > 1:
                num_j = random.choice(clientsRestau[r])
                gainsJoueur[num_j] += 1

        #taux de remplissage de chaque restaurant
            tauxRestau[r] = len(clientsRestau[r]) / nbPlayers
            print("Taux remplissage Restau", r, " :", tauxRestau[r])

        #réinitialisation des clients par restau à chaque itération
        clientsRestau = {r: [] for r in range(nbRestaus)}

    #affichage des gains
    for g in range(len(gainsJoueur)):
        print("Le joueur", g, " a une moyenne de gains de :",
              gainsJoueur[g] / iterations)

    pygame.quit()
コード例 #19
0
    # clear tables for testing
    cur = holdings.conn.cursor()
    cur.execute('delete from holdings_record')
    cur.execute('delete from asset_values_record')
    cur.execute('delete from current_holdings')
    cur.execute('delete from orders_record')    
    
    # inject cash
    holdings.orders.append(Holdings.Order('cash', 1, opt.initial_fund, opt.date))
    holdings.update_holdings()
    
    for i in range(test_days):
        print("\n-----------Date:", opt.date, "-----------")
        # trade based on strategy S2
        holdings.orders = Strategies.S2(opt, holdings, stock_price.stock_mat, 4, 16)
        holdings.update_holdings()
        
        # trade based on strategy S1
        holdings.orders = Strategies.S1(opt, holdings, stock_price.stock_mat, 4, 16)
        holdings.update_holdings()
        
        # show current holdings and orders record
        print("\nCurrent Holdings:")
        #print("symbol \t\t num \t\t price \t\t avg_cost \t\t date")
        print("{:10}\t{:10}\t{:10}\t{:10}\t{:10}".format('symbol', 'num', 'price', 'avg_cost', 'date'))
        cur.execute('select * from current_holdings')
        temp_records = cur.fetchall()
        for stock in temp_records:
            #print(stock[0], '\t\t', stock[1], '\t\t', str.format('{0:.2f}', stock[2]), '\t\t', str.format('{0:.2f}', stock[3]), '\t\t', stock[4])
            print("{:10}\t{:10}\t{:10}\t{:10}\t{:10}".format(stock[0], stock[1], str.format('{0:.2f}', stock[2]), str.format('{0:.2f}', stock[3]), stock[4]))