<s> """<STR_LIT>""" <EOL> from rlpy . Tools import plt , id2vec , bound_vec <EOL> import numpy as np <EOL> from . Domain import Domain <EOL> import os <EOL> from rlpy . Tools import __rlpy_location__ , FONTSIZE <EOL> __copyright__ = "<STR_LIT>" <EOL> __credits__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> __license__ = "<STR_LIT>" <EOL> __author__ = "<STR_LIT>" <EOL> class IntruderMonitoring ( Domain ) : <EOL> """<STR_LIT>""" <EOL> map = None <EOL> ROWS = COLS = <NUM_LIT:0> <EOL> NUMBER_OF_AGENTS = <NUM_LIT:0> <EOL> NUMBER_OF_INTRUDERS = <NUM_LIT:0> <EOL> NUMBER_OF_DANGER_ZONES = <NUM_LIT:0> <EOL> discount_factor = <NUM_LIT> <EOL> INTRUSION_PENALTY = - <NUM_LIT:1.0> <EOL> episodeCap = <NUM_LIT:100> <EOL> EMPTY , INTRUDER , AGENT , DANGER = xrange ( <NUM_LIT:4> ) <EOL> ACTIONS_PER_AGENT = np . array ( [ [ - <NUM_LIT:1> , <NUM_LIT:0> ] , [ + <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , - <NUM_LIT:1> ] , [ <NUM_LIT:0> , + <NUM_LIT:1> ] , [ <NUM_LIT:0> , <NUM_LIT:0> ] , ] ) <EOL> domain_fig = None <EOL> ally_fig = None <EOL> intruder_fig = None <EOL> default_map_dir = os . path . join ( <EOL> __rlpy_location__ , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> def __init__ ( self , mapname = os . path . join ( <EOL> default_map_dir , "<STR_LIT>" ) ) : <EOL> self . setupMap ( mapname ) <EOL> self . state_space_dims = <NUM_LIT:2> * ( self . NUMBER_OF_AGENTS + self . NUMBER_OF_INTRUDERS ) <EOL> _statespace_limits = np . vstack ( <EOL> [ [ <NUM_LIT:0> , self . ROWS - <NUM_LIT:1> ] , [ <NUM_LIT:0> , self . COLS - <NUM_LIT:1> ] ] ) <EOL> self . statespace_limits = np . tile ( <EOL> _statespace_limits , ( ( self . NUMBER_OF_AGENTS + self . NUMBER_OF_INTRUDERS ) , <NUM_LIT:1> ) ) <EOL> self . actions_num = <NUM_LIT:5> ** self . NUMBER_OF_AGENTS <EOL> self . ACTION_LIMITS = [ <NUM_LIT:5> ] * self . NUMBER_OF_AGENTS <EOL> self . DimNames = [ ] <EOL> super ( IntruderMonitoring , self ) . __init__ ( ) <EOL> def setupMap ( self , mapname ) : <EOL> self . map = np . loadtxt ( mapname , dtype = np . uint8 ) <EOL> if self . map . ndim == <NUM_LIT:1> : <EOL> self . map = self . map [ np . newaxis , : ] <EOL> self . ROWS , self . COLS = np . shape ( self . map ) <EOL> R , C = ( self . map == self . AGENT ) . nonzero ( ) <EOL> self . agents_initial_locations = np . vstack ( [ R , C ] ) . T <EOL> self . NUMBER_OF_AGENTS = len ( self . agents_initial_locations ) <EOL> R , C = ( self . map == self . INTRUDER ) . nonzero ( ) <EOL> self . intruders_initial_locations = np . vstack ( [ R , C ] ) . T <EOL> self . NUMBER_OF_INTRUDERS = len ( self . intruders_initial_locations ) <EOL> R , C = ( self . map == self . DANGER ) . nonzero ( ) <EOL> self . danger_zone_locations = np . vstack ( [ R , C ] ) . T <EOL> self . NUMBER_OF_DANGER_ZONES = len ( self . danger_zone_locations ) <EOL> def step ( self , a ) : <EOL> """<STR_LIT>""" <EOL> s = self . state <EOL> agents = np . array ( s [ : self . NUMBER_OF_AGENTS * <NUM_LIT:2> ] . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> actions = id2vec ( a , self . ACTION_LIMITS ) <EOL> actions = self . ACTIONS_PER_AGENT [ actions ] <EOL> agents += actions <EOL> intruders = np . array ( s [ self . NUMBER_OF_AGENTS * <NUM_LIT:2> : ] . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> actions = [ self . IntruderPolicy ( intruders [ i ] ) <EOL> for i in xrange ( self . NUMBER_OF_INTRUDERS ) ] <EOL> actions = self . ACTIONS_PER_AGENT [ actions ] <EOL> intruders += actions <EOL> ns = np . hstack ( ( agents . ravel ( ) , intruders . ravel ( ) ) ) <EOL> ns = bound_vec ( ns , self . discrete_statespace_limits ) <EOL> agents = ns [ : self . NUMBER_OF_AGENTS * <NUM_LIT:2> ] . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> intruders = ns [ self . NUMBER_OF_AGENTS * <NUM_LIT:2> : ] . reshape ( - <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> map = np . zeros ( ( self . ROWS , self . COLS ) , '<STR_LIT:bool>' ) <EOL> map [ intruders [ : , <NUM_LIT:0> ] , intruders [ : , <NUM_LIT:1> ] ] = True <EOL> map [ agents [ : , <NUM_LIT:0> ] , agents [ : , <NUM_LIT:1> ] ] = False <EOL> intrusion_counter = np . count_nonzero ( <EOL> map [ self . danger_zone_locations [ : , <NUM_LIT:0> ] , self . danger_zone_locations [ : , <NUM_LIT:1> ] ] ) <EOL> r = intrusion_counter * self . INTRUSION_PENALTY <EOL> ns = bound_vec ( ns , self . discrete_statespace_limits ) <EOL> self . state = ns . copy ( ) <EOL> return r , ns , False , self . possibleActions ( ) <EOL> def s0 ( self ) : <EOL> self . state = np . hstack ( <EOL> [ self . agents_initial_locations . ravel ( ) , <EOL> self . intruders_initial_locations . ravel ( ) ] ) <EOL> return self . state . copy ( ) , self . isTerminal ( ) , self . possibleActions ( ) <EOL> def possibleActionsPerAgent ( self , s_i ) : <EOL> """<STR_LIT>""" <EOL> tile_s = np . tile ( s_i , [ len ( self . ACTIONS_PER_AGENT ) , <NUM_LIT:1> ] ) <EOL> next_states = tile_s + self . ACTIONS_PER_AGENT <EOL> next_states_rows = next_states [ : , <NUM_LIT:0> ] <EOL> next_states_cols = next_states [ : , <NUM_LIT:1> ] <EOL> possibleActions1 = np . logical_and ( <EOL> <NUM_LIT:0> <= next_states_rows , <EOL> next_states_rows < self . ROWS ) <EOL> possibleActions2 = np . logical_and ( <EOL> <NUM_LIT:0> <= next_states_cols , <EOL> next_states_cols < self . COLS ) <EOL> possibleActions , _ = np . logical_and ( <EOL> possibleActions1 , possibleActions2 ) . reshape ( - <NUM_LIT:1> , <NUM_LIT:1> ) . nonzero ( ) <EOL> return possibleActions <EOL> def printDomain ( self , s , a ) : <EOL> print '<STR_LIT>' <EOL> for i in xrange ( <NUM_LIT:0> , self . NUMBER_OF_AGENTS ) : <EOL> s_a = s [ i * <NUM_LIT:2> : i * <NUM_LIT:2> + <NUM_LIT:2> ] <EOL> aa = id2vec ( a , self . ACTION_LIMITS ) <EOL> print '<STR_LIT>' . format ( i , s_a , aa ) <EOL> offset = <NUM_LIT:2> * self . NUMBER_OF_AGENTS <EOL> for i in xrange ( <NUM_LIT:0> , self . NUMBER_OF_INTRUDERS ) : <EOL> s_i = s [ offset + i * <NUM_LIT:2> : offset + i * <NUM_LIT:2> + <NUM_LIT:2> ] <EOL> print '<STR_LIT>' , s_i <EOL> r , ns , terminal = self . step ( s , a ) <EOL> print '<STR_LIT>' , r <EOL> def IntruderPolicy ( self , s_i ) : <EOL> """<STR_LIT>""" <EOL> return self . random_state . choice ( self . possibleActionsPerAgent ( s_i ) ) <EOL> def showDomain ( self , a ) : <EOL> s = self . state <EOL> if self . domain_fig is None : <EOL> self . domain_fig = plt . imshow ( <EOL> self . map , <EOL> cmap = '<STR_LIT>' , <EOL> interpolation = '<STR_LIT>' , <EOL> vmin = <NUM_LIT:0> , <EOL> vmax = <NUM_LIT:3> ) <EOL> plt . xticks ( np . arange ( self . COLS ) , fontsize = FONTSIZE ) <EOL> plt . yticks ( np . arange ( self . ROWS ) , fontsize = FONTSIZE ) <EOL> plt . show ( ) <EOL> if self . ally_fig is not None : <EOL> self . ally_fig . pop ( <NUM_LIT:0> ) . remove ( ) <EOL> self . intruder_fig . pop ( <NUM_LIT:0> ) . remove ( ) <EOL> s_ally = s [ <NUM_LIT:0> : self . NUMBER_OF_AGENTS * <NUM_LIT:2> ] . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> s_intruder = s [ self . NUMBER_OF_AGENTS * <NUM_LIT:2> : ] . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> self . ally_fig = plt . plot ( <EOL> s_ally [ : , <EOL> <NUM_LIT:1> ] , <EOL> s_ally [ : , <EOL> <NUM_LIT:0> ] , <EOL> '<STR_LIT>' , <EOL> markersize = <NUM_LIT> , <EOL> alpha = <NUM_LIT> , <EOL> markeredgecolor = '<STR_LIT:k>' , <EOL> markeredgewidth = <NUM_LIT:2> ) <EOL> self . intruder_fig = plt . plot ( <EOL> s_intruder [ : , <EOL> <NUM_LIT:1> ] , <EOL> s_intruder [ : , <EOL> <NUM_LIT:0> ] , <EOL> '<STR_LIT>' , <EOL> color = '<STR_LIT>' , <EOL> markersize = <NUM_LIT> , <EOL> alpha = <NUM_LIT> , <EOL> markeredgecolor = '<STR_LIT:k>' , <EOL> markeredgewidth = <NUM_LIT:2> ) <EOL> plt . draw ( ) </s>
