def create_counterstrategy(consmdp, capacity, targets, init_state, energy=None, solver=GoalLeaningES, objective=BUCHI, threshold=0.1): """ Create counter strategy for given parameters and the current consMDP object and return the strategy """ if energy is None: energy = capacity if solver == GoalLeaningES: slvr = GoalLeaningES(consmdp, capacity, targets, threshold=threshold) elif solver == BasicES: slvr = BasicES(consmdp, capacity, targets) selector = slvr.get_selector(objective) strategy = CounterStrategy(consmdp, selector, capacity, energy, init_state=init_state) return strategy
# for item in targets: # reload_list.append(item) print(reload_list) env = SynchronousMultiAgentEnv(num_agents=num_agents, grid_size=[gridsize, gridsize], capacities=[400 for _ in range(num_agents)], reloads=reload_list, targets=targets, init_states=init_state, enhanced_actionspace=0) env.allocate_targets(final_tarjan_assignment) consmdp1 = env.get_consmdp() MDP = consmdp1[0] #generate targets #compute strategies, pulled from Pranay for agent in env.agents: print(agent) solver = GoalLeaningES(MDP, env.capacities[agent], env.targets_alloc[agent], threshold=0.3) selector = solver.get_selector(AS_REACH) strategy = CounterStrategy(env.consmdp, selector, env.capacities[agent], env.energies[agent], init_state=env.init_states[agent]) env.update_strategy(strategy, agent) env.animate_simulation(num_steps=400, interval=100)
s.next_action() s.update_state(3) assert False except ValueError: print("Passed test 7 for Strategy in file test_strategy.py") # # Test CounterStrategy from fimdp.core import CounterStrategy # + solver = BasicES(m, 30, T) selector = solver.get_selector(AS_REACH) s = CounterStrategy(m, selector=selector, capacity=30, init_energy=15, init_state=6) s.next_action() s.update_state(7) assert s.energy == 14 s.next_action() s.update_state(6) assert s.energy == 27 s.next_action() s.update_state(3) assert s.energy == 21 s.next_action() s.next_action(6) s.next_action(7)
# Get ProductSelector dba_sel = lmdp.selector_for_dba(aut, cap=capacity) ltl_sel = lmdp.selector_for_ltl("GF s1 & GF s2", cap=capacity) assert ltl_sel == dba_sel print("Passed test selector_for_ltl in file test_product.py") # ### Test DBACounterStrategy # We create 2 strategies: # 1. A `CounterStrategy` that works on product CMDP # 2. A `DBACounterStrategy` that works on labeled CMDP # Get CounterSelector product, targets = lmdp.product_with_dba(aut) psolver = BasicES(product, capacity, targets) counter_sel = psolver.get_selector(BUCHI) from fimdp.core import CounterStrategy from fimdp.labeled import DBACounterStategy cs = CounterStrategy(product, counter_sel, capacity, init_energy) dbas = DBACounterStategy(lmdp, aut, dba_sel, capacity, init_energy) # We now start a play from the initial states (0 in both cases) and check whether the strategies give equivalent choices. for outcome in [0, 2, 3, 0, 2, 3, 0, 1, 4, 5, 4, 5, 6, 3, 0]: pr_action = cs.next_action(outcome) orig_action = dbas.next_action(product.components[outcome][0]) assert product.orig_action(pr_action) == orig_action print("Passed test for DBA strategy in file test_product")