Exemple #1
0
    def __init__(self, infile='skpar_in.yaml', verbose=True):

        # setup logger
        # -------------------------------------------------------------------
        loglevel = logging.DEBUG if verbose else logging.INFO
        self.logger = get_logger(name='skpar',
                                 filename='skpar.log',
                                 verbosity=loglevel)
        # specific for printing/reporting from numpy objects
        np.set_printoptions(threshold=60, linewidth=79, suppress=True)

        # Project work directory
        # -------------------------------------------------------------------
        self.workdir = os.getcwd()

        # Main part
        # -------------------------------------------------------------------
        # parse input file
        self.logger.info('Parsing input file {:s}'.format(infile))
        taskdict, tasklist, objectives, optimisation, config =\
            parse_input(infile, verbose=verbose)
        if optimisation is not None:
            algo, options, parameters = optimisation
            parnames = [p.name for p in parameters]
        else:
            parnames = None

        # instantiate the evaluator machinery
        self.logger.info('Instantiating Evaluator')
        self.evaluator = Evaluator(objectives,
                                   tasklist,
                                   taskdict,
                                   parnames,
                                   config,
                                   verbose=verbose)

        # instantiate the optimiser
        if optimisation is not None:
            self.do_optimisation = True
            self.logger.info('Instantiating Optimiser')
            self.optimiser = Optimiser(algo,
                                       parameters,
                                       self.evaluator,
                                       options,
                                       verbose=True)
        else:
            self.do_optimisation = False
Exemple #2
0
from os.path import normpath, expanduser
from os.path import join as joinpath
from os.path import split as splitpath
import numpy as np
import logging
import yaml
from pprint import pprint, pformat
from skpar.core.utils import get_logger, normalise, arr2s
from skpar.core.utils import get_ranges, f2prange
from skpar.core.database import Query
from skpar.core.evaluate import COSTF, ERRF

DEFAULT_COST_FUNC = "rms"
DEFAULT_ERROR_FUNC = "abs"

LOGGER = get_logger(__name__)


def parse_weights_keyval(spec, data, normalised=True):
    """Parse the weights corresponding to key-value type of data.

    Args:
        spec (dict): Specification of weights, in a key-value fashion.
            It is in the example format::
                { 'dflt': 0., 'key1': w1, 'key3': w3}
            with w1, w3, etc. being float values.
        data (structured numpy array): Data to be weighted.

            Typical way of obtaining `data` in this format is to use::

                loader_args = {'dtype': [('keys', 'S15'), ('values', 'float')]}
Exemple #3
0
Therefore the above assumptions is safe.
The interpretation of min/max parameter values remains the same as 
usual.

While there is no encoded limitation on the number of parameters to 
use in a scan, practically, for more than 2 or three parameters, the
computational effort will be excessive, in comparison to optimisation
based on the PSO.
"""
import numpy as np
from deap import base
from deap import creator
from deap import tools
from skpar.core.utils import get_logger

module_logger = get_logger('skpar.pscan')


def declareTypes(weights=(-1, )):
    """Declare a types relevant to PSCAN.
    """
    creator.create("pFitness", base.Fitness, weights=weights)
    creator.create("Population", list, inext=None, best=None, ibest=None)
    creator.create("Point", list, ind=None, fitness=creator.pFitness)


def create_positions(ranges, numpts):
    """Create a sequence of np.prod(nupmts) over the ranges.
    
    Args:
        ranges: list of 2-tuples, each tuple being [min, max] of a numerical range.
    * the user must create an OrderedDict of parameter name:value pairs;
      the OrderedDict eliminates automatically duplicate definitions of parameters,
      yielding the minimum possible number of degrees of freedom for the optimiser
    * the OrderedDictionary is built by parsing an skdefs.template file that
      contains parameter-strings (conveying the name, initial value and range).
    * skdefs.template is parsed by functions looking for a given format from which
      parameter-strings are extracted
    * the Class Parameter is initialised from a parameter-string.
    * a reduced skdefs.template is obtained, by removing the initial value and range
      from the input skdefs.template which is ready for creating the final skdefs
      file by string.format(dict(zip(ParNames,Parvalues)) substitution.
"""
import os.path
from skpar.core.utils import get_logger

LOGGER = get_logger('__name__')

def get_parameters(userinp):
    """Parse user input for definitions of parameters.

    The definitions should be of the form ('name', 'optionally_something').
    The optional part could in principle be one or more str/float/int.
    """
    params = []
    for pardef in userinp:
        try:
            # Due to yaml/json representation, each parameter definition is a
            # dictionary on its own, with one item. Using (key, val), we
            # extract the one and only item in this dict.
            (pname, pdef), = pardef.items()
        except AttributeError:
Exemple #5
0
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator
import numpy as np
import itertools
import logging
from skpar.core.utils import get_logger

module_logger = get_logger('skpar.tasks')


def set_mplrcpar(**kwargs):
    """Configure matplotlib rcParams."""
    matplotlib.rcParams.update({'axes.titlesize': kwargs.get('fontsize', 18),\
                                'font.size': kwargs.get('fontsize', 18),\
                                'font.family': kwargs.get('fontfamily', 'sans-serif'),
                                'font.sans-serif': kwargs.get('font',
                                ['Arial', 'DejaVu Sans', 'Bitstream Vera Sans', 'Lucida Grande',
                                'Verdana', 'Geneva', 'Lucid', 'Helvetica', 'Avant Garde', 'sans-serif'])})
    plt.rc('lines', linewidth=2)
    plt.rc('savefig', bbox='tight')
    plt.rc('savefig', transparent='True')


def set_axes(ax,
             xlabel,
             ylabel,
             xticklabels=None,
             yticklabels=None,
             extend_xticks=False,
Exemple #6
0
    * ``gbestfit`` -- globally the best fitness (i.e. the quality value of gbest)   

The swarm is declared, created and let to evolve with the help of the ``PSO`` class.
"""
import random
import operator
import sys
import numpy as np

from deap import base
from deap import creator
from deap import tools

from skpar.core.utils import get_logger

module_logger = get_logger('skpar.pso')


def declareTypes(weights):
    """
    Declare Particle class with fitting objectives (-1: minimization; +1 maximization).
    Each particle consists of a set of normalised coordinates, returned by the
    particle's instance itself. The true (physical) coordinates of the particle
    are stored in the field 'renormalized'. The field 'best' contains the best 
    fitness-wise position that the particle has ever had (in normalized coords).
    Declare also Swarm class; inherit from list + PSO specific attributes
    gbest and gbestfit.
    Arguments:
        weights -- tupple, e.g. (-1,) for single objective minimization, or
                   (+1,+0.5) for two objective maximization, etc.
    """