def test_discretize_domain(self):
        discretization = DomainEntity.discretize_domain([self.bounds_domain_x],
                                                        [1])
        assert discretization == [self.discretization_domain_x]

        with self.assertRaises(ValueError):
            DomainEntity.discretize_domain([self.bounds_domain_x], [1, 2])
    def setUp(self):
        super(TestDomainEntity, self).setUp()

        self.bounds_domain_x = BoundsEntity({
            'lower_bound': 0,
            'upper_bound': 1
        })

        self.discretization_domain_x = [0]

        self.domain = DomainEntity({
            'dim_x':
            1,
            'choose_noise':
            True,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [[3]],
            'discretization_domain_x': [self.discretization_domain_x]
        })

        self.spec = {
            'dim_x': 1,
            'choose_noise': True,
            'bounds_domain_x': [self.bounds_domain_x]
        }
Esempio n. 3
0
    def load_discretization(cls, problem_name, bounds_domain_x,
                            number_points_each_dimension_x):
        """
        Try to load discretization for problem_name from file. If the file doesn't exist, will
        generate the discretization and store it.

        :param problem_name: (str)
        :param bounds_domain_x: ([BoundsEntity])
        :param number_points_each_dimension_x: ([int])

        :return: [[float]]
        """

        bounds_str = BoundsEntity.get_bounds_as_lists(bounds_domain_x)

        filename = cls._disc_x_filename(
            name=problem_name,
            bounds=bounds_str,
            number_points_each_dimension=number_points_each_dimension_x)

        if not os.path.exists(path.join(PROBLEM_DIR, problem_name)):
            os.mkdir(path.join(PROBLEM_DIR, problem_name))

        domain_dir = path.join(PROBLEM_DIR, problem_name, DOMAIN_DIR)

        if not os.path.exists(domain_dir):
            os.mkdir(domain_dir)

        domain_path = path.join(domain_dir, filename)

        discretization_data = JSONFile.read(domain_path)
        if discretization_data is not None:
            return discretization_data

        logger.info('Gnerating discretization of domain_x')
        discretization_data = DomainEntity.discretize_domain(
            bounds_domain_x, number_points_each_dimension_x)
        logger.info('Generated discretization of domain_x')

        JSONFile.write(discretization_data, domain_path)

        return discretization_data
Esempio n. 4
0
    def from_dict(cls, spec):
        """
        Create from dict

        :param spec: dict
        :return: DomainEntity
        """
        entry = {}
        entry['dim_x'] = int(spec['dim_x'])
        entry['choose_noise'] = spec['choose_noise']
        entry['bounds_domain_x'] = spec['bounds_domain_x']

        entry['dim_w'] = spec.get('dim_w')
        entry['bounds_domain_w'] = spec.get('bounds_domain_w')
        entry['domain_w'] = spec.get('domain_w')

        if 'number_points_each_dimension' in spec:
            entry['discretization_domain_x'] = \
                cls.load_discretization(spec['problem_name'], entry['bounds_domain_x'],
                                        spec['number_points_each_dimension'])

        return DomainEntity(entry)
    def test_validate(self):

        self.domain.validate()

        wrong_bounds = BoundsEntity({'lower_bound': 1, 'upper_bound': 0})

        with self.assertRaises(ModelValidationError):
            wrong_bounds.validate()

        bad_dim_domain_w = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x]
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w.validate()

        bad_dim_domain_w_ = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            2,
            'bounds_domain_x': [self.bounds_domain_x]
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_.validate()

        bad_dim_domain_w_2 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1.0,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_2.validate()

        bad_dim_domain_w_3 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [self.discretization_domain_x]
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_3.validate()

        bad_dim_domain_w_4 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [self.discretization_domain_x],
            'discretization_domain_x': [[5, 6]],
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_4.validate()

        bad_dim_domain_w_5 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [self.discretization_domain_x],
            'discretization_domain_x': [[5]],
            'bounds_domain_w': [self.bounds_domain_x, self.bounds_domain_x],
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_5.validate()

        bad_dim_domain_w_6 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [self.discretization_domain_x, [4, 5]],
            'discretization_domain_x': [[5]],
            'bounds_domain_w': [self.bounds_domain_x],
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_6.validate()
class TestDomainEntity(unittest.TestCase):
    def setUp(self):
        super(TestDomainEntity, self).setUp()

        self.bounds_domain_x = BoundsEntity({
            'lower_bound': 0,
            'upper_bound': 1
        })

        self.discretization_domain_x = [0]

        self.domain = DomainEntity({
            'dim_x':
            1,
            'choose_noise':
            True,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [[3]],
            'discretization_domain_x': [self.discretization_domain_x]
        })

        self.spec = {
            'dim_x': 1,
            'choose_noise': True,
            'bounds_domain_x': [self.bounds_domain_x]
        }

    def test_discretize_domain(self):
        discretization = DomainEntity.discretize_domain([self.bounds_domain_x],
                                                        [1])
        assert discretization == [self.discretization_domain_x]

        with self.assertRaises(ValueError):
            DomainEntity.discretize_domain([self.bounds_domain_x], [1, 2])

    def test_get_bounds_as_lists(self):
        bounds = [self.bounds_domain_x]
        assert [[0.0, 1.0]] == BoundsEntity.get_bounds_as_lists(bounds)

    def test_validate(self):

        self.domain.validate()

        wrong_bounds = BoundsEntity({'lower_bound': 1, 'upper_bound': 0})

        with self.assertRaises(ModelValidationError):
            wrong_bounds.validate()

        bad_dim_domain_w = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x]
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w.validate()

        bad_dim_domain_w_ = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            2,
            'bounds_domain_x': [self.bounds_domain_x]
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_.validate()

        bad_dim_domain_w_2 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1.0,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_2.validate()

        bad_dim_domain_w_3 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [self.discretization_domain_x]
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_3.validate()

        bad_dim_domain_w_4 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [self.discretization_domain_x],
            'discretization_domain_x': [[5, 6]],
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_4.validate()

        bad_dim_domain_w_5 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [self.discretization_domain_x],
            'discretization_domain_x': [[5]],
            'bounds_domain_w': [self.bounds_domain_x, self.bounds_domain_x],
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_5.validate()

        bad_dim_domain_w_6 = DomainEntity({
            'choose_noise':
            True,
            'dim_x':
            1,
            'bounds_domain_x': [self.bounds_domain_x],
            'dim_w':
            1,
            'domain_w': [self.discretization_domain_x, [4, 5]],
            'discretization_domain_x': [[5]],
            'bounds_domain_w': [self.bounds_domain_x],
        })

        with self.assertRaises(ModelValidationError):
            bad_dim_domain_w_6.validate()