Beispiel #1
0
def _md_problems(problem_dir):
    global _md

    _md = ModelDict()
    _md.directory = problem_dir
    _md.load(ModelBase)

    lst = []
    for k, m in sorted(_md._models.items()):
        lst.append({'key': k, 'solved': m.solved})

    return lst
Beispiel #2
0
def _md_problems(problem_dir):        
    global _md
    
    _md = ModelDict()
    _md.directory = problem_dir
    _md.load(ModelBase)
    
    lst = []
    for k, m in sorted(_md._models.items()):
        lst.append({ 'key' : k, 'solved' : m.solved })
                
    return lst  
Beispiel #3
0
    def __init__(self, parameters, functionals, model_class):
        self._parameters = None
        self.parameters = parameters

        self._functionals = None
        self.functionals = functionals

        self.model_dict = ModelDict()
        self.model_class = model_class
Beispiel #4
0
    def test_parameteric_study(self):
        mg = ModelGenerator()
        n = 50
        k = 2
        mg.add_parameter('x', np.linspace(-10, 10, n))
        mg.add_parameter('y', np.linspace(-10, 10, n))
        mg.combination()

        cwd = '{0}/models'.format(pythonlab.tempname())
        mg.save(cwd)

        md = ModelDict()
        md.directory = cwd
        md.load(booths_function.BoothsFunction)
        md.solve()

        self.assertEqual(len(md.solved_models), n**k)
Beispiel #5
0
    def setUp(self):
        md = ModelDict()
        variants = [1e-3, 1e-2, 1e2, 1e-4, 1e-1, 
                    1e-5, 1e-8, 1e3, 1e6, 1e-8]

        for x in variants:
            model = quadratic_function.QuadraticFunction()
            model.parameters['x'] = x
            GeneticInfo.set_population_from(model, 0)
            GeneticInfo.set_population_to(model, 0)
            md.add_model(model)

        md.solve(save=False)

        self.functionals = Functionals(Functional('F', 'min'))
        self.selector = SingleCriteriaSelector(self.functionals)
        self.selector.recomended_population_size = len(variants)
        self.population = md.models
Beispiel #6
0
 def setUp(self):
     self.md = ModelDict()
     self.md.directory = '{0}/models'.format(pythonlab.tempname())
Beispiel #7
0
class TestModelDict(Agros2DTestCase):
    def setUp(self):
        self.md = ModelDict()
        self.md.directory = '{0}/models'.format(pythonlab.tempname())

    """
    def test_set_directory(self):
        model = ModelBase()
        self.md.add_model(model)

        with self.assertRaises(RuntimeError):
            self.md.directory = '{0}/models'.format(pythonlab.tempname())
    """

    def test_set_wrong_directory(self):
        with self.assertRaises(IOError):
            self.md.directory = '/models'

    def test_add_model(self):
        model = ModelBase()
        self.md.add_model(model)
        self.assertTrue(len(self.md.models))

    """
    def test_add_model_with_existing_name(self):
        model = ModelBase()
        self.md.add_model(model, 'model')
        self.md.save()
        self.md.clear()

        with self.assertRaises(KeyError):
            self.md.add_model(model, 'model')
    """
    """
    def test_add_model_with_automatic_name(self):
        model = ModelBase()
        indexes = [0, 1, 3]
        for index in indexes:
            self.md.add_model(model, name='model_{0:06d}'.format(index))
        self.md.save()

        self.md.add_model(model)
        self.md.save()
        self.md.clear()

        self.md.load(ModelBase)
        self.assertEqual(len(indexes)+1, len(self.md.models))

        self.md.load(ModelBase, mask='model_000002.pickle')
        self.assertEqual(len(indexes)+1, len(self.md.models))
    """

    def test_save_and_load(self):
        model = ModelBase()

        N = 10
        for i in range(N):
            self.md.add_model(model)
        self.md.save()

        self.md.clear()
        self.md.load(ModelBase)
        self.assertEqual(N, len(self.md.models))

    def test_load_files_with_wrong_directory(self):
        model = ModelBase()
        self.md.add_model(model)

        with self.assertRaises(RuntimeError):
            self.md.load(ModelBase,
                         mask='{0}/models'.format(pythonlab.tempname()))

    def test_solve(self):
        variants = [(1, 2), (2, 3), (3, 4)]
        for a, x in variants:
            model = quadratic_function.QuadraticFunction()
            model.parameters['a'] = a
            model.parameters['x'] = x
            self.md.add_model(model)

        self.md.solve()
        for a, x in variants:
            model = self.md.find_model_by_parameters({'a': a, 'x': x})
            self.assertEqual(a * x**2, model.variables['F'])

    def test_solve_models_by_mask(self):
        for a, x in [(1, 2), (2, 3), (3, 4)]:
            model = quadratic_function.QuadraticFunction()
            model.parameters['a'] = a
            model.parameters['x'] = x
            self.md.add_model(model)

        self.md.solve(mask='model_00000[0, 2]')
        self.assertEqual(len(self.md.models), 3)
        self.assertEqual(len(self.md.solved_models), 2)

        for a, x in [(1, 2), (3, 4)]:
            model = self.md.find_model_by_parameters({'a': a, 'x': x})
            self.assertEqual(a * x**2, model.variables['F'])
