def test_vertices_2D(self):
        # type: (ResultSetTestCase) -> None

        s1 = {(0.250003814697266, 1.0), (0.875003814930096, 0.12500953685958), (0.12500953685958, 1.0),
              (0.75, 0.250003814697266), (0.625, 0.375001907290425), (0.75, 0.375001907290425),
              (0.250003814697266, 0.875003814930096), (0.625, 0.500007629394531), (1.0, 0.12500953685958),
              (0.500007629394531, 0.500007629394531), (0.250003814697266, 0.75), (0.75, 0.500007629394531),
              (0.12500953685958, 0.875003814930096), (1.0, 0.250003814697266), (0.500007629394531, 1.0),
              (0.500007629394531, 0.75), (1.0, 1.0), (0.875003814930096, 0.250003814697266),
              (1.0, 0.500007629394531)}

        s2 = {(0.125001907348633, 0.874996185302734), (0.5, 0.5), (0.249996185186319, 0.5), (0.0, 0.749992370605469),
              (0.0, 0.5), (0.874996185302734, 0.125001907348633), (0.749992370605469, 0.249996185186319),
              (0.624992370605469, 0.249996185186319), (0.125001907348633, 0.749992370605469), (0.749992370605469, 0.0),
              (0.5, 0.0), (0.624992370605469, 0.374994277546644), (0.0, 0.874996185302734), (0.5, 0.249996185186319),
              (0.0, 0.0), (0.5, 0.374994277546644), (0.874996185302734, 0.0), (0.749992370605469, 0.125001907348633),
              (0.249996185186319, 0.749992370605469)}

        s3 = {(0.0, 0.874996185302734), (0.75, 0.375001907290425), (0.249996185186319, 0.75),
              (0.875003814930096, 0.250003814697266), (0.624992370605469, 0.375001907290425),
              (0.125001907348633, 0.749992370605469), (0.874996185302734, 0.0),
              (0.749992370605469, 0.250003814697266),
              (0.249996185186319, 0.5), (0.500007629394531, 0.5), (0.500007629394531, 0.75),
              (0.250003814697266, 0.749992370605469), (0.5, 0.374994277546644), (0.12500953685958, 1.0),
              (0.250003814697266, 0.875003814930096), (0.125001907348633, 0.875003814930096),
              (0.5, 0.500007629394531),
              (0.75, 0.249996185186319), (1.0, 0.0), (0.0, 1.0), (0.624992370605469, 0.249996185186319),
              (0.625, 0.500007629394531), (1.0, 0.12500953685958), (0.625, 0.374994277546644),
              (0.12500953685958, 0.874996185302734), (0.749992370605469, 0.125001907348633),
              (0.875003814930096, 0.125001907348633), (0.874996185302734, 0.12500953685958)}

        self.assertEqual(s1, self.rs_2D.vertices_yup())
        self.assertEqual(s2, self.rs_2D.vertices_ylow())
        self.assertEqual(s3, self.rs_2D.vertices_border())
        self.assertEqual(s1.union(s2).union(s3), self.rs_2D.vertices())

        pareto_points = set(self.rs_2D.get_points_pareto())
        for p in pareto_points:
            self.assertTrue(p in self.rs_2D)

        rs = ResultSet(xspace=self.rs_2D.xspace)
        rs.set_points_pareto(pareto_points)
        pareto_points2 = set(rs.get_points_pareto())
        self.assertEqual(pareto_points, pareto_points2)
    def test_files_3D(self):
        # type: (ResultSetTestCase) -> None
        tmpfile = tf.NamedTemporaryFile(delete=False)
        nfile = tmpfile.name

        self.rs_3D.to_file(nfile)
        self.rs2.from_file(nfile)

        self.assertEqual(self.rs_3D, self.rs2)
        self.assertEqual(hash(self.rs_3D), hash(self.rs2))

        temp_rs = ResultSet()
        self.assertNotEqual(self.rs_3D, temp_rs)
        self.assertNotEqual(self.rs2, temp_rs)
        del temp_rs

        print('ResultSet: {0}'.format(self.rs_3D))

        # Remove tempfile
        # os.unlink(nfile)
        self.add_file_to_clean(nfile)
    def test_volume_3D(self):
        # type: (ResultSetTestCase) -> None

        self.assertEqual(0.0, self.rs_3D.overlapping_volume_yup())
        self.assertEqual(0.0, self.rs_3D.overlapping_volume_ylow())
        self.assertAlmostEqual(1.7168407867280266e-05, self.rs_3D.overlapping_volume_border())
        self.assertAlmostEqual(2.0029416264500524e-05, self.rs_3D.overlapping_volume_total())

        # May differ in the last decimals because of arithmetic precision
        self.assertAlmostEqual(0.2968666554443193, self.rs_3D.volume_yup())
        self.assertAlmostEqual(0.2968699931807370, self.rs_3D.volume_ylow())
        self.assertAlmostEqual(0.4062633513749437, self.rs_3D.volume_border())
        self.assertAlmostEqual(self.rs_3D.volume_border(), self.rs_3D.volume_border_2())
        # self.assertAlmostEqual(0.4062633501234191, self.rs_3D.volume_border_2())

        # Simplify the current result set (i.e., fusion of contiguous rectangles).
        # Overlapping should disappear
        # Volume should remain identical to previous computations.
        # rs_sim = copy.deepcopy(self.rs_3D)
        rs_sim = ResultSet(self.border_3D, self.ylow_3D, self.yup_3D, self.xspace_3D)
        rs_sim.simplify()
        rs_sim.fusion()

        self.assertEqual(0.0, rs_sim.overlapping_volume_yup())
        self.assertEqual(0.0, rs_sim.overlapping_volume_ylow())
        self.assertEqual(0.0, rs_sim.overlapping_volume_border())
        self.assertEqual(0.0, rs_sim.overlapping_volume_total())

        # May differ in the last decimals because of arithmetic precision
        self.assertAlmostEqual(0.34374141701118816, rs_sim.volume_yup())
        self.assertAlmostEqual(0.3124945163472164, rs_sim.volume_ylow())
        self.assertAlmostEqual(0.3437640666415955, rs_sim.volume_border())
        self.assertAlmostEqual(0.3437640666415954, rs_sim.volume_border_2())
        self.assertAlmostEqual(rs_sim.volume_border(), rs_sim.volume_border_2())
    def test_volume_2D(self):
        # type: (ResultSetTestCase) -> None

        self.assertEqual(0.0, self.rs_2D.overlapping_volume_yup())
        self.assertEqual(0.0, self.rs_2D.overlapping_volume_ylow())
        self.assertAlmostEqual(3.492557355327832e-10, self.rs_2D.overlapping_volume_border())
        self.assertAlmostEqual(3.492557355327832e-10, self.rs_2D.overlapping_volume_total())

        # May differ in the last decimals because of arithmetic precision
        self.assertAlmostEqual(0.42186760904587917, self.rs_2D.volume_yup())
        self.assertAlmostEqual(0.42186951636540826, self.rs_2D.volume_ylow())
        self.assertAlmostEqual(0.15626287458871257, self.rs_2D.volume_border())
        self.assertAlmostEqual(self.rs_2D.volume_border(), self.rs_2D.volume_border_2())

        # Simplify the current result set (i.e., fusion of contiguous rectangles).
        # Overlapping should disappear
        # Volume should remain identical to previous computations.
        # rs_sim = copy.deepcopy(self.rs_2D)
        rs_sim = ResultSet(self.border_2D, self.ylow_2D, self.yup_2D, self.xspace_2D)
        rs_sim.simplify()
        rs_sim.fusion()

        self.assertEqual(0.0, rs_sim.overlapping_volume_yup())
        self.assertEqual(0.0, rs_sim.overlapping_volume_ylow())
        self.assertEqual(0.0, rs_sim.overlapping_volume_border())
        self.assertEqual(0.0, rs_sim.overlapping_volume_total())

        # May differ in the last decimals because of arithmetic precision
        self.assertAlmostEqual(rs_sim.volume_yup(), self.rs_2D.volume_yup())
        self.assertAlmostEqual(rs_sim.volume_ylow(), self.rs_2D.volume_ylow())
        self.assertAlmostEqual(rs_sim.volume_border(), self.rs_2D.volume_border())
        self.assertAlmostEqual(rs_sim.volume_border_2(), self.rs_2D.volume_border_2())
        self.assertAlmostEqual(rs_sim.volume_border(), rs_sim.volume_border_2())
    def test_vertices_3D(self):
        # type: (ResultSetTestCase) -> None

        s1 = {(0.75, 0.500007629394531, 0.500007629394531), (0.250003814697266, 1.0, 0.500007629394531),
               (0.500007629394531, 1.0, 7.629510947e-06), (1.0, 0.250003814697266, 0.250003814697266),
               (0.75, 0.250003814697266, 0.75), (0.500007629394531, 0.75, 0.500007629394531),
               (0.75, 0.500007629394531, 1.0), (1.0, 1.0, 7.629510947e-06), (1.0, 0.500007629394531, 1.0),
               (1.0, 1.0, 1.0), (1.0, 0.500007629394531, 0.75), (0.250003814697266, 0.75, 0.500007629394531),
               (1.0, 0.500007629394531, 7.629510947e-06), (1.0, 0.250003814697266, 0.75),
               (0.500007629394531, 1.0, 0.500007629394531), (0.500007629394531, 1.0, 1.0),
               (1.0, 0.500007629394531, 0.500007629394531), (0.500007629394531, 1.0, 0.250003814697266),
               (0.75, 0.250003814697266, 1.0), (0.250003814697266, 0.75, 0.250003814697266),
               (0.500007629394531, 0.500007629394531, 1.0), (1.0, 0.250003814697266, 1.0),
               (0.500007629394531, 0.500007629394531, 7.629510947e-06), (1.0, 0.500007629394531, 0.250003814697266),
               (0.75, 0.500007629394531, 0.75), (0.500007629394531, 0.75, 0.250003814697266),
               (0.75, 0.250003814697266, 0.250003814697266), (1.0, 0.250003814697266, 0.500007629394531),
               (0.250003814697266, 1.0, 0.250003814697266), (0.75, 0.500007629394531, 0.250003814697266),
               (0.75, 0.250003814697266, 0.500007629394531)}

        s2 = {(0.749992370605469, 0.0, 0.749992370605469), (0.5, 0.0, 0.749992370605469),
               (0.749992370605469, 0.249996185186319, 0.5), (0.0, 0.0, 0.5), (0.749992370605469, 0.0, 0.5),
               (0.0, 0.5, 0.5), (0.499999999883584, 0.0, 0.999992370605469),
               (0.749992370605469, 0.249996185186319, 0.249996185186319), (0.0, 0.0, 0.0),
               (0.5, 0.249996185186319, 0.5),
               (0.0, 0.5, 0.249996185186319), (0.5, 0.249996185186319, 0.249996185186319),
               (0.0, 0.749992370605469, 0.249996185186319), (0.749992370605469, 0.0, 0.0),
               (0.249996185186319, 0.749992370605469, 0.0), (0.5, 0.0, 0.5),
               (0.5, 0.249996185186319, 0.749992370605469),
               (0.249996185186319, 0.749992370605469, 0.249996185186319),
               (0.499999999883584, 0.499999999883584, 0.999992370605469), (0.5, 0.0, 0.249996185186319),
               (0.249996185186319, 0.5, 0.249996185186319), (0.0, 0.0, 0.999992370605469),
               (0.5, 0.249996185186319, 0.0),
               (0.249996185186319, 0.5, 0.0), (0.0, 0.5, 0.0), (0.0, 0.749992370605469, 0.0), (0.5, 0.5, 0.5),
               (0.499999999883584, 0.499999999883584, 0.5), (0.749992370605469, 0.249996185186319, 0.749992370605469),
               (0.749992370605469, 0.0, 0.249996185186319), (0.0, 0.499999999883584, 0.999992370605469),
               (0.499999999883584, 0.0, 0.5), (0.5, 0.0, 0.0), (0.0, 0.499999999883584, 0.5), (0.5, 0.5, 0.0),
               (0.749992370605469, 0.249996185186319, 0.0)}

        s3 = {(0.749992370605469, 0.0, 0.250003814697266), (0.249996185186319, 0.749992370605469, 0.0),
               (0.75, 0.500007629394531, 0.250003814697266), (1.0, 0.500007629394531, 0.75),
               (1.0, 0.249996185186319, 0.5), (1.0, 0.249996185186319, 0.250003814697266), (1.0, 0.0, 0.5),
               (0.749992370605469, 0.249996185186319, 0.0), (0.749992370605469, 0.250003814697266, 0.250003814697266),
               (1.0, 0.250003814697266, 0.749992370605469), (1.0, 1.0, 7.629510947e-06),
               (0.500007629394531, 0.500007629394531, 0.500007629394531),
               (0.250003814697266, 0.749992370605469, 0.250003814697266), (0.250003814697266, 1.0, 0.0),
               (0.500007629394531, 0.5, 1.0), (0.5, 0.249996185186319, 0.500007629394531),
               (0.499999999883584, 0.0, 0.5),
               (0.749992370605469, 0.250003814697266, 0.5), (0.75, 0.0, 0.249996185186319),
               (0.5, 0.250003814697266, 0.249996185186319), (0.500007629394531, 1.0, 0.0),
               (1.0, 0.250003814697266, 0.500007629394531), (0.749992370605469, 0.0, 0.500007629394531),
               (0.749992370605469, 0.250003814697266, 0.500007629394531), (0.249996185186319, 1.0, 0.0),
               (0.749992370605469, 0.0, 1.0), (0.5, 0.500007629394531, 0.0),
               (0.749992370605469, 0.249996185186319, 0.75),
               (0.249996185186319, 0.5, 0.0), (0.250003814697266, 0.75, 0.249996185186319), (1.0, 0.0, 1.0),
               (0.5, 0.0, 0.749992370605469), (0.0, 0.5, 1.0), (1.0, 0.250003814697266, 0.250003814697266),
               (0.749992370605469, 0.250003814697266, 0.75), (0.0, 0.749992370605469, 0.249996185186319),
               (0.75, 0.500007629394531, 1.0), (1.0, 0.0, 0.500007629394531),
               (0.75, 0.249996185186319, 0.749992370605469), (1.0, 1.0, 0.0),
               (0.500007629394531, 0.749992370605469, 0.0),
               (0.499999999883584, 0.0, 0.999992370605469), (0.500007629394531, 0.499999999883584, 1.0),
               (0.5, 0.500007629394531, 1.0), (0.5, 1.0, 7.629510947e-06),
               (0.250003814697266, 0.749992370605469, 0.500007629394531), (0.5, 0.500007629394531, 0.75),
               (0.0, 0.499999999883584, 1.0), (1.0, 0.249996185186319, 0.0), (0.749992370605469, 0.0, 0.0),
               (1.0, 0.250003814697266, 1.0), (0.75, 0.500007629394531, 0.5),
               (0.500007629394531, 0.5, 0.500007629394531),
               (0.75, 0.500007629394531, 0.249996185186319), (0.500007629394531, 0.5, 7.629510947e-06),
               (0.0, 1.0, 0.500007629394531), (0.0, 0.5, 0.500007629394531),
               (0.75, 0.500007629394531, 0.749992370605469),
               (0.500007629394531, 0.75, 0.500007629394531), (0.500007629394531, 0.749992370605469, 0.250003814697266),
               (0.500007629394531, 0.5, 0.250003814697266), (0.0, 1.0, 0.250003814697266),
               (0.749992370605469, 0.500007629394531, 0.250003814697266), (0.75, 0.249996185186319, 0.75),
               (1.0, 0.500007629394531, 7.629510947e-06), (0.500007629394531, 0.500007629394531, 0.0),
               (0.500007629394531, 0.0, 0.999992370605469), (0.749992370605469, 0.249996185186319, 0.250003814697266),
               (1.0, 0.500007629394531, 0.0), (0.749992370605469, 0.0, 0.5),
               (0.0, 0.499999999883584, 0.999992370605469),
               (0.249996185186319, 0.75, 0.500007629394531), (1.0, 0.5, 0.500007629394531),
               (0.500007629394531, 1.0, 1.0),
               (0.5, 0.249996185186319, 0.749992370605469), (0.500007629394531, 0.5, 0.5),
               (0.499999999883584, 0.0, 1.0),
               (0.5, 0.0, 1.0), (0.75, 0.250003814697266, 1.0),
               (0.749992370605469, 0.250003814697266, 0.249996185186319),
               (0.0, 0.749992370605469, 0.0), (0.5, 0.249996185186319, 0.0),
               (0.749992370605469, 0.249996185186319, 0.5),
               (0.0, 0.75, 0.500007629394531), (1.0, 0.249996185186319, 0.75),
               (0.75, 0.249996185186319, 0.250003814697266), (0.5, 1.0, 0.0), (0.749992370605469, 0.0, 0.75),
               (0.499999999883584, 0.500007629394531, 0.5), (1.0, 0.0, 0.749992370605469), (0.0, 0.0, 1.0),
               (1.0, 0.500007629394531, 0.250003814697266), (0.500007629394531, 0.75, 0.250003814697266),
               (0.5, 0.500007629394531, 0.5), (0.500007629394531, 0.500007629394531, 0.5), (0.75, 0.0, 1.0),
               (0.0, 1.0, 0.0), (0.75, 0.249996185186319, 0.500007629394531), (1.0, 0.5, 7.629510947e-06),
               (0.75, 0.0, 0.749992370605469), (0.0, 0.500007629394531, 0.5),
               (0.0, 0.749992370605469, 0.500007629394531),
               (0.5, 0.5, 0.500007629394531), (0.75, 0.249996185186319, 0.5),
               (0.75, 0.500007629394531, 0.500007629394531), (0.75, 0.249996185186319, 1.0),
               (0.749992370605469, 0.500007629394531, 0.0), (0.500007629394531, 0.499999999883584, 0.5),
               (0.0, 0.5, 0.5),
               (0.749992370605469, 0.250003814697266, 0.749992370605469), (0.250003814697266, 1.0, 0.249996185186319),
               (0.249996185186319, 0.75, 0.250003814697266), (0.0, 0.500007629394531, 1.0),
               (0.250003814697266, 0.749992370605469, 0.249996185186319), (1.0, 0.250003814697266, 0.0),
               (0.75, 0.249996185186319, 0.249996185186319), (0.499999999883584, 0.499999999883584, 1.0),
               (0.0, 0.749992370605469, 0.250003814697266), (0.249996185186319, 0.5, 0.500007629394531),
               (0.5, 0.249996185186319, 0.75), (0.5, 0.0, 0.249996185186319), (1.0, 0.500007629394531, 0.5),
               (0.5, 0.0, 0.500007629394531), (0.499999999883584, 0.500007629394531, 0.999992370605469),
               (0.5, 0.249996185186319, 0.249996185186319), (1.0, 0.250003814697266, 0.5), (0.5, 0.5, 7.629510947e-06),
               (0.250003814697266, 1.0, 0.250003814697266), (1.0, 0.500007629394531, 0.500007629394531),
               (0.249996185186319, 0.749992370605469, 0.250003814697266), (0.500007629394531, 1.0, 0.500007629394531),
               (0.500007629394531, 0.499999999883584, 0.999992370605469), (0.0, 0.75, 0.249996185186319),
               (0.249996185186319, 0.75, 0.0), (0.75, 0.250003814697266, 0.249996185186319),
               (0.500007629394531, 0.75, 0.0), (0.749992370605469, 0.500007629394531, 0.75),
               (0.500007629394531, 0.5, 0.0), (0.5, 0.500007629394531, 0.749992370605469),
               (0.749992370605469, 0.500007629394531, 0.5), (0.250003814697266, 0.5, 0.500007629394531),
               (0.5, 0.249996185186319, 0.5), (0.749992370605469, 0.250003814697266, 1.0),
               (0.749992370605469, 0.250003814697266, 0.0), (0.499999999883584, 0.500007629394531, 1.0),
               (0.500007629394531, 0.0, 1.0), (0.249996185186319, 0.5, 0.250003814697266),
               (0.5, 0.500007629394531, 0.250003814697266), (0.5, 0.5, 0.0),
               (0.5, 0.500007629394531, 0.500007629394531),
               (0.500007629394531, 1.0, 0.250003814697266), (0.0, 1.0, 1.0), (0.0, 0.5, 0.249996185186319),
               (1.0, 0.5, 0.0), (0.500007629394531, 0.0, 0.5), (0.249996185186319, 1.0, 0.250003814697266),
               (1.0, 0.0, 0.250003814697266), (0.5, 1.0, 0.500007629394531), (0.0, 0.499999999883584, 0.5),
               (0.5, 0.250003814697266, 0.500007629394531), (1.0, 0.250003814697266, 0.249996185186319),
               (0.75, 0.500007629394531, 0.0), (0.75, 0.249996185186319, 0.0), (1.0, 0.0, 0.0),
               (0.5, 0.500007629394531, 7.629510947e-06), (0.5, 0.250003814697266, 0.749992370605469),
               (0.500007629394531, 1.0, 0.5), (0.249996185186319, 0.75, 0.249996185186319),
               (0.75, 0.0, 0.500007629394531), (0.5, 0.500007629394531, 0.249996185186319),
               (0.500007629394531, 0.5, 0.249996185186319), (1.0, 0.0, 0.75),
               (0.249996185186319, 0.5, 0.249996185186319),
               (0.500007629394531, 1.0, 7.629510947e-06), (0.0, 0.0, 0.999992370605469),
               (0.500007629394531, 0.500007629394531, 0.999992370605469), (1.0, 0.0, 0.249996185186319),
               (0.5, 0.249996185186319, 1.0), (0.0, 1.0, 0.5), (0.75, 0.250003814697266, 0.749992370605469),
               (0.75, 0.250003814697266, 0.500007629394531), (0.0, 0.500007629394531, 0.999992370605469),
               (0.500007629394531, 0.500007629394531, 1.0), (0.250003814697266, 0.75, 0.500007629394531),
               (0.250003814697266, 1.0, 0.500007629394531), (0.75, 0.500007629394531, 0.75),
               (0.500007629394531, 0.75, 0.249996185186319), (0.749992370605469, 0.0, 0.249996185186319),
               (0.0, 1.0, 0.249996185186319), (0.5, 0.249996185186319, 0.250003814697266),
               (0.499999999883584, 0.499999999883584, 0.5), (0.250003814697266, 0.749992370605469, 0.0),
               (1.0, 0.250003814697266, 0.75), (0.5, 0.250003814697266, 1.0),
               (0.250003814697266, 0.5, 0.249996185186319),
               (0.749992370605469, 0.0, 0.749992370605469)}

        self.assertEqual(s1, self.rs_3D.vertices_yup())
        self.assertEqual(s2, self.rs_3D.vertices_ylow())
        self.assertEqual(s3, self.rs_3D.vertices_border())
        self.assertEqual(s1.union(s2).union(s3), self.rs_3D.vertices())

        pareto_points = set(self.rs_3D.get_points_pareto())
        for p in pareto_points:
            self.assertTrue(p in self.rs_3D)

        rs = ResultSet(xspace=self.rs_3D.xspace)
        rs.set_points_pareto(pareto_points)
        pareto_points2 = set(rs.get_points_pareto())
        self.assertEqual(pareto_points, pareto_points2)
    def setUp(self):
        # type: (ResultSetTestCase) -> None
        self.files_to_clean = set()

        # Set of rectangles calculated by doc/example/example2d.py
        # Search2D stopped after 5 steps
        self.border_2D = [Rectangle((0.624992370605469, 0.249996185186319), (0.75, 0.375001907290425)),
                          Rectangle((0.125001907348633, 0.749992370605469), (0.250003814697266, 0.875003814930096)),
                          Rectangle((0.749992370605469, 0.125001907348633), (0.875003814930096, 0.250003814697266)),
                          Rectangle((0.5, 0.374994277546644), (0.625, 0.500007629394531)),
                          Rectangle((0.0, 0.874996185302734), (0.12500953685958, 1.0)),
                          Rectangle((0.874996185302734, 0.0), (1.0, 0.12500953685958)),
                          Rectangle((0.249996185186319, 0.5), (0.500007629394531, 0.75))]

        self.ylow_2D = [Rectangle((0.0, 0.0), (0.5, 0.5)),
                        Rectangle((0.5, 0.0), (0.749992370605469, 0.249996185186319)),
                        Rectangle((0.0, 0.5), (0.249996185186319, 0.749992370605469)),
                        Rectangle((0.749992370605469, 0.0), (0.874996185302734, 0.125001907348633)),
                        Rectangle((0.0, 0.749992370605469), (0.125001907348633, 0.874996185302734)),
                        Rectangle((0.5, 0.249996185186319), (0.624992370605469, 0.374994277546644))]

        self.yup_2D = [Rectangle((0.500007629394531, 0.500007629394531), (1.0, 1.0)),
                       Rectangle((0.75, 0.250003814697266), (1.0, 0.500007629394531)),
                       Rectangle((0.250003814697266, 0.75), (0.500007629394531, 1.0)),
                       Rectangle((0.875003814930096, 0.12500953685958), (1.0, 0.250003814697266)),
                       Rectangle((0.12500953685958, 0.875003814930096), (0.250003814697266, 1.0)),
                       Rectangle((0.625, 0.375001907290425), (0.75, 0.500007629394531))]

        self.xspace_2D = create_2D_space(0.0, 0.0, 1.0, 1.0)

        self.rs_2D = ResultSet(self.border_2D, self.ylow_2D, self.yup_2D, self.xspace_2D)
        self.rs2 = ResultSet()

        # Set of rectangles calculated by doc/example/example3d.py
        # Search3D stopped after 5 steps
        self.border_3D = [Rectangle((0.5, 0.5, 0.0), (0.500007629394531, 0.500007629394531, 0.500007629394531)),
                          Rectangle((0.5, 0.5, 0.0), (1.0, 0.500007629394531, 7.629510947e-06)),
                          Rectangle((0.5, 0.5, 0.0), (0.500007629394531, 1.0, 7.629510947e-06)),
                          Rectangle((0.0, 0.499999999883584, 0.999992370605469),
                                    (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.499999999883584, 0.0, 0.999992370605469),
                                    (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.499999999883584, 0.499999999883584, 0.5),
                                    (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.5, 0.5, 0.0), (0.500007629394531, 1.0, 0.500007629394531)),
                          Rectangle((0.5, 0.5, 0.0), (1.0, 0.500007629394531, 0.500007629394531)),
                          Rectangle((0.5, 0.5, 0.0), (1.0, 1.0, 7.629510947e-06)),
                          Rectangle((0.0, 0.0, 0.999992370605469), (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.499999999883584, 0.0, 0.5), (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.0, 0.499999999883584, 0.5), (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.5, 0.249996185186319, 0.5), (0.75, 0.500007629394531, 0.75)),
                          Rectangle((0.749992370605469, 0.0, 0.5), (1.0, 0.250003814697266, 0.75)),
                          Rectangle((0.5, 0.0, 0.749992370605469), (0.75, 0.250003814697266, 1.0)),
                          Rectangle((0.5, 0.0, 0.249996185186319), (0.75, 0.250003814697266, 0.500007629394531)),
                          Rectangle((0.5, 0.249996185186319, 0.0), (0.75, 0.500007629394531, 0.250003814697266)),
                          Rectangle((0.249996185186319, 0.5, 0.0), (0.500007629394531, 0.75, 0.250003814697266)),
                          Rectangle((0.0, 0.5, 0.249996185186319), (0.250003814697266, 0.75, 0.500007629394531)),
                          Rectangle((0.749992370605469, 0.0, 0.0), (1.0, 0.250003814697266, 0.250003814697266)),
                          Rectangle((0.0, 0.749992370605469, 0.0), (0.250003814697266, 1.0, 0.250003814697266)),
                          Rectangle((0.5, 0.249996185186319, 0.749992370605469), (0.75, 0.500007629394531, 1.0)),
                          Rectangle((0.749992370605469, 0.249996185186319, 0.5), (1.0, 0.500007629394531, 0.75)),
                          Rectangle((0.749992370605469, 0.0, 0.749992370605469), (1.0, 0.250003814697266, 1.0)),
                          Rectangle((0.5, 0.249996185186319, 0.249996185186319),
                                    (0.75, 0.500007629394531, 0.500007629394531)),
                          Rectangle((0.249996185186319, 0.5, 0.249996185186319),
                                    (0.500007629394531, 0.75, 0.500007629394531)),
                          Rectangle((0.749992370605469, 0.249996185186319, 0.0),
                                    (1.0, 0.500007629394531, 0.250003814697266)),
                          Rectangle((0.749992370605469, 0.0, 0.249996185186319),
                                    (1.0, 0.250003814697266, 0.500007629394531)),
                          Rectangle((0.0, 0.749992370605469, 0.249996185186319),
                                    (0.250003814697266, 1.0, 0.500007629394531)),
                          Rectangle((0.249996185186319, 0.749992370605469, 0.0),
                                    (0.500007629394531, 1.0, 0.250003814697266)),
                          Rectangle((0.0, 0.5, 0.5), (0.500007629394531, 1.0, 1.0))]

        self.ylow_3D = [Rectangle((0.0, 0.0, 0.0), (0.5, 0.5, 0.5)),
                        Rectangle((0.0, 0.5, 0.0), (0.249996185186319, 0.749992370605469, 0.249996185186319)),
                        Rectangle((0.0, 0.0, 0.5), (0.499999999883584, 0.499999999883584, 0.999992370605469)),
                        Rectangle((0.5, 0.0, 0.0), (0.749992370605469, 0.249996185186319, 0.249996185186319)),
                        Rectangle((0.5, 0.0, 0.5), (0.749992370605469, 0.249996185186319, 0.749992370605469)),
                        Rectangle((0.5, 0.5, 0.0), (0.5, 0.5, 0.0))]

        self.yup_3D = [Rectangle((0.500007629394531, 0.500007629394531, 7.629510947e-06), (1.0, 1.0, 1.0)),
                       Rectangle((0.250003814697266, 0.75, 0.250003814697266),
                                 (0.500007629394531, 1.0, 0.500007629394531)),
                       Rectangle((0.500007629394531, 0.500007629394531, 1.0),
                                 (0.500007629394531, 0.500007629394531, 1.0)),
                       Rectangle((0.75, 0.250003814697266, 0.250003814697266),
                                 (1.0, 0.500007629394531, 0.500007629394531)),
                       Rectangle((0.75, 0.250003814697266, 0.75), (1.0, 0.500007629394531, 1.0))]

        self.xspace_3D = create_3D_space(0.0, 0.0, 0.0, 1.0, 1.0, 1.0)
        self.rs_3D = ResultSet(self.border_3D, self.ylow_3D, self.yup_3D, self.xspace_3D)
