How to use cma - 10 common examples

To help you get started, we’ve selected a few cma examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CMA-ES / pycma / cma / optimization_tools.py View on Github external
Given the first values are the earliest, this is a useful policy
        also with a time changing objective.

        """
        ## meta_parameters.noise_reeval_multiplier == 1.0
        lam_reev = 1.0 * (self.lam_reeval if self.lam_reeval
                            else 2 + len(fit) / 20)
        lam_reev = int(lam_reev) + ((lam_reev % 1) > np.random.rand())
        ## meta_parameters.noise_choose_reeval == 1
        choice = 1
        if choice == 1:
            # take n_first first and reev - n_first best of the remaining
            n_first = lam_reev - lam_reev // 2
            sort_idx = np.argsort(np.array(fit, copy=False)[n_first:]) + n_first
            return np.array(list(range(0, n_first)) +
                            list(sort_idx[0:lam_reev - n_first]), copy=False)
        elif choice == 2:
            idx_sorted = np.argsort(np.array(fit, copy=False))
            # take lam_reev equally spaced, starting with best
            linsp = np.linspace(0, len(fit) - len(fit) / lam_reev, lam_reev)
            return idx_sorted[[int(i) for i in linsp]]
        # take the ``lam_reeval`` best from the first ``2 * lam_reeval + 2`` values.
        elif choice == 3:
            return np.argsort(np.array(fit, copy=False)[:2 * (lam_reev + 1)])[:lam_reev]
        else:
            raise ValueError('unrecognized choice value %d for noise reev'
                             % choice)
github CMA-ES / pycma / cma / constraints_handler.py View on Github external
`CMAEvolutionStrategy` object instance, in particular
            mean and variances and the methods from the attribute
            `gp` of type `GenoPheno` are used.

        """
        if self.bounds is None or (self.bounds[0] is None and
                                   self.bounds[1] is None):
            return self

        N = es.N
        # ## prepare
        # compute varis = sigma**2 * C_ii
        if 11 < 3:  # old
            varis = es.sigma**2 * np.array(N * [es.C] if np.isscalar(es.C) else (# scalar case
                                    es.C if np.isscalar(es.C[0]) else  # diagonal matrix case
                                    [es.C[i][i] for i in range(N)]))  # full matrix case
        else:
            varis = es.sigma**2 * es.sm.variances

        # relative violation in geno-space
        dmean = (es.mean - es.gp.geno(self.repair(es.gp.pheno(es.mean)))) / varis**0.5

        # ## Store/update a history of delta fitness value
        fvals = sorted(function_values)
        l = 1 + len(fvals)
        val = fvals[3 * l // 4] - fvals[l // 4]  # exact interquartile range apart interpolation
        val = val / np.mean(varis)  # new: val is normalized with sigma of the same iteration
        # insert val in history
        if np.isfinite(val) and val > 0:
            self.hist.insert(0, val)
        elif val == np.inf and len(self.hist) > 1:
            self.hist.insert(0, max(self.hist))
github MOCR / DDPG / test / cma_study.py View on Github external
def call_cma(self):
        w1=  self.controller.get_weight(layer_nb, numpoids1)[0]
        w2=  self.controller.get_weight(layer_nb, numpoids2)[0]
        x = np.array([w1,w2])
        self.green_x.append(w1)
        self.green_y.append(w2)
        fx = cma.fmin(self.run_episode, x, sigma, options = self.options)
        self.final_draw()
github MOCR / DDPG / test / cma_agent.py View on Github external
def __init__(self,env):
        '''
	Input:
        '''
        self.env = env
        self.env.reset(False)
        self.controller = Keras_NN(env.observation_space.low.shape[0],env.action_space.low.shape[0])
        self.nb_episodes = 0
        self.options = cma.CMAOptions()
        self.options['maxiter']=200
        self.options['popsize']=50
        self.options['CMA_diagonal']=True
        self.options['verb_log']=50
        self.options['verb_disp']=1
        self.best_perf = -10000.0
github MOCR / DDPG / test / cma_study.py View on Github external
def __init__(self,env):
        '''
	Input:
        '''
        self.env = env
        self.env.reset(False)
        self.controller = Keras_NN(env.observation_space.low.shape[0],env.action_space.low.shape[0])
        self.controller.load_theta('cma_agents/best_agent.theta-2772.2691621')
        self.nb_episodes = 0
        self.options = cma.CMAOptions()
        self.options['maxiter']=40
        self.options['popsize']=50
#        self.options['CMA_diagonal']=True
        self.options['verb_log']=50
        self.options['verb_disp']=1
        self.best_perf = -10000.0

        self.start_x = 0.73
        self.start_y = -0.26
        self.controller.set_param(layer_nb, numpoids1, self.start_x)
        self.controller.set_param(layer_nb, numpoids2, self.start_y)
        
        self.white_x = []
        self.white_y = []
        
        self.best_x = []
github assimilation / assimilation-official / cma / systemtests / testcases.py View on Github external
"ERROR: Test %s timed out waiting for %s [timeout:%s]"
                % (self.__class__.__name__, str(watcher.unmatched), timeout)
            )
            return self._record(AssimSysTest.FAIL)
        if debug:
            print("DEBUG: Test %s found regex %s" % (self.__class__.__name__, str(watcher.regexes)))
        if query.check(
            (nano, self.testenviron.cma, service), validator, minrows=minrows, maxrows=maxrows
        ):
            if debug:
                print("DEBUG: Test %s passed query %s" % (self.__class__.__name__, querystring))
            return self._record(AssimSysTest.SUCCESS)

        print("DEBUG: query.check() FAILED", file=sys.stderr)
        logger("ERROR: Test %s failed query %s" % (self.__class__.__name__, querystring))
        return self._record(AssimSysTest.FAIL)
