skjb / pysc2-tutorial Goto Github PK
View Code? Open in Web Editor NEWTutorials for building a PySC2 bot
Home Page: https://medium.com/@skjb/building-a-basic-pysc2-agent-b109cde1477c
License: MIT License
Tutorials for building a PySC2 bot
Home Page: https://medium.com/@skjb/building-a-basic-pysc2-agent-b109cde1477c
License: MIT License
Hello!
First of all I want to thank you for your tutorial which really gives me a lot help, I learned a lot through your code.
I am currently working on the Q-learning code, and I see that you used Raw_function instead of actions.FUNCTIONS in the code. I did some research but unfortunately I didn't find the difference between them. I seems to me that the origin actions.FUNCTIONS that you used in those basic agent programs are more similar to real world operations, while the utilisation of those RAW_FUNCTIONS seems to be much easier and a little bit tricky. Am I understanding right ? To simulate human operation in the game, which one should be the better choice ?
Thanks! :)
@skjb I would love to contribute to this project - I am working on tutorials for getting set up with the SC2LE and for making a basic Protoss agent. Are you open to PRs?
I found one error in zerg_agent_step5.
At line 48, need to change
larvae = self.get_units_by_type(oba, units.Zerg.Larva)
to
larvae = self.get_units_by_type(obs, units.Zerg.Larva)
thank you for your job
player_y, player_x = (obs.observation["minimap"][_PLAYER_RELATIVE] == _PLAYER_SELF).nonzero()
KeyError: 'minimap'
Why does it happen and how can I fix it...?
I'm trying to run the final zerg bot (zerg_agent_step7.py) through the command:
python3 -m pysc2.bin.agent --map Simple64 --agent zergbot.ZergAgent
and I got the error:
zerglings = self.get_units_by_type(obs, units.Zerg.Zergling) File "/Users/xx/Desktop/xx/xx/pysc2-tutorial/zergbot.py", line 25, in get_units_by_type return [unit for unit in obs.observation.feature_units AttributeError: 'NamedDict' object has no attribute 'feature_units'
a lot of names have been changed. Such as obs.observation["minimap"] to obs.observation["feature_minimap"]. Similarly obs.observation["screen"] to obs.observation["feature_screen"]. Im only starting your tutorial so I dont know of other name changes but Im guessing there are others. The version of pyc2 im using is 2.0.1
Thanks for the great tutorial!
In file zerg_agent_step7.py
It seems that Attack_minimap function is not working
OS- Ubuntu 18.04
SC2- 3.1.7
Error log:
.local/lib/python2.7/site-packages/pysc2/lib/features.py", line 1020, in func_call_ability
ability_id, cmd_type.name))
ValueError: Unknown ability_id: 3674, type: cmd_quick. Likely a bug.
After i run the agent for a while i randomly get the following:
I1020 12:20:54.603614 12624 sc2_env.py:310] Episode finished. Outcome: [-1], reward: [-1], score: [1410]
I1020 12:21:01.876163 12624 sc2_env.py:240] Starting episode: 6
Took 368.270 seconds for 9110 steps: 24.737 fps
I1020 12:21:11.313090 12624 sc2_env.py:327] Environment Close
I1020 12:21:11.315090 12624 sc2_env.py:342]
Traceback (most recent call last):
File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "C:\Python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python36\lib\site-packages\pysc2\bin\agent.py", line 112, in
app.run(main)
File "C:\Python36\lib\site-packages\absl\app.py", line 272, in run
_run_main(main, argv)
File "C:\Python36\lib\site-packages\absl\app.py", line 237, in _run_main
sys.exit(main(argv))
File "C:\Python36\lib\site-packages\pysc2\bin\agent.py", line 98, in main
run_thread(agent_cls, FLAGS.map, FLAGS.render)
File "C:\Python36\lib\site-packages\pysc2\bin\agent.py", line 77, in run_thread
run_loop.run_loop([agent], env, FLAGS.max_agent_steps)
File "C:\Python36\lib\site-packages\pysc2\env\run_loop.py", line 41, in run_loop
for agent, timestep in zip(agents, timesteps)]
File "C:\Python36\lib\site-packages\pysc2\env\run_loop.py", line 41, in
for agent, timestep in zip(agents, timesteps)]
File "C:\Python36\Lib\site-packages\pysc2\agents\smart_agent.py", line 177, in step
i = random.randrange(0, len(unit_y) - 1)
File "C:\Python36\lib\random.py", line 198, in randrange
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0,0, 0)
I1020 12:21:11.364095 12624 sc2_env.py:327] Environment Close
I1020 12:21:11.365095 12624 sc2_env.py:342]
I1020 12:21:11.367095 12624 sc2_env.py:327] Environment Close
I1020 12:21:11.375099 12624 sc2_env.py:342]
W1020 12:21:14.483033 12624 sc_process.py:183] Killing the process.
I1020 12:21:15.275633 12624 sc_process.py:166] Shutdown with return code: 1
Nesting/relocating the drone selection block in the spawning pool conditional body was not mentioned in the tutorial, and visually unclear from code.
While it makes sense we wouldn't want to keep selecting drones, with 2-space indentations, lots of wrapped lines, and no mention, if you're not copy pasting the tutorial code, it's possible you might miss the change. I think it would be nice to make that change more explicit.
spawning_pools = self.get_units_by_type(obs, units.Zerg.SpawningPool)
if len(spawning_pools) == 0:
if self.unit_type_is_selected(obs, units.Zerg.Drone):
if (actions.FUNCTIONS.Build_SpawningPool_screen.id in
obs.observation.available_actions):
x = random.randint(0, 83)
y = random.randint(0, 83)
return actions.FUNCTIONS.Build_SpawningPool_screen("now", (x, y))
drones = self.get_units_by_type(obs, units.Zerg.Drone)
if len(drones) > 0:
drone = random.choice(drones)
return actions.FUNCTIONS.select_point("select_all_type", (drone.x,
drone.y))
if self.unit_type_is_selected(obs, units.Zerg.Larva):
if (actions.FUNCTIONS.Train_Zergling_quick.id in
obs.observation.available_actions):
return actions.FUNCTIONS.Train_Zergling_quick("now")
larvae = self.get_units_by_type(oba, units.Zerg.Larva)
if len(larvae) > 0:
larva = random.choice(larvae)
return actions.FUNCTIONS.select_point("select_all_type", (larva.x,
larva.y))
Typo in file zerg_agent_step5.py on line 48 and in file zerg_agent_step6.py
on line 55
larvae = self.get_units_by_type(oba, units.Zerg.Larva)
it should be obs
larvae = self.get_units_by_type(obs, units.Zerg.Larva)
Running this on Mac. I get this error. Any ideas? thanks
I1216 13:36:12.326220 4320764736 sc2_env.py:200] Environment is ready. I1216 13:36:12.352599 4320764736 sc2_env.py:240] Starting episode: 1 0/no_op () 1/move_camera (1/minimap [64, 64]) 2/select_point (6/select_point_act [4]; 0/screen [84, 84]) 3/select_rect (7/select_add [2]; 0/screen [84, 84]; 2/screen2 [84, 84]) 4/select_control_group (4/control_group_act [5]; 5/control_group_id [10]) 264/Harvest_Gather_screen (3/queued [2]; 0/screen [84, 84]) 12/Attack_screen (3/queued [2]; 0/screen [84, 84]) 13/Attack_minimap (3/queued [2]; 1/minimap [64, 64]) 274/HoldPosition_quick (3/queued [2]) 451/Smart_screen (3/queued [2]; 0/screen [84, 84]) 452/Smart_minimap (3/queued [2]; 1/minimap [64, 64]) 453/Stop_quick (3/queued [2]) 331/Move_screen (3/queued [2]; 0/screen [84, 84]) 332/Move_minimap (3/queued [2]; 1/minimap [64, 64]) 333/Patrol_screen (3/queued [2]; 0/screen [84, 84]) 334/Patrol_minimap (3/queued [2]; 1/minimap [64, 64]) 79/Build_Refinery_screen (3/queued [2]; 0/screen [84, 84]) 91/Build_SupplyDepot_screen (3/queued [2]; 0/screen [84, 84]) 220/Effect_Repair_screen (3/queued [2]; 0/screen [84, 84]) 221/Effect_Repair_autocast () 230/Effect_Spray_screen (3/queued [2]; 0/screen [84, 84]) 269/Harvest_Return_quick (3/queued [2]) 50/Build_EngineeringBay_screen (3/queued [2]; 0/screen [84, 84]) Took 1.550 seconds for 34 steps: 21.942 fps I1216 13:36:13.902295 4320764736 sc2_env.py:327] Environment Close I1216 13:36:13.902536 4320764736 sc2_env.py:342] Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code exec(code, run_globals) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/bin/agent.py", line 112, in <module> app.run(main) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/absl/app.py", line 274, in run _run_main(main, argv) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/absl/app.py", line 238, in _run_main sys.exit(main(argv)) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/bin/agent.py", line 98, in main run_thread(agent_cls, FLAGS.map, FLAGS.render) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/bin/agent.py", line 77, in run_thread run_loop.run_loop([agent], env, FLAGS.max_agent_steps) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/env/run_loop.py", line 46, in run_loop timesteps = env.step(actions) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/env/available_actions_printer.py", line 32, in step all_obs = super(AvailableActionsPrinter, self).step(*args, **kwargs) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/env/base_env_wrapper.py", line 42, in step return self._env.step(*args, **kwargs) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/lib/stopwatch.py", line 197, in _stopwatch return func(*args, **kwargs) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/env/sc2_env.py", line 254, in step for c, o, a in zip(self._controllers, self._obs, actions)) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/lib/run_parallel.py", line 54, in run funcs = [f if callable(f) else functools.partial(*f) for f in funcs] File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/lib/run_parallel.py", line 54, in <listcomp> funcs = [f if callable(f) else functools.partial(*f) for f in funcs] File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/env/sc2_env.py", line 254, in <genexpr> for c, o, a in zip(self._controllers, self._obs, actions)) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/lib/stopwatch.py", line 197, in _stopwatch return func(*args, **kwargs) File "/Users/craigh/python/virtual-env3/sc2ai-2/lib/python3.6/site-packages/pysc2/lib/features.py", line 427, in transform_action func, func_call.arguments)) ValueError: Argument is out of range for 91/Build_SupplyDepot_screen (3/queued [2]; 0/screen [0, 0]), got: [[0], [48, 98]] I1216 13:36:13.946142 4320764736 sc2_env.py:327] Environment Close I1216 13:36:13.946537 4320764736 sc2_env.py:342] I1216 13:36:13.946666 4320764736 sc2_env.py:327] Environment Close I1216 13:36:13.946901 4320764736 sc2_env.py:342] W1216 13:36:17.142264 4320764736 sc_process.py:183] Killing the process. I1216 13:36:17.427438 4320764736 sc_process.py:166] Shutdown with return code: -9
I am fresh to this, thank you very much.
I'm trying to run the script but I'm getting this error.
I1208 14:55:06.231405 6620 sc2_env.py:200] Environment is ready.
I1208 14:55:06.232910 6620 sc2_env.py:240] Starting episode: 1
Took 0.063 seconds for 1 steps: 15.957 fps
I1208 14:55:06.295577 6620 sc2_env.py:327] Environment Close
I1208 14:55:06.295577 6620 sc2_env.py:342]
Traceback (most recent call last):
File "C:\Programy\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "C:\Programy\Python\Python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Programy\Python\Python36\lib\site-packages\pysc2\bin\agent.py", line 112, in
app.run(main)
File "C:\Programy\Python\Python36\lib\site-packages\absl\app.py", line 274, in run
_run_main(main, argv)
File "C:\Programy\Python\Python36\lib\site-packages\absl\app.py", line 238, in _run_main
sys.exit(main(argv))
File "C:\Programy\Python\Python36\lib\site-packages\pysc2\bin\agent.py", line 98, in main
run_thread(agent_cls, FLAGS.map, FLAGS.render)
File "C:\Programy\Python\Python36\lib\site-packages\pysc2\bin\agent.py", line 77, in run_thread
run_loop.run_loop([agent], env, FLAGS.max_agent_steps)
File "C:\Programy\Python\Python36\lib\site-packages\pysc2\env\run_loop.py", line 41, in run_loop
for agent, timestep in zip(agents, timesteps)]
File "C:\Programy\Python\Python36\lib\site-packages\pysc2\env\run_loop.py", line 41, in
for agent, timestep in zip(agents, timesteps)]
File "I:\Starcraft 2 Projects\SmartBot\SMBot.py", line 177, in step
rl_action = self.qlearn.choose_action(str(current_state))
File "I:\Starcraft 2 Projects\SmartBot\SMBot.py", line 80, in choose_action
action = state_action.idxmax()
File "C:\Programy\Python\Python36\lib\site-packages\pandas\core\series.py", line 1357, in idxmax
i = nanops.nanargmax(_values_from_object(self), skipna=skipna)
File "C:\Programy\Python\Python36\lib\site-packages\pandas\core\nanops.py", line 74, in _f
raise TypeError(msg.format(name=f.name.replace('nan', '')))
TypeError: reduction operation 'argmax' not allowed for this dtype
I1208 14:55:06.309614 6620 sc2_env.py:327] Environment Close
I1208 14:55:06.309614 6620 sc2_env.py:342]
I1208 14:55:06.310115 6620 sc2_env.py:327] Environment Close
I1208 14:55:06.310115 6620 sc2_env.py:342]
W1208 14:55:09.418379 6620 sc_process.py:183] Killing the process.
I1208 14:55:09.544715 6620 sc_process.py:166] Shutdown with return code: 1
When I used the latest code from https://github.com/skjb/pysc2-tutorial/blob/master/Building%20a%20Basic%20Agent/simple_agent.py, the first Marine would rally across the map alone. Then a few extra Marines would get built, but never attack. I fixed this by changing a single line:
elif obs.observation["player"][_SUPPLY_USED] == obs.observation["player"][_SUPPLY_MAX] and not self.army_rallied:
It's possible I have an error, but I can't see what it might be. Before I made the change, I noticed that the barracks often gets deselected, and Marines sometimes wouldn't build until I selected it manually. My understanding was/is that "train_marine_quick" should appear in obs.observation["available_actions"] even if the rax isn't selected. Is that correct?
@skjb Would you at all be interested in an updated version of refined_agent.py for the PySC2.0?
Would you mind adding an Open Source License? I don't want to use these examples in my open source bot without proper permision :)
Hey Steve, big fan of your PySC2 tutorials. Couldn't find your contact information online so resorting to a GitHub issue. I have my own RL company Yuri.ai and am looking for like minded people to talk to and share ideas with. Would really appreciate it if you have the time for a phone call sometime before the holidays
Have u saw this error printed ?
i m using pysc 2.0.1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.