Beispiel #8
0
    def __init__(self):
        self._dict = ModelDict()

        self._model_class = ModelBase
        self._parameters = dict()
Beispiel #9
0
class ModelGenerator:
    def __init__(self):
        self._dict = ModelDict()

        self._model_class = ModelBase
        self._parameters = dict()

    @property
    def parameters(self):
        """Parameters of the model."""
        return list(self._parameters)

    @property
    def dict(self):
        """Models dictionary."""
        return self._dict

    @property
    def models(self):
        """List of models in dictionary."""
        return self._dict.models

    def add_parameter(self, name, values):
        """Add new model parameter.

        add_parameter(name, values)

        Keyword arguments:
        name -- parameter name
        values -- list of parameter values
        """
        self._parameters[name] = values

    def remove_parameter(self, name):
        """Remove model parameter.

        remove_parameter(name)

        Keyword arguments:
        name -- parameter name
        """
        del self._parameters[name]

    def save(self, directory):
        """Save models to directory.

        save(directory)

        Keyword arguments:
        directory -- directory path
        """
        self._dict.directory = directory
        self._dict.save()

    def combination(self):
        """Generate models by combination of parameters values."""
        combinations = [[{key: value} for (key, value) in zip(self._parameters, values)] 
                       for values in product(*self._parameters.values())]

        for combination in combinations:
            model = self._model_class()
            for parameter in combination:
                model.parameters.update(parameter)

            self._dict.add_model(model)

    def random_selection(self, count):
        """Generate models by random selection of parameters values."""
        for index in range(count):
            model = self._model_class()
            for key, value in self._parameters.items():
                model.parameters[key] = choice(value)

            self._dict.add_model(model)
Beispiel #10
0
    def __init__(self):
        self._dict = ModelDict()

        self._model_class = ModelBase
        self._parameters = dict()
Beispiel #11
0
class ModelGenerator:
    def __init__(self):
        self._dict = ModelDict()

        self._model_class = ModelBase
        self._parameters = dict()

    @property
    def parameters(self):
        """Parameters of the model."""
        return list(self._parameters)

    @property
    def dict(self):
        """Models dictionary."""
        return self._dict

    @property
    def models(self):
        """List of models in dictionary."""
        return self._dict.models

    def add_parameter(self, name, values):
        """Add new model parameter.

        add_parameter(name, values)

        Keyword arguments:
        name -- parameter name
        values -- list of parameter values
        """
        self._parameters[name] = values

    def remove_parameter(self, name):
        """Remove model parameter.

        remove_parameter(name)

        Keyword arguments:
        name -- parameter name
        """
        del self._parameters[name]

    def save(self, directory):
        """Save models to directory.

        save(directory)

        Keyword arguments:
        directory -- directory path
        """
        self._dict.directory = directory
        self._dict.save()

    def combination(self):
        """Generate models by combination of parameters values."""
        combinations = [[{
            key: value
        } for (key, value) in zip(self._parameters, values)]
                        for values in product(*self._parameters.values())]

        for combination in combinations:
            model = self._model_class()
            for parameter in combination:
                model.parameters.update(parameter)

            self._dict.add_model(model)

    def random_selection(self, count):
        """Generate models by random selection of parameters values."""
        for index in range(count):
            model = self._model_class()
            for key, value in self._parameters.items():
                model.parameters[key] = choice(value)

            self._dict.add_model(model)
