Commit 260c2416 authored by drallensmith's avatar drallensmith

Merge branch 'randomization' into add_preprocess_action - bring in improvements, avoid rebase

parents 3ec6cfef b06bf087
...@@ -68,8 +68,6 @@ def main(): ...@@ -68,8 +68,6 @@ def main():
help="If doing HFO --record") help="If doing HFO --record")
parser.add_argument('--rdir', type=str, default='log/', parser.add_argument('--rdir', type=str, default='log/',
help="Set directory to use if doing --record") help="Set directory to use if doing --record")
parser.add_argument('--numTeammates', type=int, default=0)
parser.add_argument('--numOpponents', type=int, default=1)
args=parser.parse_args() args=parser.parse_args()
if args.seed: if args.seed:
random.seed(args.seed) random.seed(args.seed)
...@@ -83,12 +81,14 @@ def main(): ...@@ -83,12 +81,14 @@ def main():
hfo_env.connectToServer(hfo.HIGH_LEVEL_FEATURE_SET, hfo_env.connectToServer(hfo.HIGH_LEVEL_FEATURE_SET,
'bin/teams/base/config/formations-dt', args.port, 'bin/teams/base/config/formations-dt', args.port,
'localhost', 'base_left', False) 'localhost', 'base_left', False)
num_teammates = hfo_env.getNumTeammates()
#num_opponents = hfo_env.getNumOpponents()
if args.seed: if args.seed:
if (args.rand_pass and (args.numTeammates > 1)) or (args.epsilon > 0): if (args.rand_pass and (num_teammates > 1)) or (args.epsilon > 0):
print("Python randomization seed: {0:d}".format(args.seed)) print("Python randomization seed: {0:d}".format(args.seed))
else: else:
print("Python randomization seed useless without --rand-pass w/teammates or --epsilon >0") print("Python randomization seed useless without --rand-pass w/2+ teammates or --epsilon >0")
if args.rand_pass and (args.numTeammates > 1): if args.rand_pass and (num_teammates > 1):
print("Randomizing order of checking for a pass") print("Randomizing order of checking for a pass")
if args.epsilon > 0: if args.epsilon > 0:
print("Using epsilon {0:n}".format(args.epsilon)) print("Using epsilon {0:n}".format(args.epsilon))
...@@ -108,7 +108,7 @@ def main(): ...@@ -108,7 +108,7 @@ def main():
hfo_env.act(hfo.DRIBBLE) hfo_env.act(hfo.DRIBBLE)
num_eps += 1 num_eps += 1
else: else:
get_action(state,hfo_env,args.numTeammates,args.rand_pass) get_action(state,hfo_env,num_teammates,args.rand_pass)
num_had_ball += 1 num_had_ball += 1
else: else:
hfo_env.act(hfo.MOVE) hfo_env.act(hfo.MOVE)
......
...@@ -3,9 +3,9 @@ ...@@ -3,9 +3,9 @@
./bin/HFO --offense-agents=2 --defense-npcs=3 --offense-npcs=1 --trials 20 --headless & ./bin/HFO --offense-agents=2 --defense-npcs=3 --offense-npcs=1 --trials 20 --headless &
sleep 5 sleep 5
# -x is needed to skip first line - otherwise whatever default python version is will run # -x is needed to skip first line - otherwise whatever default python version is will run
python2.7 -x ./example/high_level_custom_agent.py --numTeammates=2 --numOpponents=3 --port 6000 &> agent1.txt & python2.7 -x ./example/high_level_custom_agent.py --port 6000 &> agent1.txt &
sleep 5 sleep 5
python3 -x ./example/high_level_custom_agent.py --numTeammates=2 --numOpponents=3 --port 6000 &> agent2.txt & python3 -x ./example/high_level_custom_agent.py --port 6000 &> agent2.txt &
# The magic line # The magic line
# $$ holds the PID for this script # $$ holds the PID for this script
......
...@@ -79,6 +79,10 @@ hfo_lib.statusToString.argtypes = [c_int] ...@@ -79,6 +79,10 @@ hfo_lib.statusToString.argtypes = [c_int]
hfo_lib.statusToString.restype = c_char_p hfo_lib.statusToString.restype = c_char_p
hfo_lib.getUnum.argtypes = [c_void_p] hfo_lib.getUnum.argtypes = [c_void_p]
hfo_lib.getUnum.restype = c_int hfo_lib.getUnum.restype = c_int
hfo_lib.getNumTeammates.argtypes = [c_void_p]
hfo_lib.getNumTeammates.restype = c_int
hfo_lib.getNumOpponents.argtypes = [c_void_p]
hfo_lib.getNumOpponents.restype = c_int
class HFOEnvironment(object): class HFOEnvironment(object):
def __init__(self): def __init__(self):
...@@ -154,3 +158,11 @@ class HFOEnvironment(object): ...@@ -154,3 +158,11 @@ class HFOEnvironment(object):
def getUnum(self): def getUnum(self):
""" Return the uniform number of the agent """ """ Return the uniform number of the agent """
return hfo_lib.getUnum(self.obj) return hfo_lib.getUnum(self.obj)
def getNumTeammates(self):
""" Returns the number of teammates of the agent """
return hfo_lib.getNumTeammates(self.obj)
def getNumOpponents(self):
""" Returns the number of opponents of the agent """
return hfo_lib.getNumOpponents(self.obj)
...@@ -48,6 +48,8 @@ extern "C" { ...@@ -48,6 +48,8 @@ extern "C" {
return StatusToString(status).c_str(); return StatusToString(status).c_str();
} }
int getUnum(hfo::HFOEnvironment *hfo) {return hfo->getUnum();} int getUnum(hfo::HFOEnvironment *hfo) {return hfo->getUnum();}
int getNumTeammates(hfo::HFOEnvironment *hfo) {return hfo->getNumTeammates();}
int getNumOpponents(hfo::HFOEnvironment *hfo) {return hfo->getNumOpponents();}
} }
#endif #endif
...@@ -115,6 +115,14 @@ int HFOEnvironment::getUnum() { ...@@ -115,6 +115,14 @@ int HFOEnvironment::getUnum() {
return agent->getUnum(); return agent->getUnum();
} }
int HFOEnvironment::getNumTeammates() {
return agent->getNumTeammates();
}
int HFOEnvironment::getNumOpponents() {
return agent->getNumOpponents();
}
Player HFOEnvironment::playerOnBall() { Player HFOEnvironment::playerOnBall() {
return agent->getPlayerOnBall(); return agent->getPlayerOnBall();
} }
......
...@@ -49,6 +49,12 @@ class HFOEnvironment { ...@@ -49,6 +49,12 @@ class HFOEnvironment {
// Returns the uniform number of the player // Returns the uniform number of the player
virtual int getUnum(); virtual int getUnum();
// Returns the number of teammates
virtual int getNumTeammates();
// Returns the number of opponents
virtual int getNumOpponents();
// Get the current player holding the ball // Get the current player holding the ball
virtual Player playerOnBall(); virtual Player playerOnBall();
......
...@@ -212,16 +212,16 @@ bool Agent::initImpl(CmdLineParser & cmd_parser) { ...@@ -212,16 +212,16 @@ bool Agent::initImpl(CmdLineParser & cmd_parser) {
} }
FeatureExtractor* Agent::getFeatureExtractor(feature_set_t feature_set_indx, FeatureExtractor* Agent::getFeatureExtractor(feature_set_t feature_set_indx,
int num_teammates, int numTeammates,
int num_opponents, int numOpponents,
bool playing_offense) { bool playing_offense) {
switch (feature_set_indx) { switch (feature_set_indx) {
case LOW_LEVEL_FEATURE_SET: case LOW_LEVEL_FEATURE_SET:
return new LowLevelFeatureExtractor(num_teammates, num_opponents, return new LowLevelFeatureExtractor(numTeammates, numOpponents,
playing_offense); playing_offense);
break; break;
case HIGH_LEVEL_FEATURE_SET: case HIGH_LEVEL_FEATURE_SET:
return new HighLevelFeatureExtractor(num_teammates, num_opponents, return new HighLevelFeatureExtractor(numTeammates, numOpponents,
playing_offense); playing_offense);
break; break;
default: default:
...@@ -339,9 +339,9 @@ Agent::ProcessTrainerMessages() ...@@ -339,9 +339,9 @@ Agent::ProcessTrainerMessages()
hfo::Config hfo_config; hfo::Config hfo_config;
if (hfo::ParseConfig(message, hfo_config)) { if (hfo::ParseConfig(message, hfo_config)) {
bool playing_offense = world().ourSide() == rcsc::LEFT; bool playing_offense = world().ourSide() == rcsc::LEFT;
int num_teammates = playing_offense ? num_teammates = playing_offense ?
hfo_config.num_offense - 1 : hfo_config.num_defense - 1; hfo_config.num_offense - 1 : hfo_config.num_defense - 1;
int num_opponents = playing_offense ? num_opponents = playing_offense ?
hfo_config.num_defense : hfo_config.num_offense; hfo_config.num_defense : hfo_config.num_offense;
feature_extractor = getFeatureExtractor( feature_extractor = getFeatureExtractor(
feature_set, num_teammates, num_opponents, playing_offense); feature_set, num_teammates, num_opponents, playing_offense);
......
...@@ -61,6 +61,8 @@ protected: ...@@ -61,6 +61,8 @@ protected:
std::string say_msg, hear_msg; // Messages to/from teammates std::string say_msg, hear_msg; // Messages to/from teammates
hfo::action_t requested_action; // Currently requested action hfo::action_t requested_action; // Currently requested action
std::vector<float> params; // Parameters of current action std::vector<float> params; // Parameters of current action
int num_teammates; // Number of teammates
int num_opponents; // Number of opponents
public: public:
inline const std::vector<float>& getState() { return state; } inline const std::vector<float>& getState() { return state; }
...@@ -68,6 +70,8 @@ protected: ...@@ -68,6 +70,8 @@ protected:
inline const hfo::Player& getPlayerOnBall() { return player_on_ball; } inline const hfo::Player& getPlayerOnBall() { return player_on_ball; }
inline const std::string& getHearMsg() { return hear_msg; } inline const std::string& getHearMsg() { return hear_msg; }
int getUnum(); // Returns the uniform number of the player int getUnum(); // Returns the uniform number of the player
inline int getNumTeammates() { return num_teammates; }
inline int getNumOpponents() { return num_opponents; }
inline void setFeatureSet(hfo::feature_set_t fset) { feature_set = fset; } inline void setFeatureSet(hfo::feature_set_t fset) { feature_set = fset; }
inline std::vector<float>* mutable_params() { return &params; } inline std::vector<float>* mutable_params() { return &params; }
......
...@@ -53,6 +53,12 @@ def test_with_server(): ...@@ -53,6 +53,12 @@ def test_with_server():
print("My unum is {!s}".format(my_unum)) print("My unum is {!s}".format(my_unum))
num_teammates = hfo_env.getNumTeammates()
assert (num_teammates == 2), "Wrong num teammates ({!r})".format(num_teammates)
num_opponents = hfo_env.getNumOpponents()
assert (num_opponents == 2), "Wrong num opponents ({!r})".format(num_opponents)
had_ok_unum = False had_ok_unum = False
had_ok_unum_set_my_side = set() had_ok_unum_set_my_side = set()
had_ok_unum_set_their_side = set(); had_ok_unum_set_their_side = set();
......
...@@ -54,6 +54,12 @@ def test_with_server(): ...@@ -54,6 +54,12 @@ def test_with_server():
print("My unum is {!s}".format(my_unum)) print("My unum is {!s}".format(my_unum))
num_teammates = hfo_env.getNumTeammates()
assert (num_teammates == 2), "Wrong num teammates ({!r})".format(num_teammates)
num_opponents = hfo_env.getNumOpponents()
assert (num_opponents == 2), "Wrong num opponents ({!r})".format(num_opponents)
had_ok_unum = False had_ok_unum = False
had_ok_unum_set_my_side = set() had_ok_unum_set_my_side = set()
had_ok_unum_set_their_side = set(); had_ok_unum_set_their_side = set();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment