def init(self, config: Config): """ if `config.microrts_path` is set, then the script will automatically try to launch a microrts client instance. Otherwise you need to set the `config.height` and `config.this script will just wait to listen to the microrts client """ self.config = config root = ET.parse( os.path.expanduser( os.path.join(self.config.microrts_path, self.config.map_path))).getroot() self.config.height, self.config.width = int(root.get("height")), int( root.get("width")) self.running_first_episode = True self.closed = False # Launch the JVM if not jpype._jpype.isStarted(): registerDomain("ts", alias="tests") registerDomain("ai") jpype.addClassPath( os.path.expanduser( os.path.join(self.config.microrts_path, "microrts.jar"))) jpype.startJVM(convertStrings=False) self.client = self.start_client() # computed properties self.init_properties()
def __init__(self, render_theme=2, frame_skip=0, ai2=microrts_ai.passiveAI, map_path="maps/10x10/basesTwoWorkers10x10.xml"): self.render_theme = render_theme self.frame_skip = frame_skip self.ai2 = ai2 self.map_path = map_path self.microrts_path = os.path.join(gym_microrts.__path__[0], 'microrts') root = ET.parse(os.path.join(self.microrts_path, self.map_path)).getroot() self.height, self.width = int(root.get("height")), int( root.get("width")) # Launch the JVM if not jpype._jpype.isStarted(): registerDomain("ts", alias="tests") registerDomain("ai") jpype.addClassPath(os.path.join(self.microrts_path, "microrts.jar")) jpype.addClassPath(os.path.join(self.microrts_path, "Coac.jar")) jpype.startJVM(convertStrings=False) from rts.units import UnitTypeTable self.real_utt = UnitTypeTable() self.client = self.start_client() self.client.renderTheme = self.render_theme # get the unit type table self.utt = json.loads(str(self.client.sendUTT())) # computed properties self.init_properties()
def __init__(self, num_envs=2, max_steps=2000, render_theme=2, frame_skip=0, ai2s=[microrts_ai.passiveAI, microrts_ai.passiveAI], map_path="maps/10x10/basesTwoWorkers10x10.xml", reward_weight=np.array([0.0, 1.0, 0.0, 0.0, 0.0, 5.0])): assert num_envs == len(ai2s), "for each environment, a microrts ai should be provided" self.num_envs = num_envs self.max_steps = max_steps self.render_theme = render_theme self.frame_skip = frame_skip self.ai2s = ai2s self.map_path = map_path self.reward_weight = reward_weight # read map self.microrts_path = os.path.join(gym_microrts.__path__[0], 'microrts') root = ET.parse(os.path.join(self.microrts_path, self.map_path)).getroot() self.height, self.width = int(root.get("height")), int(root.get("width")) # launch the JVM if not jpype._jpype.isStarted(): registerDomain("ts", alias="tests") registerDomain("ai") jars = [ "microrts.jar", "Coac.jar", "Droplet.jar", "GRojoA3N.jar", "Izanagi.jar", "MixedBot.jar", "RojoBot.jar", "TiamatBot.jar", "UMSBot.jar" # "MindSeal.jar" ] for jar in jars: jpype.addClassPath(os.path.join(self.microrts_path, jar)) jpype.startJVM(convertStrings=False) # start microrts client from rts.units import UnitTypeTable self.real_utt = UnitTypeTable() from ai.rewardfunction import RewardFunctionInterface, WinLossRewardFunction, ResourceGatherRewardFunction, AttackRewardFunction, ProduceWorkerRewardFunction, ProduceBuildingRewardFunction, ProduceCombatUnitRewardFunction, CloserToEnemyBaseRewardFunction self.rfs = JArray(RewardFunctionInterface)([ WinLossRewardFunction(), ResourceGatherRewardFunction(), ProduceWorkerRewardFunction(), ProduceBuildingRewardFunction(), AttackRewardFunction(), ProduceCombatUnitRewardFunction(), # CloserToEnemyBaseRewardFunction(), ]) self.start_client() # computed properties # [num_planes_hp(5), num_planes_resources(5), num_planes_player(5), # num_planes_unit_type(z), num_planes_unit_action(6)] self.num_planes = [5, 5, 3, len(self.utt['unitTypes'])+1, 6] self.observation_space = gym.spaces.Box(low=0.0, high=1.0, shape=(self.height, self.width, sum(self.num_planes)), dtype=np.int32) self.action_space = gym.spaces.MultiDiscrete([ self.height * self.width, 6, 4, 4, 4, 4, len(self.utt['unitTypes']), 7 * 7 ])
import jpype import jpype.imports as jimport # This would be better separated somewhere else # in any case, path to JAR should be defined somewhere more "central" jimport.registerDomain('ch') jpype.addClassPath( "../../java/target/demo-python-java-api-1.0-SNAPSHOT-jar-with-dependencies.jar" ) jpype.startJVM(jpype.get_default_jvm_path(), "-Djava.class.path=%s" % jpype.getClassPath()) from ch.dubernet.demopythonapi.simulation.api import ProtobufAdapter, ProtobufBufferedAdapter from api.protobuf.JumpEvent_pb2 import JumpEvent from api.protobuf.SingEvent_pb2 import SingEvent from api.protobuf.SpeakEvent_pb2 import SpeakEvent from api.protobuf.EventBuffer_pb2 import EventBuffer, EventContainer # Simple method: simply functions to wrap instances def create_event_handler(handler): class ProtobufHandler: def notifyStart(self): if hasattr(handler, "notifyStart"): handler.notifyStart() def notifyEnd(self): if hasattr(handler, "notifyEnd"): handler.notifyEnd()
# Import module import numpy as np import jpype from jpype.imports import registerDomain import jpype.imports registerDomain("ts", alias="tests") registerDomain("ai") from jpype.types import * jpype.addClassPath("/home/costa/Documents/work/go/src/github.com/vwxyzjn/microrts/microrts.jar") # Launch the JVM jpype.startJVM() from java.lang import System print(System.getProperty("java.class.path")) from ts import JNIClient from ai.rewardfunction import ResourceGatherRewardFunction rf = ResourceGatherRewardFunction() jc = JNIClient(rf, "/home/costa/Documents/work/go/src/github.com/vwxyzjn/microrts/", "maps/4x4/baseTwoWorkers4x4.xml")