Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Episodic manager #13

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
74 changes: 74 additions & 0 deletions examples/QTable/model/new_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
import argparse
import time

from mivp_agent.episodic_manager import EpisodicManager
from mivp_agent.util.math import dist
#from mivp_agent.util.display import ModelConsole
from mivp_agent.aquaticus.const import FIELD_BLUE_FLAG
from constants import DEFAULT_RUN_MODEL
from model import load_model

class Agent:
def __init__(self, own_id, opponent_id, model) -> None:
self.own_id = own_id
self.opponent_id = opponent_id
self.q, self.attack_actions, self.retreat_actions = load_model(model)
self.current_action = None

def id(self):
return self.own_id

def obs_to_rpr(self, observation):
model_representation = self.q.get_state(
observation['NAV_X'],
observation['NAV_Y'],
observation['NODE_REPORTS'][self.opponent_id]['NAV_X'],
observation['NODE_REPORTS'][self.opponent_id]['NAV_Y'],
observation['HAS_FLAG']
)
#console.tick(observation) #needs full msg, obervation is an msg.state

return model_representation

def rpr_to_act(self, rpr, observation): #why rpr and observation? we talked about this but can't remember
self.current_action = self.q.get_action(rpr)

# Determine action set
if observation['HAS_FLAG']:
current_action_set = self.retreat_actions
else:
current_action_set = self.attack_actions

# Construct instruction for BHV_Agent
action = {
'speed': current_action_set[self.current_action]['speed'],
'course': current_action_set[self.current_action]['course']
}

flag_dist = abs(dist((observation['NAV_X'], observation['NAV_Y']), FIELD_BLUE_FLAG))

if flag_dist < 10:
action['posts']= {
'FLAG_GRAB_REQUEST': f'vname={self.own_id}'
}

return action


if __name__ == '__main__':
# Create agents required
parser = argparse.ArgumentParser()
parser.add_argument('--model', default=DEFAULT_RUN_MODEL)
args = parser.parse_args()

agents = []
wait_for = []
for i in [1, 2, 3]:
agents.append(Agent(f'agent_1{i}', f'drone_2{i}', args.model))
wait_for.append(f'agent_1{i}')

#console = ModelConsole() #where will this live? Do we care? Needs a full msg instead of msg.state
mgr = EpisodicManager(agents, 13, wait_for=wait_for) #13 for 10 full episodes... not ideal
mgr.start('runner')

120 changes: 57 additions & 63 deletions examples/QTable/model/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,73 @@
import argparse
import time

from mivp_agent.manager import MissionManager
from mivp_agent.episodic_manager import EpisodicManager
from mivp_agent.util.math import dist
from mivp_agent.util.display import ModelConsole
#from mivp_agent.util.display import ModelConsole
from mivp_agent.aquaticus.const import FIELD_BLUE_FLAG

from constants import DEFAULT_RUN_MODEL
from model import load_model

class Agent:
def __init__(self, own_id, opponent_id, model) -> None:
self.own_id = own_id
self.opponent_id = opponent_id
self.q, self.attack_actions, self.retreat_actions = load_model(model)
self.current_action = None

def id(self):
return self.own_id


def obs_to_rpr(self, observation):
model_representation = self.q.get_state(
observation['NAV_X'],
observation['NAV_Y'],
observation['NODE_REPORTS'][self.opponent_id]['NAV_X'],
observation['NODE_REPORTS'][self.opponent_id]['NAV_Y'],
observation['HAS_FLAG']
)
#console.tick(observation) #needs full msg, obervation is an msg.state

return model_representation

def rpr_to_act(self, rpr, observation):
self.current_action = self.q.get_action(rpr)

# Determine action set
if observation['HAS_FLAG']:
current_action_set = self.retreat_actions
else:
current_action_set = self.attack_actions

def run(args):
q, attack_actions, retreat_actions = load_model(args.model)

with MissionManager('runner', log=False) as mgr:
print('Waiting for sim vehicle connections...')
while mgr.get_vehicle_count() < 1:
time.sleep(0.1)
# ---------------------------------------
# Part 1: Asserting simulation state

last_state = None
current_action = None
current_action_set = None
console = ModelConsole()

while True:
# Listen for state
msg = mgr.get_message()
while False:
print('-------------------------------------------')
print(f"({msg.vname}) {msg.state['HAS_FLAG']}")
print('-------------------------------------------')
msg.request_new()
msg = mgr.get_message()

console.tick(msg)

# Detect state transitions
model_state = q.get_state(
msg.state['NAV_X'],
msg.state['NAV_Y'],
msg.state['NODE_REPORTS'][args.enemy]['NAV_X'],
msg.state['NODE_REPORTS'][args.enemy]['NAV_Y'],
msg.state['HAS_FLAG']
)

# Detect state transition
if model_state != last_state:
current_action = q.get_action(model_state)
last_state = model_state

# Determine action set
if msg.state['HAS_FLAG']:
current_action_set = retreat_actions
else:
current_action_set = attack_actions

# Construct instruction for BHV_Agent
action = {
'speed': current_action_set[current_action]['speed'],
'course': current_action_set[current_action]['course']
# Construct instruction for BHV_Agent
action = {
'speed': current_action_set[self.current_action]['speed'],
'course': current_action_set[self.current_action]['course']
}

flag_dist = abs(dist((observation['NAV_X'], observation['NAV_Y']), FIELD_BLUE_FLAG))

if flag_dist < 10:
action['posts']= {
'FLAG_GRAB_REQUEST': f'vname={self.own_id}'
}

flag_dist = abs(dist((msg.state['NAV_X'], msg.state['NAV_Y']), FIELD_BLUE_FLAG))
if flag_dist < 10:
action['posts']= {
'FLAG_GRAB_REQUEST': f'vname={msg.vname}'
}

msg.act(action)
return action


if __name__ == '__main__':
# Create agents required
parser = argparse.ArgumentParser()
parser.add_argument('--model', default=DEFAULT_RUN_MODEL)
parser.add_argument('--enemy', default='drone_21')
args = parser.parse_args()
run(args)

agents = []
wait_for = []
for i in [1, 2, 3]:
agents.append(Agent(f'agent_1{i}', f'drone_2{i}', args.model))

#console = ModelConsole() #where will this live? Do we care? Needs a full msg instead of msg.state
mgr = EpisodicManager(agents, 13, wait_for=wait_for) #13 for 10 full episodes... not ideal
mgr.start('runner')

Loading