コード例 #1
0
    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()
コード例 #2
0
def generate(content, engine, name):
    #dir_name,uuidx = Util.set_dir()
    dir_tmp = tempfile.TemporaryDirectory()
    uuidx = uuid.uuid4().__str__()
    dir_name = dir_tmp.name
    swagger_file_path = os.path.join(dir_name, uuidx)
    swagger_file = open(swagger_file_path, "w+")
    swagger_file.write(content)
    swagger_file.close()
    #startJVM(getDefaultJVMPath(),"-ea", "-Djava.class.path=halo-flask-openapi-generator.jar;openapi-generator-cli.jar")
    if not jpype.isJVMStarted():
        #@todo make sure running directory is project root
        jars_dir = os.path.join(settings.BASE_DIR, '..', 'jars')
        jars_path = jars_dir + '/*'
        #jar_dir = os.path.join(os.getcwd(), 'jars')
        print("starting jvm:" + jars_path)
        startJVM("-ea", classpath=[jars_path], convertStrings=False)
    print(java.lang.System.getProperty('java.class.path'))
    addClassPath("org/openapitools/codegen")
    #from org.openapitools.codegen import OpenAPIGenerator
    jpype.JClass("org.openapitools.codegen.OpenAPIGenerator").main([
        'generate', '-g', engine, '-i', swagger_file_path, "-o", dir_name,
        "--package-name", name
    ])
    print("finished generate")
    return Util.build_result(dir_name, name)
コード例 #3
0
def startJVM():
    jars = os.path.join(os.path.dirname(os.path.realpath(__file__)), "libs")
    classpath = f'{str.join(os.pathsep, [os.path.join(jars, entry) for entry in os.listdir(jars)])}'
    if not jpype.isJVMStarted():
        jpype.startJVM(classpath = classpath)
    else:
        jpype.addClassPath(classpath)
コード例 #4
0
 def __init__(self):
     self.pdfbox_path = self._get_pdfbox_path()
     jpype.addClassPath(self.pdfbox_path)
     if not jpype.isJVMStarted():
         jpype.startJVM(convertStrings=False)
     import org.apache.pdfbox.tools as tools
     self.pdfbox_tools = tools
コード例 #5
0
 def add_jar_class_path(self, dir_or_jar):
     try:
         if os.path.isdir(dir_or_jar):
             jpype.addClassPath(os.path.join(dir_or_jar, "*"))
         elif os.path.splitext(dir_or_jar)[-1] == '.jar':
             jpype.addClassPath(dir_or_jar)
     except Exception as ex:
         raise NameError("Error adding class path: {}".format(ex))
コード例 #6
0
 def startJVM(self):
     jpype.addClassPath(
         'HWdevices/PSI_java/lib/jar/bioreactor-commander-0.8.7.jar')
     jpype.startJVM(
         jvmpath=jpype.getDefaultJVMPath(),
         convertStrings=False,
         classpath=
         "HWdevices/PSI_java/lib/jar/bioreactor-commander-0.8.7.jar")
コード例 #7
0
 def setUp(self):
     root = path.dirname(path.abspath(path.dirname(__file__)))
     jpype.addClassPath(path.join(root, 'classes'))
     jvm_path = jpype.getDefaultJVMPath()
     classpath_arg = "-Djava.class.path=%s"
     classpath_arg %= jpype.getClassPath()
     jpype.startJVM(jvm_path, "-ea",
                    # "-Xcheck:jni",
                    "-Xmx256M", "-Xms16M", classpath_arg)
コード例 #8
0
ファイル: client.py プロジェクト: crucial-project/dso
    def __init__(self,server="127.0.0.1:11222"):

        dir = os.path.dirname(os.path.abspath(__file__))
        classpath=dir+"/java/*"
        jpype.addClassPath(classpath)
        startJVM(getDefaultJVMPath(), "-ea", convertStrings=False)

        self.Package = JPackage("org.crucial.dso")
        self.client = self.Package.client.Client.getClient(server)