class ResultSetTestCase(unittest.TestCase):

    def setUp(self):
        # type: (ResultSetTestCase) -> None
        self.files_to_clean = set()

        # Set of rectangles calculated by doc/example/example2d.py
        # Search2D stopped after 5 steps
        self.border_2D = [Rectangle((0.624992370605469, 0.249996185186319), (0.75, 0.375001907290425)),
                          Rectangle((0.125001907348633, 0.749992370605469), (0.250003814697266, 0.875003814930096)),
                          Rectangle((0.749992370605469, 0.125001907348633), (0.875003814930096, 0.250003814697266)),
                          Rectangle((0.5, 0.374994277546644), (0.625, 0.500007629394531)),
                          Rectangle((0.0, 0.874996185302734), (0.12500953685958, 1.0)),
                          Rectangle((0.874996185302734, 0.0), (1.0, 0.12500953685958)),
                          Rectangle((0.249996185186319, 0.5), (0.500007629394531, 0.75))]

        self.ylow_2D = [Rectangle((0.0, 0.0), (0.5, 0.5)),
                        Rectangle((0.5, 0.0), (0.749992370605469, 0.249996185186319)),
                        Rectangle((0.0, 0.5), (0.249996185186319, 0.749992370605469)),
                        Rectangle((0.749992370605469, 0.0), (0.874996185302734, 0.125001907348633)),
                        Rectangle((0.0, 0.749992370605469), (0.125001907348633, 0.874996185302734)),
                        Rectangle((0.5, 0.249996185186319), (0.624992370605469, 0.374994277546644))]

        self.yup_2D = [Rectangle((0.500007629394531, 0.500007629394531), (1.0, 1.0)),
                       Rectangle((0.75, 0.250003814697266), (1.0, 0.500007629394531)),
                       Rectangle((0.250003814697266, 0.75), (0.500007629394531, 1.0)),
                       Rectangle((0.875003814930096, 0.12500953685958), (1.0, 0.250003814697266)),
                       Rectangle((0.12500953685958, 0.875003814930096), (0.250003814697266, 1.0)),
                       Rectangle((0.625, 0.375001907290425), (0.75, 0.500007629394531))]

        self.xspace_2D = create_2D_space(0.0, 0.0, 1.0, 1.0)

        self.rs_2D = ResultSet(self.border_2D, self.ylow_2D, self.yup_2D, self.xspace_2D)
        self.rs2 = ResultSet()

        # Set of rectangles calculated by doc/example/example3d.py
        # Search3D stopped after 5 steps
        self.border_3D = [Rectangle((0.5, 0.5, 0.0), (0.500007629394531, 0.500007629394531, 0.500007629394531)),
                          Rectangle((0.5, 0.5, 0.0), (1.0, 0.500007629394531, 7.629510947e-06)),
                          Rectangle((0.5, 0.5, 0.0), (0.500007629394531, 1.0, 7.629510947e-06)),
                          Rectangle((0.0, 0.499999999883584, 0.999992370605469),
                                    (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.499999999883584, 0.0, 0.999992370605469),
                                    (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.499999999883584, 0.499999999883584, 0.5),
                                    (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.5, 0.5, 0.0), (0.500007629394531, 1.0, 0.500007629394531)),
                          Rectangle((0.5, 0.5, 0.0), (1.0, 0.500007629394531, 0.500007629394531)),
                          Rectangle((0.5, 0.5, 0.0), (1.0, 1.0, 7.629510947e-06)),
                          Rectangle((0.0, 0.0, 0.999992370605469), (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.499999999883584, 0.0, 0.5), (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.0, 0.499999999883584, 0.5), (0.500007629394531, 0.500007629394531, 1.0)),
                          Rectangle((0.5, 0.249996185186319, 0.5), (0.75, 0.500007629394531, 0.75)),
                          Rectangle((0.749992370605469, 0.0, 0.5), (1.0, 0.250003814697266, 0.75)),
                          Rectangle((0.5, 0.0, 0.749992370605469), (0.75, 0.250003814697266, 1.0)),
                          Rectangle((0.5, 0.0, 0.249996185186319), (0.75, 0.250003814697266, 0.500007629394531)),
                          Rectangle((0.5, 0.249996185186319, 0.0), (0.75, 0.500007629394531, 0.250003814697266)),
                          Rectangle((0.249996185186319, 0.5, 0.0), (0.500007629394531, 0.75, 0.250003814697266)),
                          Rectangle((0.0, 0.5, 0.249996185186319), (0.250003814697266, 0.75, 0.500007629394531)),
                          Rectangle((0.749992370605469, 0.0, 0.0), (1.0, 0.250003814697266, 0.250003814697266)),
                          Rectangle((0.0, 0.749992370605469, 0.0), (0.250003814697266, 1.0, 0.250003814697266)),
                          Rectangle((0.5, 0.249996185186319, 0.749992370605469), (0.75, 0.500007629394531, 1.0)),
                          Rectangle((0.749992370605469, 0.249996185186319, 0.5), (1.0, 0.500007629394531, 0.75)),
                          Rectangle((0.749992370605469, 0.0, 0.749992370605469), (1.0, 0.250003814697266, 1.0)),
                          Rectangle((0.5, 0.249996185186319, 0.249996185186319),
                                    (0.75, 0.500007629394531, 0.500007629394531)),
                          Rectangle((0.249996185186319, 0.5, 0.249996185186319),
                                    (0.500007629394531, 0.75, 0.500007629394531)),
                          Rectangle((0.749992370605469, 0.249996185186319, 0.0),
                                    (1.0, 0.500007629394531, 0.250003814697266)),
                          Rectangle((0.749992370605469, 0.0, 0.249996185186319),
                                    (1.0, 0.250003814697266, 0.500007629394531)),
                          Rectangle((0.0, 0.749992370605469, 0.249996185186319),
                                    (0.250003814697266, 1.0, 0.500007629394531)),
                          Rectangle((0.249996185186319, 0.749992370605469, 0.0),
                                    (0.500007629394531, 1.0, 0.250003814697266)),
                          Rectangle((0.0, 0.5, 0.5), (0.500007629394531, 1.0, 1.0))]

        self.ylow_3D = [Rectangle((0.0, 0.0, 0.0), (0.5, 0.5, 0.5)),
                        Rectangle((0.0, 0.5, 0.0), (0.249996185186319, 0.749992370605469, 0.249996185186319)),
                        Rectangle((0.0, 0.0, 0.5), (0.499999999883584, 0.499999999883584, 0.999992370605469)),
                        Rectangle((0.5, 0.0, 0.0), (0.749992370605469, 0.249996185186319, 0.249996185186319)),
                        Rectangle((0.5, 0.0, 0.5), (0.749992370605469, 0.249996185186319, 0.749992370605469)),
                        Rectangle((0.5, 0.5, 0.0), (0.5, 0.5, 0.0))]

        self.yup_3D = [Rectangle((0.500007629394531, 0.500007629394531, 7.629510947e-06), (1.0, 1.0, 1.0)),
                       Rectangle((0.250003814697266, 0.75, 0.250003814697266),
                                 (0.500007629394531, 1.0, 0.500007629394531)),
                       Rectangle((0.500007629394531, 0.500007629394531, 1.0),
                                 (0.500007629394531, 0.500007629394531, 1.0)),
                       Rectangle((0.75, 0.250003814697266, 0.250003814697266),
                                 (1.0, 0.500007629394531, 0.500007629394531)),
                       Rectangle((0.75, 0.250003814697266, 0.75), (1.0, 0.500007629394531, 1.0))]

        self.xspace_3D = create_3D_space(0.0, 0.0, 0.0, 1.0, 1.0, 1.0)
        self.rs_3D = ResultSet(self.border_3D, self.ylow_3D, self.yup_3D, self.xspace_3D)

    def tearDown(self):
        # type: (ResultSetTestCase) -> None
        for filename in self.files_to_clean:
            if os.path.isfile(filename):
                os.remove(filename)

    def add_file_to_clean(self, filename):
        # type: (ResultSetTestCase) -> None
        self.files_to_clean.add(filename)

    def test_files_2D(self):
        # type: (ResultSetTestCase) -> None
        tmpfile = tf.NamedTemporaryFile(delete=False)
        nfile = tmpfile.name

        self.rs_2D.to_file(nfile)
        self.rs2.from_file(nfile)

        self.assertEqual(self.rs_2D, self.rs2)
        self.assertEqual(hash(self.rs_2D), hash(self.rs2))

        temp_rs = ResultSet()
        self.assertNotEqual(self.rs_2D, temp_rs)
        self.assertNotEqual(self.rs2, temp_rs)
        del temp_rs

        print('ResultSet: {0}'.format(self.rs_2D))

        # Remove tempfile
        # os.unlink(nfile)
        self.add_file_to_clean(nfile)

    def test_files_3D(self):
        # type: (ResultSetTestCase) -> None
        tmpfile = tf.NamedTemporaryFile(delete=False)
        nfile = tmpfile.name

        self.rs_3D.to_file(nfile)
        self.rs2.from_file(nfile)

        self.assertEqual(self.rs_3D, self.rs2)
        self.assertEqual(hash(self.rs_3D), hash(self.rs2))

        temp_rs = ResultSet()
        self.assertNotEqual(self.rs_3D, temp_rs)
        self.assertNotEqual(self.rs2, temp_rs)
        del temp_rs

        print('ResultSet: {0}'.format(self.rs_3D))

        # Remove tempfile
        # os.unlink(nfile)
        self.add_file_to_clean(nfile)

    def test_vertices_2D(self):
        # type: (ResultSetTestCase) -> None

        s1 = {(0.250003814697266, 1.0), (0.875003814930096, 0.12500953685958), (0.12500953685958, 1.0),
              (0.75, 0.250003814697266), (0.625, 0.375001907290425), (0.75, 0.375001907290425),
              (0.250003814697266, 0.875003814930096), (0.625, 0.500007629394531), (1.0, 0.12500953685958),
              (0.500007629394531, 0.500007629394531), (0.250003814697266, 0.75), (0.75, 0.500007629394531),
              (0.12500953685958, 0.875003814930096), (1.0, 0.250003814697266), (0.500007629394531, 1.0),
              (0.500007629394531, 0.75), (1.0, 1.0), (0.875003814930096, 0.250003814697266),
              (1.0, 0.500007629394531)}

        s2 = {(0.125001907348633, 0.874996185302734), (0.5, 0.5), (0.249996185186319, 0.5), (0.0, 0.749992370605469),
              (0.0, 0.5), (0.874996185302734, 0.125001907348633), (0.749992370605469, 0.249996185186319),
              (0.624992370605469, 0.249996185186319), (0.125001907348633, 0.749992370605469), (0.749992370605469, 0.0),
              (0.5, 0.0), (0.624992370605469, 0.374994277546644), (0.0, 0.874996185302734), (0.5, 0.249996185186319),
              (0.0, 0.0), (0.5, 0.374994277546644), (0.874996185302734, 0.0), (0.749992370605469, 0.125001907348633),
              (0.249996185186319, 0.749992370605469)}

        s3 = {(0.0, 0.874996185302734), (0.75, 0.375001907290425), (0.249996185186319, 0.75),
              (0.875003814930096, 0.250003814697266), (0.624992370605469, 0.375001907290425),
              (0.125001907348633, 0.749992370605469), (0.874996185302734, 0.0),
              (0.749992370605469, 0.250003814697266),
              (0.249996185186319, 0.5), (0.500007629394531, 0.5), (0.500007629394531, 0.75),
              (0.250003814697266, 0.749992370605469), (0.5, 0.374994277546644), (0.12500953685958, 1.0),
              (0.250003814697266, 0.875003814930096), (0.125001907348633, 0.875003814930096),
              (0.5, 0.500007629394531),
              (0.75, 0.249996185186319), (1.0, 0.0), (0.0, 1.0), (0.624992370605469, 0.249996185186319),
              (0.625, 0.500007629394531), (1.0, 0.12500953685958), (0.625, 0.374994277546644),
              (0.12500953685958, 0.874996185302734), (0.749992370605469, 0.125001907348633),
              (0.875003814930096, 0.125001907348633), (0.874996185302734, 0.12500953685958)}

        self.assertEqual(s1, self.rs_2D.vertices_yup())
        self.assertEqual(s2, self.rs_2D.vertices_ylow())
        self.assertEqual(s3, self.rs_2D.vertices_border())
        self.assertEqual(s1.union(s2).union(s3), self.rs_2D.vertices())

        pareto_points = set(self.rs_2D.get_points_pareto())
        for p in pareto_points:
            self.assertTrue(p in self.rs_2D)

        rs = ResultSet(xspace=self.rs_2D.xspace)
        rs.set_points_pareto(pareto_points)
        pareto_points2 = set(rs.get_points_pareto())
        self.assertEqual(pareto_points, pareto_points2)

    def test_vertices_3D(self):
        # type: (ResultSetTestCase) -> None

        s1 = {(0.75, 0.500007629394531, 0.500007629394531), (0.250003814697266, 1.0, 0.500007629394531),
               (0.500007629394531, 1.0, 7.629510947e-06), (1.0, 0.250003814697266, 0.250003814697266),
               (0.75, 0.250003814697266, 0.75), (0.500007629394531, 0.75, 0.500007629394531),
               (0.75, 0.500007629394531, 1.0), (1.0, 1.0, 7.629510947e-06), (1.0, 0.500007629394531, 1.0),
               (1.0, 1.0, 1.0), (1.0, 0.500007629394531, 0.75), (0.250003814697266, 0.75, 0.500007629394531),
               (1.0, 0.500007629394531, 7.629510947e-06), (1.0, 0.250003814697266, 0.75),
               (0.500007629394531, 1.0, 0.500007629394531), (0.500007629394531, 1.0, 1.0),
               (1.0, 0.500007629394531, 0.500007629394531), (0.500007629394531, 1.0, 0.250003814697266),
               (0.75, 0.250003814697266, 1.0), (0.250003814697266, 0.75, 0.250003814697266),
               (0.500007629394531, 0.500007629394531, 1.0), (1.0, 0.250003814697266, 1.0),
               (0.500007629394531, 0.500007629394531, 7.629510947e-06), (1.0, 0.500007629394531, 0.250003814697266),
               (0.75, 0.500007629394531, 0.75), (0.500007629394531, 0.75, 0.250003814697266),
               (0.75, 0.250003814697266, 0.250003814697266), (1.0, 0.250003814697266, 0.500007629394531),
               (0.250003814697266, 1.0, 0.250003814697266), (0.75, 0.500007629394531, 0.250003814697266),
               (0.75, 0.250003814697266, 0.500007629394531)}

        s2 = {(0.749992370605469, 0.0, 0.749992370605469), (0.5, 0.0, 0.749992370605469),
               (0.749992370605469, 0.249996185186319, 0.5), (0.0, 0.0, 0.5), (0.749992370605469, 0.0, 0.5),
               (0.0, 0.5, 0.5), (0.499999999883584, 0.0, 0.999992370605469),
               (0.749992370605469, 0.249996185186319, 0.249996185186319), (0.0, 0.0, 0.0),
               (0.5, 0.249996185186319, 0.5),
               (0.0, 0.5, 0.249996185186319), (0.5, 0.249996185186319, 0.249996185186319),
               (0.0, 0.749992370605469, 0.249996185186319), (0.749992370605469, 0.0, 0.0),
               (0.249996185186319, 0.749992370605469, 0.0), (0.5, 0.0, 0.5),
               (0.5, 0.249996185186319, 0.749992370605469),
               (0.249996185186319, 0.749992370605469, 0.249996185186319),
               (0.499999999883584, 0.499999999883584, 0.999992370605469), (0.5, 0.0, 0.249996185186319),
               (0.249996185186319, 0.5, 0.249996185186319), (0.0, 0.0, 0.999992370605469),
               (0.5, 0.249996185186319, 0.0),
               (0.249996185186319, 0.5, 0.0), (0.0, 0.5, 0.0), (0.0, 0.749992370605469, 0.0), (0.5, 0.5, 0.5),
               (0.499999999883584, 0.499999999883584, 0.5), (0.749992370605469, 0.249996185186319, 0.749992370605469),
               (0.749992370605469, 0.0, 0.249996185186319), (0.0, 0.499999999883584, 0.999992370605469),
               (0.499999999883584, 0.0, 0.5), (0.5, 0.0, 0.0), (0.0, 0.499999999883584, 0.5), (0.5, 0.5, 0.0),
               (0.749992370605469, 0.249996185186319, 0.0)}

        s3 = {(0.749992370605469, 0.0, 0.250003814697266), (0.249996185186319, 0.749992370605469, 0.0),
               (0.75, 0.500007629394531, 0.250003814697266), (1.0, 0.500007629394531, 0.75),
               (1.0, 0.249996185186319, 0.5), (1.0, 0.249996185186319, 0.250003814697266), (1.0, 0.0, 0.5),
               (0.749992370605469, 0.249996185186319, 0.0), (0.749992370605469, 0.250003814697266, 0.250003814697266),
               (1.0, 0.250003814697266, 0.749992370605469), (1.0, 1.0, 7.629510947e-06),
               (0.500007629394531, 0.500007629394531, 0.500007629394531),
               (0.250003814697266, 0.749992370605469, 0.250003814697266), (0.250003814697266, 1.0, 0.0),
               (0.500007629394531, 0.5, 1.0), (0.5, 0.249996185186319, 0.500007629394531),
               (0.499999999883584, 0.0, 0.5),
               (0.749992370605469, 0.250003814697266, 0.5), (0.75, 0.0, 0.249996185186319),
               (0.5, 0.250003814697266, 0.249996185186319), (0.500007629394531, 1.0, 0.0),
               (1.0, 0.250003814697266, 0.500007629394531), (0.749992370605469, 0.0, 0.500007629394531),
               (0.749992370605469, 0.250003814697266, 0.500007629394531), (0.249996185186319, 1.0, 0.0),
               (0.749992370605469, 0.0, 1.0), (0.5, 0.500007629394531, 0.0),
               (0.749992370605469, 0.249996185186319, 0.75),
               (0.249996185186319, 0.5, 0.0), (0.250003814697266, 0.75, 0.249996185186319), (1.0, 0.0, 1.0),
               (0.5, 0.0, 0.749992370605469), (0.0, 0.5, 1.0), (1.0, 0.250003814697266, 0.250003814697266),
               (0.749992370605469, 0.250003814697266, 0.75), (0.0, 0.749992370605469, 0.249996185186319),
               (0.75, 0.500007629394531, 1.0), (1.0, 0.0, 0.500007629394531),
               (0.75, 0.249996185186319, 0.749992370605469), (1.0, 1.0, 0.0),
               (0.500007629394531, 0.749992370605469, 0.0),
               (0.499999999883584, 0.0, 0.999992370605469), (0.500007629394531, 0.499999999883584, 1.0),
               (0.5, 0.500007629394531, 1.0), (0.5, 1.0, 7.629510947e-06),
               (0.250003814697266, 0.749992370605469, 0.500007629394531), (0.5, 0.500007629394531, 0.75),
               (0.0, 0.499999999883584, 1.0), (1.0, 0.249996185186319, 0.0), (0.749992370605469, 0.0, 0.0),
               (1.0, 0.250003814697266, 1.0), (0.75, 0.500007629394531, 0.5),
               (0.500007629394531, 0.5, 0.500007629394531),
               (0.75, 0.500007629394531, 0.249996185186319), (0.500007629394531, 0.5, 7.629510947e-06),
               (0.0, 1.0, 0.500007629394531), (0.0, 0.5, 0.500007629394531),
               (0.75, 0.500007629394531, 0.749992370605469),
               (0.500007629394531, 0.75, 0.500007629394531), (0.500007629394531, 0.749992370605469, 0.250003814697266),
               (0.500007629394531, 0.5, 0.250003814697266), (0.0, 1.0, 0.250003814697266),
               (0.749992370605469, 0.500007629394531, 0.250003814697266), (0.75, 0.249996185186319, 0.75),
               (1.0, 0.500007629394531, 7.629510947e-06), (0.500007629394531, 0.500007629394531, 0.0),
               (0.500007629394531, 0.0, 0.999992370605469), (0.749992370605469, 0.249996185186319, 0.250003814697266),
               (1.0, 0.500007629394531, 0.0), (0.749992370605469, 0.0, 0.5),
               (0.0, 0.499999999883584, 0.999992370605469),
               (0.249996185186319, 0.75, 0.500007629394531), (1.0, 0.5, 0.500007629394531),
               (0.500007629394531, 1.0, 1.0),
               (0.5, 0.249996185186319, 0.749992370605469), (0.500007629394531, 0.5, 0.5),
               (0.499999999883584, 0.0, 1.0),
               (0.5, 0.0, 1.0), (0.75, 0.250003814697266, 1.0),
               (0.749992370605469, 0.250003814697266, 0.249996185186319),
               (0.0, 0.749992370605469, 0.0), (0.5, 0.249996185186319, 0.0),
               (0.749992370605469, 0.249996185186319, 0.5),
               (0.0, 0.75, 0.500007629394531), (1.0, 0.249996185186319, 0.75),
               (0.75, 0.249996185186319, 0.250003814697266), (0.5, 1.0, 0.0), (0.749992370605469, 0.0, 0.75),
               (0.499999999883584, 0.500007629394531, 0.5), (1.0, 0.0, 0.749992370605469), (0.0, 0.0, 1.0),
               (1.0, 0.500007629394531, 0.250003814697266), (0.500007629394531, 0.75, 0.250003814697266),
               (0.5, 0.500007629394531, 0.5), (0.500007629394531, 0.500007629394531, 0.5), (0.75, 0.0, 1.0),
               (0.0, 1.0, 0.0), (0.75, 0.249996185186319, 0.500007629394531), (1.0, 0.5, 7.629510947e-06),
               (0.75, 0.0, 0.749992370605469), (0.0, 0.500007629394531, 0.5),
               (0.0, 0.749992370605469, 0.500007629394531),
               (0.5, 0.5, 0.500007629394531), (0.75, 0.249996185186319, 0.5),
               (0.75, 0.500007629394531, 0.500007629394531), (0.75, 0.249996185186319, 1.0),
               (0.749992370605469, 0.500007629394531, 0.0), (0.500007629394531, 0.499999999883584, 0.5),
               (0.0, 0.5, 0.5),
               (0.749992370605469, 0.250003814697266, 0.749992370605469), (0.250003814697266, 1.0, 0.249996185186319),
               (0.249996185186319, 0.75, 0.250003814697266), (0.0, 0.500007629394531, 1.0),
               (0.250003814697266, 0.749992370605469, 0.249996185186319), (1.0, 0.250003814697266, 0.0),
               (0.75, 0.249996185186319, 0.249996185186319), (0.499999999883584, 0.499999999883584, 1.0),
               (0.0, 0.749992370605469, 0.250003814697266), (0.249996185186319, 0.5, 0.500007629394531),
               (0.5, 0.249996185186319, 0.75), (0.5, 0.0, 0.249996185186319), (1.0, 0.500007629394531, 0.5),
               (0.5, 0.0, 0.500007629394531), (0.499999999883584, 0.500007629394531, 0.999992370605469),
               (0.5, 0.249996185186319, 0.249996185186319), (1.0, 0.250003814697266, 0.5), (0.5, 0.5, 7.629510947e-06),
               (0.250003814697266, 1.0, 0.250003814697266), (1.0, 0.500007629394531, 0.500007629394531),
               (0.249996185186319, 0.749992370605469, 0.250003814697266), (0.500007629394531, 1.0, 0.500007629394531),
               (0.500007629394531, 0.499999999883584, 0.999992370605469), (0.0, 0.75, 0.249996185186319),
               (0.249996185186319, 0.75, 0.0), (0.75, 0.250003814697266, 0.249996185186319),
               (0.500007629394531, 0.75, 0.0), (0.749992370605469, 0.500007629394531, 0.75),
               (0.500007629394531, 0.5, 0.0), (0.5, 0.500007629394531, 0.749992370605469),
               (0.749992370605469, 0.500007629394531, 0.5), (0.250003814697266, 0.5, 0.500007629394531),
               (0.5, 0.249996185186319, 0.5), (0.749992370605469, 0.250003814697266, 1.0),
               (0.749992370605469, 0.250003814697266, 0.0), (0.499999999883584, 0.500007629394531, 1.0),
               (0.500007629394531, 0.0, 1.0), (0.249996185186319, 0.5, 0.250003814697266),
               (0.5, 0.500007629394531, 0.250003814697266), (0.5, 0.5, 0.0),
               (0.5, 0.500007629394531, 0.500007629394531),
               (0.500007629394531, 1.0, 0.250003814697266), (0.0, 1.0, 1.0), (0.0, 0.5, 0.249996185186319),
               (1.0, 0.5, 0.0), (0.500007629394531, 0.0, 0.5), (0.249996185186319, 1.0, 0.250003814697266),
               (1.0, 0.0, 0.250003814697266), (0.5, 1.0, 0.500007629394531), (0.0, 0.499999999883584, 0.5),
               (0.5, 0.250003814697266, 0.500007629394531), (1.0, 0.250003814697266, 0.249996185186319),
               (0.75, 0.500007629394531, 0.0), (0.75, 0.249996185186319, 0.0), (1.0, 0.0, 0.0),
               (0.5, 0.500007629394531, 7.629510947e-06), (0.5, 0.250003814697266, 0.749992370605469),
               (0.500007629394531, 1.0, 0.5), (0.249996185186319, 0.75, 0.249996185186319),
               (0.75, 0.0, 0.500007629394531), (0.5, 0.500007629394531, 0.249996185186319),
               (0.500007629394531, 0.5, 0.249996185186319), (1.0, 0.0, 0.75),
               (0.249996185186319, 0.5, 0.249996185186319),
               (0.500007629394531, 1.0, 7.629510947e-06), (0.0, 0.0, 0.999992370605469),
               (0.500007629394531, 0.500007629394531, 0.999992370605469), (1.0, 0.0, 0.249996185186319),
               (0.5, 0.249996185186319, 1.0), (0.0, 1.0, 0.5), (0.75, 0.250003814697266, 0.749992370605469),
               (0.75, 0.250003814697266, 0.500007629394531), (0.0, 0.500007629394531, 0.999992370605469),
               (0.500007629394531, 0.500007629394531, 1.0), (0.250003814697266, 0.75, 0.500007629394531),
               (0.250003814697266, 1.0, 0.500007629394531), (0.75, 0.500007629394531, 0.75),
               (0.500007629394531, 0.75, 0.249996185186319), (0.749992370605469, 0.0, 0.249996185186319),
               (0.0, 1.0, 0.249996185186319), (0.5, 0.249996185186319, 0.250003814697266),
               (0.499999999883584, 0.499999999883584, 0.5), (0.250003814697266, 0.749992370605469, 0.0),
               (1.0, 0.250003814697266, 0.75), (0.5, 0.250003814697266, 1.0),
               (0.250003814697266, 0.5, 0.249996185186319),
               (0.749992370605469, 0.0, 0.749992370605469)}

        self.assertEqual(s1, self.rs_3D.vertices_yup())
        self.assertEqual(s2, self.rs_3D.vertices_ylow())
        self.assertEqual(s3, self.rs_3D.vertices_border())
        self.assertEqual(s1.union(s2).union(s3), self.rs_3D.vertices())

        pareto_points = set(self.rs_3D.get_points_pareto())
        for p in pareto_points:
            self.assertTrue(p in self.rs_3D)

        rs = ResultSet(xspace=self.rs_3D.xspace)
        rs.set_points_pareto(pareto_points)
        pareto_points2 = set(rs.get_points_pareto())
        self.assertEqual(pareto_points, pareto_points2)

    def test_min_max_dimension_values_2D(self):
        # type: (ResultSetTestCase) -> None
        # d = 2
        d = self.yup_2D[0].dim()
        self.assertEqual(2.0, d)

        for i in range(d):
            self.assertEqual(0.12500953685958, self.rs_2D.get_min_val_dimension_yup(i))
            self.assertEqual(0.0, self.rs_2D.get_min_val_dimension_ylow(i))
            self.assertEqual(0.0, self.rs_2D.get_min_val_dimension_border(i))

            self.assertEqual(1.0, self.rs_2D.get_max_val_dimension_yup(i))
            self.assertEqual(0.874996185302734, self.rs_2D.get_max_val_dimension_ylow(i))
            self.assertEqual(1.0, self.rs_2D.get_max_val_dimension_border(i))

    def test_min_max_dimension_values_3D(self):
        # type: (ResultSetTestCase) -> None
        # d = 3
        d = self.yup_3D[0].dim()
        self.assertEqual(3.0, d)

        self.assertEqual(self.rs_3D.get_min_val_dimension_yup(0), 0.250003814697266)
        self.assertEqual(self.rs_3D.get_min_val_dimension_yup(1), 0.250003814697266)
        self.assertEqual(self.rs_3D.get_min_val_dimension_yup(2), 7.629510947e-06)
        self.assertEqual(self.rs_3D.get_min_val_dimension_ylow(0), 0.0)
        self.assertEqual(self.rs_3D.get_min_val_dimension_ylow(1), 0.0)
        self.assertEqual(self.rs_3D.get_min_val_dimension_ylow(2), 0.0)
        self.assertEqual(self.rs_3D.get_min_val_dimension_border(0), 0.0)
        self.assertEqual(self.rs_3D.get_min_val_dimension_border(1), 0.0)
        self.assertEqual(self.rs_3D.get_min_val_dimension_border(2), 0.0)

        self.assertEqual(self.rs_3D.get_max_val_dimension_yup(0), 1.0)
        self.assertEqual(self.rs_3D.get_max_val_dimension_yup(1), 1.0)
        self.assertEqual(self.rs_3D.get_max_val_dimension_yup(2), 1.0)
        self.assertEqual(self.rs_3D.get_max_val_dimension_ylow(0), 0.749992370605469)
        self.assertEqual(self.rs_3D.get_max_val_dimension_ylow(1), 0.749992370605469)
        self.assertEqual(self.rs_3D.get_max_val_dimension_ylow(2), 0.999992370605469)
        self.assertEqual(self.rs_3D.get_max_val_dimension_border(0), 1.0)
        self.assertEqual(self.rs_3D.get_max_val_dimension_border(1), 1.0)
        self.assertEqual(self.rs_3D.get_max_val_dimension_border(2), 1.0)

    def test_volume_2D(self):
        # type: (ResultSetTestCase) -> None

        self.assertEqual(0.0, self.rs_2D.overlapping_volume_yup())
        self.assertEqual(0.0, self.rs_2D.overlapping_volume_ylow())
        self.assertAlmostEqual(3.492557355327832e-10, self.rs_2D.overlapping_volume_border())
        self.assertAlmostEqual(3.492557355327832e-10, self.rs_2D.overlapping_volume_total())

        # May differ in the last decimals because of arithmetic precision
        self.assertAlmostEqual(0.42186760904587917, self.rs_2D.volume_yup())
        self.assertAlmostEqual(0.42186951636540826, self.rs_2D.volume_ylow())
        self.assertAlmostEqual(0.15626287458871257, self.rs_2D.volume_border())
        self.assertAlmostEqual(self.rs_2D.volume_border(), self.rs_2D.volume_border_2())

        # Simplify the current result set (i.e., fusion of contiguous rectangles).
        # Overlapping should disappear
        # Volume should remain identical to previous computations.
        # rs_sim = copy.deepcopy(self.rs_2D)
        rs_sim = ResultSet(self.border_2D, self.ylow_2D, self.yup_2D, self.xspace_2D)
        rs_sim.simplify()
        rs_sim.fusion()

        self.assertEqual(0.0, rs_sim.overlapping_volume_yup())
        self.assertEqual(0.0, rs_sim.overlapping_volume_ylow())
        self.assertEqual(0.0, rs_sim.overlapping_volume_border())
        self.assertEqual(0.0, rs_sim.overlapping_volume_total())

        # May differ in the last decimals because of arithmetic precision
        self.assertAlmostEqual(rs_sim.volume_yup(), self.rs_2D.volume_yup())
        self.assertAlmostEqual(rs_sim.volume_ylow(), self.rs_2D.volume_ylow())
        self.assertAlmostEqual(rs_sim.volume_border(), self.rs_2D.volume_border())
        self.assertAlmostEqual(rs_sim.volume_border_2(), self.rs_2D.volume_border_2())
        self.assertAlmostEqual(rs_sim.volume_border(), rs_sim.volume_border_2())
        # self.assertEqual(0.1562628745887126, rs_sim.volume_border_2())

    def test_volume_3D(self):
        # type: (ResultSetTestCase) -> None

        self.assertEqual(0.0, self.rs_3D.overlapping_volume_yup())
        self.assertEqual(0.0, self.rs_3D.overlapping_volume_ylow())
        self.assertAlmostEqual(1.7168407867280266e-05, self.rs_3D.overlapping_volume_border())
        self.assertAlmostEqual(2.0029416264500524e-05, self.rs_3D.overlapping_volume_total())

        # May differ in the last decimals because of arithmetic precision
        self.assertAlmostEqual(0.2968666554443193, self.rs_3D.volume_yup())
        self.assertAlmostEqual(0.2968699931807370, self.rs_3D.volume_ylow())
        self.assertAlmostEqual(0.4062633513749437, self.rs_3D.volume_border())
        self.assertAlmostEqual(self.rs_3D.volume_border(), self.rs_3D.volume_border_2())
        # self.assertAlmostEqual(0.4062633501234191, self.rs_3D.volume_border_2())

        # Simplify the current result set (i.e., fusion of contiguous rectangles).
        # Overlapping should disappear
        # Volume should remain identical to previous computations.
        # rs_sim = copy.deepcopy(self.rs_3D)
        rs_sim = ResultSet(self.border_3D, self.ylow_3D, self.yup_3D, self.xspace_3D)
        rs_sim.simplify()
        rs_sim.fusion()

        self.assertEqual(0.0, rs_sim.overlapping_volume_yup())
        self.assertEqual(0.0, rs_sim.overlapping_volume_ylow())
        self.assertEqual(0.0, rs_sim.overlapping_volume_border())
        self.assertEqual(0.0, rs_sim.overlapping_volume_total())

        # May differ in the last decimals because of arithmetic precision
        self.assertAlmostEqual(0.34374141701118816, rs_sim.volume_yup())
        self.assertAlmostEqual(0.3124945163472164, rs_sim.volume_ylow())
        self.assertAlmostEqual(0.3437640666415955, rs_sim.volume_border())
        self.assertAlmostEqual(0.3437640666415954, rs_sim.volume_border_2())
        self.assertAlmostEqual(rs_sim.volume_border(), rs_sim.volume_border_2())

    def test_points_2D(self):
        # type: (ResultSetTestCase) -> None
        n = 10
        for r in self.rs_2D.get_points_yup(n):
            self.assertTrue(self.rs_2D.member_yup(r))

        for r in self.rs_2D.get_points_ylow(n):
            self.assertTrue(self.rs_2D.member_ylow(r))

        for r in self.rs_2D.get_points_border(n):
            self.assertTrue(self.rs_2D.member_border(r))

        for r in self.rs_2D.get_points_space(n):
            self.assertTrue(self.rs_2D.member_space(r))

    def test_points_3D(self):
        # type: (ResultSetTestCase) -> None
        n = 10
        for r in self.rs_3D.get_points_yup(n):
            self.assertTrue(self.rs_3D.member_yup(r))
            # self.assertTrue(self.rs_3D.member_yup(r), 'Point {0} not in Yup {1}, in Border? {2}'.format(str(r), str(self.rs_3D.yup), str(self.rs_3D.member_border(r))))

        for r in self.rs_3D.get_points_ylow(n):
            self.assertTrue(self.rs_3D.member_ylow(r))

        for r in self.rs_3D.get_points_border(n):
            self.assertTrue(self.rs_3D.member_border(r))

        for r in self.rs_3D.get_points_space(n):
            self.assertTrue(self.rs_3D.member_space(r))

    @pytest.mark.skipif(
        'DISPLAY' not in os.environ,
        reason='Display is not defined'
    )
    def test_plot_2D(self):
        # type: (ResultSetTestCase) -> None
        tmpfile = tf.NamedTemporaryFile(delete=False)
        nfile = tmpfile.name

        # Plot 2D by screen
        self.rs_2D.plot_2D(sec=1.0)
        self.rs_2D.plot_2D_light(sec=1.0)
        self.rs_2D.plot_2D_pareto(sec=1.0)

        # Plot 2D by file
        self.rs_2D.plot_2D(sec=1.0, filename=nfile)
        self.rs_2D.plot_2D_light(sec=1.0, filename=nfile)
        self.rs_2D.plot_2D_pareto(sec=1.0, filename=nfile)

        # Remove tempfile
        # os.unlink(nfile)
        self.add_file_to_clean(nfile)

    @pytest.mark.skipif(
        'DISPLAY' not in os.environ,
        reason='Display is not defined'
    )
    def test_plot_3D(self):
        # type: (ResultSetTestCase) -> None
        tmpfile = tf.NamedTemporaryFile(delete=False)
        nfile = tmpfile.name

        # Plot 3D by screen
        self.rs_3D.plot_3D(sec=1.0)
        self.rs_3D.plot_3D_light(sec=1.0)
        self.rs_3D.plot_3D_pareto(sec=1.0)

        # Plot 3D by file
        self.rs_3D.plot_3D(sec=1.0, filename=nfile)
        self.rs_3D.plot_3D_light(sec=1.0, filename=nfile)
        self.rs_3D.plot_3D_pareto(sec=1.0, filename=nfile)

        # Remove tempfile
        # os.unlink(nfile)
        self.add_file_to_clean(nfile)
 def __init__(self, border=list(), ylow=list(), yup=list(), xspace=Rectangle()):
     # type: (ParResultSet, iter, iter, iter, Rectangle) -> None
     # super(ParResultSet, self).__init__(border, ylow, yup, xspace)
     ResultSet.__init__(self, border, ylow, yup, xspace)
     self.p = Pool(cpu_count())