Beispiel #12
0
def create_model_dict(directory):   
    md = ModelDict()
    md.load(ModelBase, directory)
    return md
Beispiel #13
0
class TestModelDict(Agros2DTestCase):
    def setUp(self):
        self.md = ModelDict()
        self.md.directory = '{0}/models'.format(pythonlab.tempname())

    """
    def test_set_directory(self):
        model = ModelBase()
        self.md.add_model(model)

        with self.assertRaises(RuntimeError):
            self.md.directory = '{0}/models'.format(pythonlab.tempname())
    """

    def test_set_wrong_directory(self):
        with self.assertRaises(IOError):
            self.md.directory = '/models'

    def test_add_model(self):
        model = ModelBase()
        self.md.add_model(model)
        self.assertTrue(len(self.md.models))

    """
    def test_add_model_with_existing_name(self):
        model = ModelBase()
        self.md.add_model(model, 'model')
        self.md.save()
        self.md.clear()

        with self.assertRaises(KeyError):
            self.md.add_model(model, 'model')
    """

    """
    def test_add_model_with_automatic_name(self):
        model = ModelBase()
        indexes = [0, 1, 3]
        for index in indexes:
            self.md.add_model(model, name='model_{0:06d}'.format(index))
        self.md.save()

        self.md.add_model(model)
        self.md.save()
        self.md.clear()

        self.md.load(ModelBase)
        self.assertEqual(len(indexes)+1, len(self.md.models))

        self.md.load(ModelBase, mask='model_000002.pickle')
        self.assertEqual(len(indexes)+1, len(self.md.models))
    """

    def test_save_and_load(self):
        model = ModelBase()

        N = 10
        for i in range(N):
            self.md.add_model(model)
        self.md.save()

        self.md.clear()
        self.md.load(ModelBase)
        self.assertEqual(N, len(self.md.models))

    def test_load_files_with_wrong_directory(self):
        model = ModelBase()
        self.md.add_model(model)

        with self.assertRaises(RuntimeError):
            self.md.load(ModelBase, mask='{0}/models'.format(pythonlab.tempname()))

    def test_solve(self):
        variants = [(1, 2), (2, 3), (3, 4)]
        for a, x in variants:
            model = quadratic_function.QuadraticFunction()
            model.parameters['a'] = a
            model.parameters['x'] = x
            self.md.add_model(model)

        self.md.solve()
        for a, x in variants:
            model = self.md.find_model_by_parameters({'a' : a, 'x' : x})
            self.assertEqual(a*x**2, model.variables['F'])

    def test_solve_models_by_mask(self):
        for a, x in [(1, 2), (2, 3), (3, 4)]:
            model = quadratic_function.QuadraticFunction()
            model.parameters['a'] = a
            model.parameters['x'] = x
            self.md.add_model(model)

        self.md.solve(mask='model_00000[0, 2]')
        self.assertEqual(len(self.md.models), 3)
        self.assertEqual(len(self.md.solved_models), 2)

        for a, x in [(1, 2), (3, 4)]:
            model = self.md.find_model_by_parameters({'a' : a, 'x' : x})
            self.assertEqual(a*x**2, model.variables['F'])
Beispiel #14
0
 def setUp(self):
     self.md = ModelDict()
     self.md.directory = '{0}/models'.format(pythonlab.tempname())