コード例 #9
0
ファイル: pdfbox_test.py プロジェクト: sangwook236/SWDT
def initialize_java_vm(class_path=None):
	if not jpype.isJVMStarted():
		try:
			if class_path: jpype.addClassPath(class_path)
			jpype.startJVM(convertStrings=False)
		except TypeError as ex:
			print("TypeError raised: {}.".format(ex))
		except OSError as ex:
			print("OSError raised: {}.".format(ex))
コード例 #10
0
    def testAddClassPath(self):
        import pathlib
        import org.jpype as ojp
        self.assertFalse("late" in dir(ojp))
        with self.assertRaises(ImportError):
            import org.jpype.late as late

        jpype.addClassPath(pathlib.Path("test/jar/late/late.jar").absolute())
        import org.jpype.late as late
        self.assertTrue("Test" in dir(late))
        t = late.Test()
        self.assertTrue(t.field == 5)
        self.assertTrue(t.method() == "Yes")
コード例 #11
0
ファイル: core.py プロジェクト: jiaxy/EHAdvisor
def run_jar(project_folder: str,
            get_features_jar: str,
            jvm_path: Optional[str] = None):
    """调用 getFeatures.jar 生成 features.csv, comments.csv, link.csv"""
    if jvm_path is None:
        jvm_path = jpype.getDefaultJVMPath()
    abs_jar_path = os.path.abspath(get_features_jar)
    jpype.startJVM(jvm_path, '-ea', f'-Djava.class.path={abs_jar_path}')
    jpype.addClassPath(abs_jar_path)  # 手动添加 classpath
    java_class = jpype.JClass('com.tcl.App')
    abs_proj_path = os.path.abspath(project_folder)
    java_class.getFeatures(abs_proj_path)
    java_class.getComments(abs_proj_path)
    jpype.shutdownJVM()
コード例 #12
0
    def start_jvm(self):
        self.lock.acquire()
        if not self.is_jvm_started():
            jpype.addClassPath(
                'custom/devices/PSI/java/lib/jar/bioreactor-commander-0.8.7.jar'
            )
            jpype.startJVM(
                jvmpath=jpype.getDefaultJVMPath(),
                convertStrings=False,
                classpath=
                "custom/devices/PSI/java/lib/jar/bioreactor-commander-0.8.7.jar"
            )

        self.lock.release()
コード例 #13
0
ファイル: common.py プロジェクト: morganics/jpype
 def setUp(self):
     if not jpype.isJVMStarted():
         root = path.dirname(path.abspath(path.dirname(__file__)))
         jpype.addClassPath(path.join(root, 'classes'))
         jvm_path = jpype.getDefaultJVMPath()
         logger = logging.getLogger(__name__)
         logger.info("Running testsuite using JVM %s" % jvm_path)
         classpath_arg = "-Djava.class.path=%s"
         classpath_arg %= jpype.getClassPath()
         jpype.startJVM(jvm_path, "-ea",
                        # "-Xcheck:jni",
                        "-Xmx256M", "-Xms16M", classpath_arg)
     self.jpype = jpype.JPackage('jpype')
     if sys.version < '3':
         self.assertCountEqual = self.assertItemsEqual
コード例 #14
0
ファイル: common.py プロジェクト: originell/jpype
 def setUp(self):
     if not jpype.isJVMStarted():
         root = path.dirname(path.abspath(path.dirname(__file__)))
         jpype.addClassPath(path.join(root, 'classes'))
         jvm_path = jpype.getDefaultJVMPath()
         logger = logging.getLogger(__name__)
         logger.info("Running testsuite using JVM %s" % jvm_path)
         classpath_arg = "-Djava.class.path=%s"
         classpath_arg %= jpype.getClassPath()
         jpype.startJVM(jvm_path, "-ea",
                        # "-Xcheck:jni",
                        "-Xmx256M", "-Xms16M", classpath_arg)
     self.jpype = jpype.JPackage('jpype')
     if sys.version < '3':
         self.assertCountEqual = self.assertItemsEqual