Beispiel #9
0
def multidim_search_opt_0(xspace,
                          oracle,
                          epsilon=EPS,
                          delta=DELTA,
                          max_step=STEPS,
                          blocking=False,
                          sleep=0.0,
                          logging=True):
    # type: (Rectangle, Oracle, float, float, float, bool, float, bool) -> ResultSet

    # Xspace is a particular case of maximal rectangle
    # Xspace = [min_corner, max_corner]^n = [0, 1]^n
    # xspace.min_corner = (0,) * n
    # xspace.max_corner = (1,) * n

    # Dimension
    n = xspace.dim()

    # Set of comparable and incomparable rectangles, represented by 'alpha' indices
    comparable = comp(n)
    incomparable = incomp(n)
    # comparable = [zero, one]
    # incomparable = list(set(alpha) - set(comparable))
    # with:
    # zero = (0_1,...,0_n)
    # one = (1_1,...,1_n)

    # List of incomparable rectangles
    # border = [xspace]
    border = SortedListWithKey(key=Rectangle.volume)
    # border = SortedSet(key=Rectangle.volume)
    border.add(xspace)

    ylow = []
    yup = []

    # oracle function
    f = oracle.membership()

    error = (epsilon,) * n
    vol_total = xspace.volume()
    vol_yup = 0
    vol_ylow = 0
    vol_border = vol_total
    step = 0

    RootSearch.logger.debug('xspace: {0}'.format(xspace))
    RootSearch.logger.debug('vol_border: {0}'.format(vol_border))
    RootSearch.logger.debug('delta: {0}'.format(delta))
    RootSearch.logger.debug('step: {0}'.format(step))
    RootSearch.logger.debug('incomparable: {0}'.format(incomparable))
    RootSearch.logger.debug('comparable: {0}'.format(comparable))

    # Create temporary directory for storing the result of each step
    tempdir = tempfile.mkdtemp()

    RootSearch.logger.info('Report\nStep, Ylow, Yup, Border, Total, nYlow, nYup, nBorder, BinSearch')
    while (vol_border >= delta) and (step <= max_step) and (len(border) > 0):
        step = step + 1
        # if RootSearch.logger.isEnabledFor(RootSearch.logger.DEBUG):
        #    RootSearch.logger.debug('border: {0}'.format(border))
        # l.sort(key=Rectangle.volume)

        xrectangle = border.pop()

        RootSearch.logger.debug('xrectangle: {0}'.format(xrectangle))
        RootSearch.logger.debug('xrectangle.volume: {0}'.format(xrectangle.volume()))
        RootSearch.logger.debug('xrectangle.norm: {0}'.format(xrectangle.norm()))

        # y, segment
        # y = search(xrectangle.diag(), f, epsilon)
        y, steps_binsearch = binary_search(xrectangle.diag(), f, error)
        RootSearch.logger.debug('y: {0}'.format(y))

        # b0 = Rectangle(xspace.min_corner, y.low)
        b0 = Rectangle(xrectangle.min_corner, y.low)
        ylow.append(b0)
        vol_ylow += b0.volume()

        RootSearch.logger.debug('b0: {0}'.format(b0))
        RootSearch.logger.debug('ylow: {0}'.format(ylow))

        # b1 = Rectangle(y.high, xspace.max_corner)
        b1 = Rectangle(y.high, xrectangle.max_corner)
        yup.append(b1)
        vol_yup += b1.volume()

        RootSearch.logger.debug('b1: {0}'.format(b1))
        RootSearch.logger.debug('yup: {0}'.format(yup))

        yrectangle = Rectangle(y.low, y.high)
        i = irect(incomparable, yrectangle, xrectangle)
        # i = pirect(incomparable, yrectangle, xrectangle)
        # l.extend(i)

        border += i
        RootSearch.logger.debug('irect: {0}'.format(i))

        # Remove boxes in the boundary with volume 0
        # border = border[border.bisect_key_right(0.0):]
        del border[:border.bisect_key_left(0.0)]

        vol_border = vol_total - vol_yup - vol_ylow

        RootSearch.logger.info(
            '{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}'.format(step, vol_ylow, vol_yup, vol_border, vol_total,
                                                                 len(ylow), len(yup), len(border),
                                                                 steps_binsearch))
        if sleep > 0.0:
            rs = ResultSet(border, ylow, yup, xspace)
            if n == 2:
                rs.plot_2D_light(blocking=blocking, sec=sleep, opacity=0.7)
            elif n == 3:
                rs.plot_3D_light(blocking=blocking, sec=sleep, opacity=0.7)

        if logging:
            rs = ResultSet(border, ylow, yup, xspace)
            name = os.path.join(tempdir, str(step))
            rs.to_file(name)

    return ResultSet(border, ylow, yup, xspace)