github assimilation / assimilation-official / cma / systemtests / testcases.py View on Github external
rc = StopNanoprobe(self.store, self.logfilename, self.testenviron).run(
            nano, debug=self.debug, timeout=timeout
        )
        if rc != AssimSysTest.SUCCESS:
            return self._record(rc)
        if self.delay > 0:
            time.sleep(self.delay)
        return self._record(
            StartNanoprobe(self.store, self.logfilename, self.testenviron).run(
                nano, debug=self.debug, timeout=timeout
            )
        )


@AssimSysTest.register
class RestartCMA(AssimSysTest):
    "A restart CMA test: stop then restart the CMA.  Scary stuff!"

    def __init__(self, store, logfilename, testenviron, debug=False, delay=0):
        AssimSysTest.__init__(self, store, logfilename, testenviron, debug)
        self.delay = delay

    def run(self, nano=None, debug=None, timeout=60):
        "Actually stop and start (restart) the CMA and see if it worked"
        if debug is None:
            debug = self.debug
        cma = self.testenviron.cma
        cma.stopservice(SystemTestEnvironment.CMASERVICE)
        regexes = self.cma_start_regexes()
        watch = LogWatcher(self.logfilename, regexes, timeout=timeout, debug=debug)
        watch.setwatch()
        if self.delay > 0:
github assimilation / assimilation-official / cma / systemtests / testcases.py View on Github external
def run(self, nano=None, debug=None, timeout=180):
        "Actually stop the nanoprobe and see if it worked"
        if debug is None:
            debug = self.debug
        if nano is None:
            nanozero = self.testenviron.select_nano_service()
            if len(nanozero) > 0:
                nano = nanozero[0]
        if (
            nano is None
            or nano.status != TestSystem.RUNNING
            or SystemTestEnvironment.NANOSERVICE not in nano.runningservices
        ):
            return self._record(AssimSysTest.SKIPPED)
        regexes = self.nano_kill9_regexes(nano)
        print("KILL9_REGEXES ARE:", regexes, file=sys.stderr)
        watch = LogWatcher(self.logfilename, regexes, timeout=timeout, debug=debug)
        watch.setwatch()
        qstr = (
            """START drone=node:Drone('*:*') """
            """WHERE drone.designation = "{0.hostname}" and drone.status = "dead" """
            """and drone.reason = "HBDEAD packet received"       RETURN drone"""
        )
        nano.kill9service(SystemTestEnvironment.NANOSERVICE)
        # print >> sys.stderr, 'NANO NEIGHBORS', get_nano_neighbors(self.store, None, nano)
        return self.checkresults(watch, timeout, qstr, None, nano)
github assimilation / assimilation-official / cma / systemtests / testcases.py View on Github external
):
            return self._record(AssimSysTest.SKIPPED)
        regexes = self.nano_stop_regexes(nano)
        watch = LogWatcher(self.logfilename, regexes, timeout=timeout, debug=debug)
        watch.setwatch()
        qstr = (
            """START drone=node:Drone('*:*') """
            """WHERE drone.designation = "{0.hostname}" and drone.status = "dead" """
            """and drone.reason = "HBSHUTDOWN"       RETURN drone"""
        )
        nano.stopservice(SystemTestEnvironment.NANOSERVICE)
        return self.checkresults(watch, timeout, qstr, None, nano)


@AssimSysTest.register
class KillNanoprobe(AssimSysTest):
    "A kill -9 nanoprobe test"

    def run(self, nano=None, debug=None, timeout=180):
        "Actually stop the nanoprobe and see if it worked"
        if debug is None:
            debug = self.debug
        if nano is None:
            nanozero = self.testenviron.select_nano_service()
            if len(nanozero) > 0:
                nano = nanozero[0]
        if (
            nano is None
            or nano.status != TestSystem.RUNNING
            or SystemTestEnvironment.NANOSERVICE not in nano.runningservices
        ):
            return self._record(AssimSysTest.SKIPPED)
github assimilation / assimilation-official / cma / systemtests / testcases.py View on Github external
regexes = self.nano_start_regexes(nano)

        watch = LogWatcher(self.logfilename, regexes, timeout=timeout, debug=debug)
        watch.setwatch()
        qstr = (
            """START drone=node:Drone('*:*') """
            """WHERE drone.designation = "{0.hostname}" and drone.status = "up" """
            """RETURN drone"""
        )
        nano.startservice(SystemTestEnvironment.NANOSERVICE)
        return self.checkresults(watch, timeout, qstr, None, nano)


@AssimSysTest.register
class FlipNanoprobe(AssimSysTest):
    """A flip nanoprobe test - if it's up, bring it down -- and vice versa"""

    def run(self, nano=None, debug=None, timeout=240):
        "Actually flip the nanoprobe and see if it worked"
        if debug is None:
            debug = self.debug
        if nano is None:
            nanozero = self.testenviron.select_nanoprobe()
            if len(nanozero) > 0:
                nano = nanozero[0]
        if nano is None:
            return self._record(AssimSysTest.SKIPPED)
        if SystemTestEnvironment.NANOSERVICE in nano.runningservices:
            return self._record(
                StopNanoprobe(self.store, self.logfilename, self.testenviron).run(
                    nano, debug=self.debug, timeout=timeout