コード例 #15
0
ファイル: _main.py プロジェクト: hofmann-tobias/swissREFRAME
    def __init__(self,
                 path_reframeLib_jar: str = r'jar/reframeLib.jar',
                 path_jvm_dll: str = r''):

        self._path_lib = path_reframeLib_jar
        self._path_jvm = path_jvm_dll
        self._lib = None

        jpype.addClassPath(_get_path(self._path_lib))

        if self._path_jvm == '' and not jpype.isJVMStarted():
            jpype.startJVM(jpype.getDefaultJVMPath(),
                           "-ea",
                           convertStrings=False)
        elif not jpype.isJVMStarted():
            jpype.startJVM(self._path_jvm, "-ea", convertStrings=False)

        javaPackage = jpype.JPackage('com.swisstopo.geodesy.reframe_lib')
        jReframe = jpype.JClass('com.swisstopo.geodesy.reframe_lib.Reframe')
        jPlanimetricFrame = javaPackage.IReframe.PlanimetricFrame
        jAltimetricFrame = javaPackage.IReframe.AltimetricFrame
        jProjectionChange = javaPackage.IReframe.ProjectionChange

        self._lib = jReframe()

        self.planimetric_frames = {
            'lv95': jPlanimetricFrame.LV95,
            'lv03_military': jPlanimetricFrame.LV03_Military,
            'lv03_civil': jPlanimetricFrame.LV03_Civil
        }

        self.altimetric_frames = {
            'lhn95': jAltimetricFrame.LHN95,
            'ln02': jAltimetricFrame.LN02,
            'ellipsoid': jAltimetricFrame.Ellipsoid
        }

        self.projection_changes = {
            'etrf93_geocentric_to_lv95':
            jProjectionChange.ETRF93GeocentricToLV95,
            'etrf93_gepgraphic_to_lv95':
            jProjectionChange.ETRF93GeographicToLV95,
            'lv95_to_etrf93_geocentric':
            jProjectionChange.LV95ToETRF93Geocentric,
            'lv95_to_etrf93_geographic':
            jProjectionChange.LV95ToETRF93Geographic
        }
コード例 #16
0
    def start_jvm(self):
        """
        Start Java virtual machine.
        """
        self.lock.acquire()
        if not self.is_jvm_started():
            jpype.addClassPath(
                'app/workspace/devices/PSI/java/lib/jar/bioreactor-commander-0.8.7.jar'
            )
            jpype.startJVM(
                jvmpath=jpype.getDefaultJVMPath(),
                convertStrings=False,
                classpath=
                "app/workspace/devices/PSI/java/lib/jar/bioreactor-commander-0.8.7.jar"
            )

        self.lock.release()
コード例 #17
0
ファイル: common.py プロジェクト: yongfuxie/jpype
 def setUp(self):
     if not jpype.isJVMStarted():
         root = path.dirname(path.abspath(path.dirname(__file__)))
         jpype.addClassPath(path.join(root, 'classes'))
         jvm_path = jpype.getDefaultJVMPath()
         logger = logging.getLogger(__name__)
         logger.info("Running testsuite using JVM %s" % jvm_path)
         classpath_arg = "-Djava.class.path=%s"
         classpath_arg %= jpype.getClassPath()
         JPypeTestCase.str_conversion = eval(os.getenv('JPYPE_STR_CONVERSION', 'True'))
         jpype.startJVM(jvm_path, "-ea",
                        # TODO: enabling this check crashes the JVM with: FATAL ERROR in native method: Bad global or local ref passed to JNI
                        #"-Xcheck:jni",
                        "-Xmx256M", "-Xms16M", classpath_arg, convertStrings=self.str_conversion)
     self.jpype = jpype.JPackage('jpype')
     if sys.version < '3':
         self.assertCountEqual = self.assertItemsEqual