Beispiel #10
0
def multidim_search_opt_3(xspace,
                          oracle,
                          epsilon=EPS,
                          delta=DELTA,
                          max_step=STEPS,
                          blocking=False,
                          sleep=0.0,
                          logging=True):
    # type: (Rectangle, Oracle, float, float, float, bool, float, bool) -> ResultSet

    # xspace is a particular case of maximal rectangle
    # xspace = [min_corner, max_corner]^n = [0, 1]^n
    # xspace.min_corner = (0,) * n
    # xspace.max_corner = (1,) * n

    # Dimension
    n = xspace.dim()

    # Set of comparable and incomparable rectangles, represented by 'alpha' indices
    comparable = comp(n)
    incomparable = incomp(n)
    # comparable = [zero, one]
    # incomparable = list(set(alpha) - set(comparable))
    # with:
    # zero = (0_1,...,0_n)
    # one = (1_1,...,1_n)

    # List of incomparable rectangles
    # border = [xspace]
    # border = SortedListWithKey(key=Rectangle.volume)
    border = SortedSet([], key=Rectangle.volume)
    border.add(xspace)

    lattice_border_ylow = Lattice(dim=xspace.dim(), key=lambda x: x.min_corner)
    lattice_border_yup = Lattice(dim=xspace.dim(), key=lambda x: x.max_corner)

    lattice_border_ylow.add(xspace)
    lattice_border_yup.add(xspace)

    ylow = []
    yup = []

    # x_minimal = points from 'x' that are strictly incomparable (Pareto optimal)
    ylow_minimal = []
    yup_minimal = []

    # oracle function
    f = oracle.membership()

    error = (epsilon,) * n
    vol_total = xspace.volume()
    vol_yup = 0
    vol_ylow = 0
    vol_border = vol_total
    step = 0

    RootSearch.logger.debug('xspace: {0}'.format(xspace))
    RootSearch.logger.debug('vol_border: {0}'.format(vol_border))
    RootSearch.logger.debug('delta: {0}'.format(delta))
    RootSearch.logger.debug('step: {0}'.format(step))
    RootSearch.logger.debug('incomparable: {0}'.format(incomparable))
    RootSearch.logger.debug('comparable: {0}'.format(comparable))

    # Create temporary directory for storing the result of each step
    tempdir = tempfile.mkdtemp()

    RootSearch.logger.info(
        'Report\nStep, Ylow, Yup, Border, Total, nYlow, nYup, nBorder, BinSearch, nBorder dominated by Ylow, nBorder dominated by Yup')
    while (vol_border >= delta) and (step <= max_step) and (len(border) > 0):
        step = step + 1
        # if RootSearch.logger.isEnabledFor(RootSearch.logger.DEBUG):
        #    RootSearch.logger.debug('border: {0}'.format(border))
        # l.sort(key=Rectangle.volume)

        xrectangle = border.pop()

        lattice_border_ylow.remove(xrectangle)
        lattice_border_yup.remove(xrectangle)

        RootSearch.logger.debug('xrectangle: {0}'.format(xrectangle))
        RootSearch.logger.debug('xrectangle.volume: {0}'.format(xrectangle.volume()))
        RootSearch.logger.debug('xrectangle.norm: {0}'.format(xrectangle.norm()))

        # y, segment
        # y = search(xrectangle.diag(), f, epsilon)
        y, steps_binsearch = binary_search(xrectangle.diag(), f, error)
        RootSearch.logger.debug('y: {0}'.format(y))
        # discovered_segments.append(y)

        # b0 = Rectangle(xrectangle.min_corner, y.low)
        # b1 = Rectangle(y.high, xrectangle.max_corner)
        #
        # ylow.append(b0)
        # yup.append(b1)
        #
        # vol_ylow += b0.volume()
        # vol_yup += b1.volume()

        ################################
        # Every Border rectangle that dominates B0 is included in Ylow
        b0_extended = Rectangle(xspace.min_corner, y.low)
        # border_overlapping_b0 = [rect for rect in border if rect.overlaps(b0_extended)]
        # border_overlapping_b0 = [rect for rect in border_overlapping_b0 if rect.overlaps(b0_extended)]
        ylow_rectangle = Rectangle(y.low, y.low)
        border_overlapping_b0 = lattice_border_ylow.less_equal(ylow_rectangle)
        # border_intersecting_b0 = [b0_extended.intersection(rect) for rect in border_overlapping_b0]

        ## border_nondominatedby_b0 = [rect - b0_extended for rect in border_overlapping_b0]
        # border_nondominatedby_b0 = []
        # for rect in border_overlapping_b0:
        #     border_nondominatedby_b0 += list(rect - b0_extended)

        list_idwc = (idwc(b0_extended, rect) for rect in border_overlapping_b0)
        border_nondominatedby_b0 = set(itertools.chain.from_iterable(list_idwc))
        # border_nondominatedby_b0 = Rectangle.fusion_rectangles(border_nondominatedby_b0)

        # if 'rect' is completely dominated by b0_extended (i.e., rect is strictly inside b0_extended), then
        # set(rect - b0_extended) == {rect}
        # Therefore, 'rect' must be removed from 'non dominated' borders

        border |= border_nondominatedby_b0
        border -= border_overlapping_b0

        lattice_border_ylow.add_list(border_nondominatedby_b0)
        lattice_border_ylow.remove_list(border_overlapping_b0)

        lattice_border_yup.add_list(border_nondominatedby_b0)
        lattice_border_yup.remove_list(border_overlapping_b0)

        # Every Border rectangle that is dominated by B1 is included in Yup
        b1_extended = Rectangle(y.high, xspace.max_corner)
        # border_overlapping_b1 = [rect for rect in border if rect.overlaps(b1_extended)]
        # border_overlapping_b1 = [rect for rect in border_overlapping_b1 if rect.overlaps(b1_extended)]
        yup_rectangle = Rectangle(y.high, y.high)
        border_overlapping_b1 = lattice_border_yup.greater_equal(yup_rectangle)
        # border_intersecting_b1 = [b1_extended.intersection(rect) for rect in border_overlapping_b1]

        ## border_nondominatedby_b1 = [rect - b1_extended for rect in border_overlapping_b1]
        # border_nondominatedby_b1 = []
        # for rect in border_overlapping_b1:
        #     border_nondominatedby_b1 += list(rect - b1_extended)

        list_iuwc = (iuwc(b1_extended, rect) for rect in border_overlapping_b1)
        border_nondominatedby_b1 = set(itertools.chain.from_iterable(list_iuwc))
        # border_nondominatedby_b1 = Rectangle.fusion_rectangles(border_nondominatedby_b1)

        # if 'rect' is completely dominated by b1_extended (i.e., rect is strictly inside b1_extended), then
        # set(rect - b1_extended) == {rect}
        # Therefore, 'rect' must be removed from 'non dominated' borders

        border |= border_nondominatedby_b1
        border -= border_overlapping_b1

        lattice_border_ylow.add_list(border_nondominatedby_b1)
        lattice_border_ylow.remove_list(border_overlapping_b1)

        lattice_border_yup.add_list(border_nondominatedby_b1)
        lattice_border_yup.remove_list(border_overlapping_b1)

        db0 = Rectangle.difference_rectangles(b0_extended, ylow_minimal)
        db1 = Rectangle.difference_rectangles(b1_extended, yup_minimal)

        vol_db0 = sum(b0.volume() for b0 in db0)
        vol_db1 = sum(b1.volume() for b1 in db1)

        # rs = ResultSet([], border_intersecting_b0 + [b0], border_intersecting_b1 + [b1], Rectangle())
        # vol_db0 = rs.volume_ylow() - rs.overlapping_volume_ylow()
        # vol_db1 = rs.volume_yup() - rs.overlapping_volume_yup()

        vol_ylow += vol_db0
        vol_yup += vol_db1

        ylow.extend(db0)
        yup.extend(db1)

        ylow_minimal.append(b0_extended)
        yup_minimal.append(b1_extended)

        RootSearch.logger.debug('b0: {0}'.format(db0))
        RootSearch.logger.debug('b1: {0}'.format(db1))

        RootSearch.logger.debug('ylow: {0}'.format(ylow))
        RootSearch.logger.debug('yup: {0}'.format(yup))

        ################################
        # Every rectangle in 'i' is incomparable for current B0 and for all B0 included in Ylow
        # Every rectangle in 'i' is incomparable for current B1 and for all B1 included in Yup
        ################################

        yrectangle = Rectangle(y.low, y.high)
        i = irect(incomparable, yrectangle, xrectangle)
        # i = pirect(incomparable, yrectangle, xrectangle)
        # l.extend(i)

        border |= i
        RootSearch.logger.debug('irect: {0}'.format(i))

        lattice_border_ylow.add_list(i)
        lattice_border_yup.add_list(i)

        # Remove boxes in the boundary with volume 0
        boxes_null_vol = border[:border.bisect_key_left(0.0)]
        border -= boxes_null_vol
        lattice_border_ylow.remove_list(boxes_null_vol)
        lattice_border_yup.remove_list(boxes_null_vol)

        vol_border = vol_total - vol_yup - vol_ylow

        RootSearch.logger.info('{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}'
                               .format(step, vol_ylow, vol_yup, vol_border, vol_total, len(ylow), len(yup), len(border),
                                       steps_binsearch,
                                       len(border_overlapping_b0), len(border_overlapping_b1)))
        if sleep > 0.0:
            rs = ResultSet(border, ylow, yup, xspace)
            if n == 2:
                rs.plot_2D_light(blocking=blocking, sec=sleep, opacity=0.7)
            elif n == 3:
                rs.plot_3D_light(blocking=blocking, sec=sleep, opacity=0.7)

        if logging:
            rs = ResultSet(border, ylow, yup, xspace)
            name = os.path.join(tempdir, str(step))
            rs.to_file(name)

    return ResultSet(border, ylow, yup, xspace)