コード例 #18
0
    def build_and_start_jvm(self,
                            maven_dir: str,
                            code_dir: str,
                            root_package: str,
                            jvm_path=None):
        # Needs to be done here to work with setuptools setup_requires
        # (otherwise imported before that line is even read)
        import jpype
        if jvm_path is None:
            jvm_path = jpype.get_default_jvm_path()
        _logger.debug('generating classpath in {}'.format(maven_dir))

        pom_path = os.path.join(maven_dir, 'pom.xml')

        self._generate_full_pom(pom_path)
        maven_completion = subprocess.run(
            ['mvn', '-DskipTests=true', 'assembly:assembly'], cwd=maven_dir)

        maven_completion.check_returncode()

        for jar in os.listdir(os.path.join(maven_dir, 'target')):
            if jar.endswith('jar-with-dependencies.jar'):
                full_path = os.path.join(maven_dir, 'target', jar)
                _logger.debug('adding {} to classpath'.format(full_path))
                jpype.addClassPath(full_path)

        jpype.startJVM(jvm_path,
                       "-Djava.class.path=%s" % jpype.getClassPath(),
                       convertStrings=False)

        # TODO: generate pxi files for all classes on classpath. Needs to be done after build
        # - generate python type-hinted classes
        # - store them next to jar
        # - put them on sys.path
        PyiUtils = jpype.JClass(
            'org.matsim.contrib.pythonmatsim.typehints.PyiUtils')

        try:
            PyiUtils.generatePythonWrappers(code_dir, root_package)
        except jpype.JException as e:
            print(e.message())
            print(e.stacktrace())
            raise e

        _logger.debug('done generating classpath')
コード例 #19
0
ファイル: common.py プロジェクト: KaiTyrusNelson/jpype
    def setUp(self):
        if not jpype.isJVMStarted():
            try:
                import faulthandler
                faulthandler.enable()
                faulthandler.disable()
            except:
                pass
            root = path.dirname(path.abspath(path.dirname(__file__)))
            jpype.addClassPath(path.join(root, 'classes'))
            jvm_path = jpype.getDefaultJVMPath()
            logger = logging.getLogger(__name__)
            logger.info("Running testsuite using JVM %s" % jvm_path)
            classpath_arg = "-Djava.class.path=%s"
            args = ["-ea", "-Xmx256M", "-Xms16M"]
            if self._checkjni:
                args.append("-Xcheck:jni")
            # TODO: enabling this check crashes the JVM with: FATAL ERROR in native method: Bad global or local ref passed to JNI
            # "-Xcheck:jni",
            if self._classpath:
                from pathlib import Path
                import warnings
                # This needs to be relative to run location
                jpype.addClassPath(Path(self._classpath).resolve())
                warnings.warn("using jar instead of thunks")
            if self._convertStrings:
                import warnings
                warnings.warn("using deprecated convertStrings")
            if self._jacoco:
                import warnings
                args.append(
                    "-javaagent:project/coverage/org.jacoco.agent-0.8.5-runtime.jar=destfile=build/coverage/jacoco.exec,includes=org.jpype.*"
                )
                warnings.warn("using JaCoCo")

            classpath_arg %= jpype.getClassPath()
            args.append(classpath_arg)
            _jpype.enableStacktraces(True)
            #JPypeTestCase.str_conversion = eval(os.getenv('JPYPE_STR_CONVERSION', 'True'))
            jpype.startJVM(jvm_path,
                           *args,
                           convertStrings=self._convertStrings)
        self.jpype = jpype.JPackage('jpype')
        if sys.version < '3':
            self.assertCountEqual = self.assertItemsEqual
コード例 #20
0
    def __init__(self):
        jar_path = Path(__file__).parent / "hartree"
        found_jars = sorted(jar_path.glob('*.jar'))
        if len(found_jars) == 0:
            raise Exception(
                "Could not find any JARs in dir {}".format(jar_path))

        for found_jar in found_jars:
            jpype.addClassPath(found_jar)

        print("Classpath: ", jpype.getClassPath())

        jpype.startJVM(convertStrings=False)

        # TODO: Figure out how to scope these imports for the class
        # noinspection PyUnresolvedReferences
        from org.cmayes.hartree.loader.gaussian import SnapshotLoader

        self.loader = SnapshotLoader()
コード例 #21
0
ファイル: jvm.py プロジェクト: matsim-eth/python-matsim
def start_jvm(jvm_path=jpype.get_default_jvm_path(),
              *additional_classpath):
    if not pkg_resources.resource_exists('javaresources', 'python-matsim-instance-1.0-SNAPSHOT-jar-with-dependencies.jar'):
        raise RuntimeError('could not find jar file')

    if jpype.isJVMStarted():
        # TODO: check that classpath etc. are the ones we want
        _logger.info("JVM is already live, do nothing.")
        return

    python_matsim_jar = pkg_resources.resource_filename('javaresources', 'python-matsim-instance-1.0-SNAPSHOT-jar-with-dependencies.jar')

    jpype.addClassPath(python_matsim_jar)

    for jar in additional_classpath:
        jpype.addClassPath(jar)

    _logger.info('start jvm with classpath {}'.format(jpype.getClassPath()))

    jpype.startJVM(jvm_path, "-Djava.class.path=%s" % jpype.getClassPath(), convertStrings=False)
コード例 #22
0
ファイル: py_graphmk.py プロジェクト: graphmk/py_graphmk
    def generateGraph(self,
                      extractionQuery,
                      filename,
                      serialization_format='gml'):
        graphmk_version = '1.0.0'
        if jpype.isJVMStarted() != True:
            classpth = os.path.dirname(os.path.abspath(
                __file__)) + "/lib/graphmk-{0}.jar".format(graphmk_version)
            jpype.startJVM(jpype.getDefaultJVMPath())
            jpype.addClassPath(classpth)

        GraphGenerateClass = jpype.JClass(
            'io.github.graphmk.graphcore.PyGenerateGraph')

        GraphGenerateClass.main([
            extractionQuery, serialization_format, filename, self.host,
            self.port, self.dbname, self.username, self.password
        ])

        return filename + "." + serialization_format
コード例 #23
0
def load(jvmPath=None):

    complete = ""

    if jvmPath == None:
        complete = jpype.getDefaultJVMPath()
    else:
        linux = os.path.join(jvmPath, "lib", "server", "libjvm.so")
        win = os.path.join(jvmPath, "bin", "server", "jvm.dll")
        mac = os.path.join(jvmPath, "lib", "server", "libjvm.dylib")

        if os.path.exists(linux):
            complete = linux
        elif os.path.exists(win):
            complete = win
        elif os.path.exists(mac):
            complete = mac

    # Link in JISA.jar classes
    jpype.addClassPath(os.path.join(path, "JISA.jar"))
    jpype.imports.registerDomain("jisa")

    # Start the JVM
    jpype.startJVM(jvmpath=complete, convertStrings=True)
コード例 #24
0
ファイル: base_env.py プロジェクト: rubensolv/gym-microrts
    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()
コード例 #25
0
    def testCygwin(self):
        if sys.platform == "win32" or sys.platform == "cygwin":
            raise common.unittest.SkipTest("not tested on cygwin")
        try:
            sys.platform = "cygwin"
            importlib.reload(jpype._classpath)
            self.assertEqual(jpype._classpath._SEP, ";")
            self.assertEqual(jpype._classpath._root, None)
            jpype._classpath._root = r'C:\cygwin64'
            self.assertEqual(jpype._classpath._get_root(), r'C:\cygwin64')
            self.assertEqual(
                jpype._classpath._splitpath('/cygdrive/c/cygwin64'),
                ['cygdrive', 'c', 'cygwin64'])
            self.assertEqual(
                jpype._classpath._posix2win('/cygdrive/c/windows'),
                'c:\\windows')
            self.assertEqual(jpype._classpath._posix2win('/bin'),
                             r"C:\cygwin64\bin")
            jpype._classpath._CLASSPATHS = []
            jpype.addClassPath("/cygdrive/c/windows")
            jpype.addClassPath("/cygdrive/c/data")
            self.assertEqual(jpype._classpath._CLASSPATHS,
                             [r"c:\windows", r"c:\data"])
            env = os.environ.get("CLASSPATH")
            os.environ["CLASSPATH"] = r"c:\programs"
            self.assertEqual(jpype.getClassPath(True),
                             r"c:\windows;c:\data;c:\programs")
            self.assertEqual(jpype.getClassPath(False), r"c:\windows;c:\data")
            # jpype.addClassPath("")
            #self.assertEqual(jpype.getClassPath(False), r"c:\windows;c:\data")
            jpype.addClassPath("/cygdrive/c/data/*")
            self.assertEqual(jpype.getClassPath(False), r"c:\windows;c:\data")
            if not env:
                del os.environ["CLASSPATH"]
            jpype._classpath._CLASSPATHS = []

            with self.assertRaises(jpype.JVMNotFoundException):
                jpype.getDefaultJVMPath()

        finally:
            sys.platform = self.platform
            importlib.reload(jpype._classpath)