Beispiel #11
0
def multidim_search_opt_1(xspace,
                          oracle,
                          epsilon=EPS,
                          delta=DELTA,
                          max_step=STEPS,
                          blocking=False,
                          sleep=0.0,
                          logging=True):
    # type: (Rectangle, Oracle, float, float, float, bool, float, bool) -> ResultSet

    # Xspace is a particular case of maximal rectangle
    # Xspace = [min_corner, max_corner]^n = [0, 1]^n
    # xspace.min_corner = (0,) * n
    # xspace.max_corner = (1,) * n

    # Dimension
    n = xspace.dim()

    # Set of comparable and incomparable rectangles, represented by 'alpha' indices
    comparable = comp(n)
    incomparable = incomp(n)
    # comparable = [zero, one]
    # incomparable = list(set(alpha) - set(comparable))
    # with:
    # zero = (0_1,...,0_n)
    # one = (1_1,...,1_n)

    # List of incomparable rectangles
    # border = [xspace]
    # border = SortedListWithKey(key=Rectangle.volume)
    border = SortedSet([], key=Rectangle.volume)
    border.add(xspace)

    ylow = []
    yup = []

    # oracle function
    f = oracle.membership()

    error = (epsilon,) * n
    vol_total = xspace.volume()
    vol_yup = 0
    vol_ylow = 0
    vol_border = vol_total
    step = 0

    RootSearch.logger.debug('xspace: {0}'.format(xspace))
    RootSearch.logger.debug('vol_border: {0}'.format(vol_border))
    RootSearch.logger.debug('delta: {0}'.format(delta))
    RootSearch.logger.debug('step: {0}'.format(step))
    RootSearch.logger.debug('incomparable: {0}'.format(incomparable))
    RootSearch.logger.debug('comparable: {0}'.format(comparable))

    # Create temporary directory for storing the result of each step
    tempdir = tempfile.mkdtemp()

    RootSearch.logger.info(
        'Report\nStep, Ylow, Yup, Border, Total, nYlow, nYup, nBorder, BinSearch, nBorder dominated by Ylow, nBorder dominated by Yup')
    while (vol_border >= delta) and (step <= max_step) and (len(border) > 0):
        step = step + 1
        # if RootSearch.logger.isEnabledFor(RootSearch.logger.DEBUG):
        #    RootSearch.logger.debug('border: {0}'.format(border))
        # l.sort(key=Rectangle.volume)

        xrectangle = border.pop()

        RootSearch.logger.debug('xrectangle: {0}'.format(xrectangle))
        RootSearch.logger.debug('xrectangle.volume: {0}'.format(xrectangle.volume()))
        RootSearch.logger.debug('xrectangle.norm: {0}'.format(xrectangle.norm()))

        # y, segment
        # y = search(xrectangle.diag(), f, epsilon)
        y, steps_binsearch = binary_search(xrectangle.diag(), f, error)
        RootSearch.logger.debug('y: {0}'.format(y))
        # discovered_segments.append(y)

        b0 = Rectangle(xrectangle.min_corner, y.low)
        b1 = Rectangle(y.high, xrectangle.max_corner)

        ylow.append(b0)
        yup.append(b1)

        vol_ylow += b0.volume()
        vol_yup += b1.volume()

        RootSearch.logger.debug('b0: {0}'.format(b0))
        RootSearch.logger.debug('b1: {0}'.format(b1))

        RootSearch.logger.debug('ylow: {0}'.format(ylow))
        RootSearch.logger.debug('yup: {0}'.format(yup))

        ################################
        # Every Border rectangle that dominates B0 is included in Ylow
        # Every Border rectangle that is dominated by B1 is included in Yup
        b0_extended = Rectangle(xspace.min_corner, y.low)
        b1_extended = Rectangle(y.high, xspace.max_corner)

        # Every cube in the boundary overlaps another cube in the boundary
        # When cubes from the boundary are moved to ylow or yup, they may still have a complementary cube
        # remaining in the boundary with a non-empty intersection.
        border_overlapping_ylow = [r for r in ylow if r.overlaps(b0_extended)]
        border_overlapping_yup = [r for r in yup if r.overlaps(b1_extended)]

        border_overlapping_b0 = [rect for rect in border if rect.overlaps(b0_extended)]
        # Warning: Be aware of the overlapping areas of the cubes in the border.
        # If we calculate the intersection of b0_extended with all the cubes in the frontier, and two cubes
        # 'a' and 'b' partially overlaps, then the volume of this overlapping portion will be counted twice
        # border_dominatedby_b0 = [rect.intersection(b0_extended) for rect in border_overlapping_b0]
        # Solution: Project the 'shadow' of the cubes in the border over b0_extended.
        border_dominatedby_b0_shadow = Rectangle.difference_rectangles(b0_extended, border_overlapping_b0)

        # The negative of this image returns a set of cubes in the boundary without overlapping.
        # border_dominatedby_b0 will be appended to ylow.
        # Remove the portion of the negative that overlaps any cube that is already appended to ylow
        border_dominatedby_b0 = Rectangle.difference_rectangles(b0_extended,
                                                                border_dominatedby_b0_shadow + border_overlapping_ylow)

        # border_nondominatedby_b0 = [rect - b0_extended for rect in border_overlapping_b0]

        border_nondominatedby_b0 = []
        for rect in border_overlapping_b0:
            border_nondominatedby_b0 += list(rect - b0_extended)

        # border_nondominatedby_b0 = set()
        # for rect in border_overlapping_b0:
        #    border_nondominatedby_b0 |= set(rect - b0_extended)
        # border_nondominatedby_b0 -= set(border_overlapping_b0)

        # if 'rect' is completely dominated by b0_extended (i.e., rect is strictly inside b0_extended), then
        # set(rect - b0_extended) == {rect}
        # Therefore, 'rect' must be removed from 'non dominated' borders

        # border -= border_overlapping_b0
        border |= border_nondominatedby_b0
        border -= border_overlapping_b0

        border_overlapping_b1 = [rect for rect in border if rect.overlaps(b1_extended)]
        # Warning: Be aware of the overlapping areas of the cubes in the border.
        # If we calculate the intersection of b1_extended with all the cubes in the frontier, and two cubes
        # 'a' and 'b' partially overlaps, then the volume of this overlapping portion will be considered twice
        # border_dominatedby_b1 = [rect.intersection(b1_extended) for rect in border_overlapping_b1]
        # Solution: Project the 'shadow' of the cubes in the border over b1_extended.
        border_dominatedby_b1_shadow = Rectangle.difference_rectangles(b1_extended, border_overlapping_b1)

        # The negative of this image returns a set of cubes in the boundary without overlapping.
        # border_dominatedby_b1 will be appended to yup.
        # Remove the portion of the negative that overlaps any cube that is already appended to yup
        border_dominatedby_b1 = Rectangle.difference_rectangles(b1_extended,
                                                                border_dominatedby_b1_shadow + border_overlapping_yup)

        # border_nondominatedby_b1 = [rect - b1_extended for rect in border_overlapping_b1]

        border_nondominatedby_b1 = []
        for rect in border_overlapping_b1:
            border_nondominatedby_b1 += list(rect - b1_extended)

        # border_nondominatedby_b1 = set()
        # for rect in border_overlapping_b1:
        #    border_nondominatedby_b1 |= set(rect - b1_extended)
        # border_nondominatedby_b1 -= set(border_overlapping_b1)

        # if 'rect' is completely dominated by b1_extended (i.e., rect is strictly inside b1_extended), then
        # set(rect - b1_extended) == {rect}
        # Therefore, 'rect' must be removed from 'non dominated' borders

        # border -= border_overlapping_b1
        border |= border_nondominatedby_b1
        border -= border_overlapping_b1

        ylow.extend(border_dominatedby_b0)
        yup.extend(border_dominatedby_b1)

        vol_ylow += sum(b0.volume() for b0 in border_dominatedby_b0)
        vol_yup += sum(b1.volume() for b1 in border_dominatedby_b1)

        ################################
        # Every rectangle in 'i' is incomparable for current B0 and for all B0 included in Ylow
        # Every rectangle in 'i' is incomparable for current B1 and for all B1 included in Yup
        ################################

        yrectangle = Rectangle(y.low, y.high)
        i = irect(incomparable, yrectangle, xrectangle)
        # i = pirect(incomparable, yrectangle, xrectangle)
        # l.extend(i)

        border |= i
        RootSearch.logger.debug('irect: {0}'.format(i))

        # Remove boxes in the boundary with volume 0
        border -= border[:border.bisect_key_left(0.0)]

        vol_border = vol_total - vol_yup - vol_ylow

        RootSearch.logger.info('{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}'
                               .format(step, vol_ylow, vol_yup, vol_border, vol_total, len(ylow), len(yup), len(border),
                                       steps_binsearch,
                                       len(border_overlapping_b0), len(border_overlapping_b1)))
        if sleep > 0.0:
            rs = ResultSet(border, ylow, yup, xspace)
            if n == 2:
                rs.plot_2D_light(blocking=blocking, sec=sleep, opacity=0.7)
            elif n == 3:
                rs.plot_3D_light(blocking=blocking, sec=sleep, opacity=0.7)

        if logging:
            rs = ResultSet(border, ylow, yup, xspace)
            name = os.path.join(tempdir, str(step))
            rs.to_file(name)

    return ResultSet(border, ylow, yup, xspace)
Beispiel #12
0
from ParetoLib.Oracle.OracleFunction import OracleFunction
from ParetoLib.Search.ResultSet import ResultSet

# File containing the definition of the Oracle
nfile = '../../Tests/Oracle/OracleFunction/2D/test1.txt'
human_readable = True

oracle = OracleFunction()
oracle.from_file(nfile, human_readable)

rs = ResultSet()
rs.from_file("result.zip")
rs.plot_2D_light(var_names=oracle.get_var_names())