コード例 #26
0
import os
import imp
import jpype
import jpype.imports
from jpype.types import *

if jpype.isJVMStarted() != True:
    jars = []
    for top, dirs, files in os.walk(
            imp.find_module('pdfextract')[1] + '/data'):
        for nm in files:
            if nm[-4:] == ".jar":
                jars.append(os.path.join(top, nm))
    jpype.addClassPath(os.pathsep.join(jars))
    jpype.startJVM(jpype.getDefaultJVMPath(), convertStrings=False)
    from java.lang import System
    from java.io import PrintStream, File
    System.setOut(PrintStream(File(os.devnull)))
コード例 #27
0
This file summarizes all java accessm dask_sql needs.

The jpype package is used to access Java classes from python.
It needs to know the java class path, which is set
to the jar file in the package resources.
"""
import pkg_resources
import logging

import jpype

logger = logging.getLogger(__name__)


# Define how to run the java virtual machine.
jpype.addClassPath(pkg_resources.resource_filename("dask_sql", "jar/DaskSQL.jar"))

# There seems to be a bug on Windows server for java >= 11 installed via conda
# It uses a wrong java class path, which can be easily recognizes
# by the \\bin\\bin part. We fix this here.
jvmpath = jpype.getDefaultJVMPath()
jvmpath = jvmpath.replace("\\bin\\bin\\server\\jvm.dll", "\\bin\\server\\jvm.dll")

logger.debug(f"Starting JVM from path {jvmpath}...")
jpype.startJVM(
    "-ea",
    "--illegal-access=deny",
    ignoreUnrecognized=True,
    convertStrings=False,
    jvmpath=jvmpath,
)
コード例 #28
0
ファイル: __init__.py プロジェクト: powsybl/powsybl-incubator
# Copyright (c) 2019, RTE (http://www.rte-france.com)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

name = "pypowsybl_open_loadflow"

import os
import jpype
mypath = os.path.dirname(__file__)
POWSYBL_OPENLOADFLOW_LIBRARIES_PATH = os.path.join(mypath, 'jars')
POWSYBL_OPENLOADFLOW_LIBRARIES = "{}/*".format(POWSYBL_OPENLOADFLOW_LIBRARIES_PATH)
jpype.addClassPath(POWSYBL_OPENLOADFLOW_LIBRARIES)
コード例 #29
0
def main():
    # Parse arguments
    import argparse
    parser = argparse.ArgumentParser(description='Runs a bayesian optimisation for some of the algorithms defined in the PTSP framework')
    parser.add_argument('--algorithm',
                        choices=["QD-MCTS", "S-MCTS", "MS-MCTS", "VanillaGA", "VanillaMCTS"],
                        help='The algorithm that should be optimized',
                        default="S-MCTS")
    parser.add_argument('--outputDir', 
                        default="./optimizationResults",
                        help='The output directory for all data generated by the optimization')
    parser.add_argument("--ptspPath",
                        default="./ptsp.jar",
                        help="The path to the .jar file containing the PTSP framework")
    parser.add_argument("--iters",
                        default="10",
                        type=int,
                        help="Number of parameter-points to test by the bayesian optimization")
    args = parser.parse_args()
    args.outputPath = f"{args.outputDir}/{args.algorithm}"
    
    # Find all previous logs for this optimization
    logs = glob.glob(f"{args.outputPath}/optimizationLogs*.json")
    csvLogs = glob.glob(f"{args.outputPath}/*.csv")
    
    # Launch the JVM
    jpype.startJVM()
    jpype.addClassPath(args.ptspPath)
    import framework.Optimization as optim
    
    # Move java output into a file
    from java.lang import System
    from java.io import PrintStream, FileOutputStream
    pathlib.Path(args.outputPath).mkdir(parents=True, exist_ok=True)
    System.setOut(PrintStream(FileOutputStream(f"{args.outputPath}/cout.txt", True)))
    
    # Algorithm specific data
    bounds = {
        "QD-MCTS" : {
                "lowER": (0.01, 10), # Exploration rate low-level search
                "highER": (0.01, 10), # Exploration rate high-level search
                "steps": (300, 600),  # Number of steps for low-level search
                "rd": (10, 30) # rolloutDepth
            },
        "S-MCTS" : {
                "cellSize": (5, 30), # Size of a cell in the subgoal grid (aka distance between subgoals)
                "er": (0.01, 10), # Exploration rate high-level search
                "steps": (300, 600),  # Number of steps for low-level search
                "rd": (10, 30) # rolloutDepth
            },
        "MS-MCTS" : {
                "cellSize": (5, 30), # Size of a cell in the subgoal grid (aka distance between subgoals)
                "er": (0.01, 10), # Exploration rate high-level search
                "steps": (300, 600),  # Number of steps for low-level search
                "rd": (10, 30) # rolloutDepth
            },
        "VanillaGA" : {
                "gl": (10, 30), # How many base-actions does a genome contain
                "ps": (1, 100), # How many genomes in one population
                "mr": (0.1, 0.9),  # Probability that an action is mutated
            },
        "VanillaMCTS" : {
                "er": (0.01, 10), # Exploration rate
                "rd": (10, 30), # RolloutDepth
            }
    }
    
    funcs = {
        "QD-MCTS" : lambda lowER, highER, steps, rd: execSafe(optim.runQD_MCTS, lowER, highER, round(steps), round(rd)),
        "S-MCTS" : lambda cellSize, er, steps, rd: execSafe(optim.runSMCTS, cellSize, er, round(steps), round(rd)),
        "MS-MCTS" : lambda cellSize, er, steps, rd: execSafe(optim.runMS_MCTS, cellSize, er, round(steps), round(rd)),
        "VanillaGA" : lambda gl, ps, mr: execSafe(optim.runVanillaGA, round(gl), round(ps), mr),
        "VanillaMCTS" : lambda er, rd: execSafe(optim.runVanillaMCTS, er, round(rd))
    }
    
    # Probe points for each algorithm, only one which I've used previously
    probes = {
        "QD-MCTS" : {"lowER": math.sqrt(2), "highER": 4, "steps": 400, "rd": 25},
        "S-MCTS" : {"cellSize": 20, "er": 4, "steps": 400, "rd": 25},
        "MS-MCTS" : {"cellSize": 20, "er": 4, "steps": 400, "rd": 25},
        "VanillaGA" : {"gl": 20, "ps": 50, "mr": 1. / 20},
        "VanillaMCTS" : {"er": math.sqrt(2), "rd": 12}
    }
    
    # Initialize optimization
    optim.setupRun(len(logs) * 11) # Different seed for each run
    optim.RUN_COUNTER = len(csvLogs) # Make sure java logs into a new csv file
    optim.NUM_TRIALS = 10
    optim.OutputDir = args.outputPath
    optim.m_mapNames = glob.glob("./maps/**/*.map", recursive=True)
    
    optimizer = BayesianOptimization(
        f=funcs[args.algorithm],
        pbounds=bounds[args.algorithm],
        random_state=len(logs) * 11, # Change behaviour for each run
    )
    print(f"Optimizing {args.algorithm} with bounds:")
    print(bounds[args.algorithm])
    
    # Probe if necessary
    init_points = 0
    if len(logs) == 0:
        print("Found no previous logs... Probing to improve results:")
        print(probes[args.algorithm])
        optimizer.probe(params=probes[args.algorithm], lazy=True)
        init_points = 5
    else: # If we found logs, load them
        print(f"Reading previous logs into optimizer...")
        load_logs(optimizer, logs=logs);
        for log in logs:
            print(f"Successfully loaded {log}")
    
    logger = JSONLogger(path=f"{args.outputPath}/optimizationLogs{len(logs) + 1}.json")
    optimizer.subscribe(Events.OPTIMIZATION_STEP, logger)
    
    # Run optimization
    print(f"Starting optimisation for {args.algorithm}...")
    optimizer.maximize(init_points=init_points, n_iter=args.iters)
    print("Finished optimisation")
    print(optimizer.max)
コード例 #30
0
def start_jvm(
    finder: Optional[Callable[..., QuPathJVMInfo]] = None,
    finder_kwargs: Optional[Dict[str, Any]] = None,
) -> Optional[QuPathVersion]:
    """start the jvm via jpype

    This is automatically called at import of `paquo.java`.
    """
    global _QUPATH_VERSION

    if jpype.isJVMStarted():
        return _QUPATH_VERSION  # nothing to be done

    if finder is None:
        finder = find_qupath

    if finder_kwargs is None:
        finder_kwargs = {}  # pragma: no cover

    # For the time being, we assume qupath is our JVM of choice
    app_dir, runtime_dir, jvm_path, jvm_options = finder(**finder_kwargs)

    patched_env: Callable[[], ContextManager[Any]]
    if platform.system() == "Windows":
        # workaround for EXCEPTION_ACCESS_VIOLATION crash
        # see: https://github.com/bayer-science-for-a-better-life/paquo/issues/67
        @contextmanager
        def patched_env():
            _old = os.environ.copy()
            os.environ.update({
                "PATH":
                f"{os.path.join(runtime_dir, 'bin')}{os.pathsep}{os.environ['PATH']}"
            })
            try:
                yield
            finally:
                os.environ.clear()
                os.environ.update(_old)

        # the above workaround doesn't fix the issue for python versions installed
        # via the Microsoft Store. Let's warn users that this might cause problems
        def is_windows_store_python() -> bool:
            parts = Path(sys.base_exec_prefix).parts
            try:
                idx = parts.index("WindowsApps")
            except ValueError:
                return False
            try:
                return parts[idx + 1].startswith("PythonSoftwareFoundation")
            except IndexError:
                return False

        if finder_kwargs.pop("warn_microsoft_store_python",
                             True) and is_windows_store_python():
            msg = dedent("""\
            MicrosoftStore Python installation detected
            Your Python version seems to be installed via the MicrosoftStore.
            If paquo crashes with a EXCEPTION_ACCESS_VIOLATION try installing Python from https://www.python.org
            To silence this warning set the following in your .paquo.toml configfile:
            >>> warn_microsoft_store_python = false <<<
            """)
            warn(msg, stacklevel=2)

    else:
        patched_env = nullcontext

    # This is not really needed, but beware we might need SL4J classes (see warning)
    jpype.addClassPath(str(app_dir / '*'))

    with patched_env():
        try:
            jpype.startJVM(str(jvm_path),
                           *jvm_options,
                           ignoreUnrecognized=False,
                           convertStrings=False)
        except RuntimeError as jvm_error:  # pragma: no cover
            # there's a chance that this RuntimeError occurred because a user provided
            # jvm_option is incorrect. let's try if that is the case and crash with a
            # more verbose error message
            try:
                jpype.startJVM(str(jvm_path),
                               *jvm_options,
                               ignoreUnrecognized=True,
                               convertStrings=False)
            except RuntimeError:
                raise jvm_error
            else:
                msg = f"Provided JAVA_OPTS prevent the JVM from starting! {jvm_options}"
                exc = RuntimeError(msg)
                exc.__cause__ = jvm_error
                raise exc

    # we'll do this explicitly here to verify the QuPath version
    try:
        _version = str(JClass("qupath.lib.common.GeneralTools").getVersion())
    except (TypeError, AttributeError):  # pragma: no cover
        version = _QUPATH_VERSION = None
    else:
        version = _QUPATH_VERSION = QuPathVersion(_version)
    return version
コード例 #31
0
ファイル: vec_env.py プロジェクト: Bam4d/gym-microrts
    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
        ])