Пример #1
0
class ChTest(unittest.TestCase):

    def setUp(self):
        rects = [(2, 12), (12, 2), (4, 17), (17, 4), (5, 14), (14, 5), (7, 25), (25, 7), (22, 14), (14, 22), (12, 29), (29, 12), (18, 24), (24, 18), (22, 20), (20, 22), (26, 18), (18, 26), (18, 27), (27, 18)]
        self.g = Guillotine((60, 60), rects)
        self.cut = (0, 0, 17, 4, 1, (17, 0, 17, 4, 1, (34, 0, 17, 4, 1, None, None), None), (0, 4, 12, 29, 1, (12, 4, 12, 2, 1, None, (12, 6, 18, 24, 1, (30, 6, 12, 2, 1, (42, 6, 12, 2, 1, None, None), (30, 8, 12, 2, 1, None, (30, 10, 25, 7, 1, None, (30, 17, 14, 5, 1, (44, 17, 12, 2, 1, None, None), None)))), None)), (0, 33, 22, 20, 1, (22, 33, 12, 2, 1, (34, 33, 12, 2, 1, None, None), (22, 35, 5, 14, 1, (27, 35, 5, 14, 1, (32, 35, 2, 12, 1, (34, 35, 2, 12, 1, (36, 35, 2, 12, 1, (38, 35, 2, 12, 1, (40, 35, 2, 12, 1, (42, 35, 2, 12, 1, (44, 35, 2, 12, 1, (46, 35, 2, 12, 1, (48, 35, 12, 2, 1, None, None), None), None), None), None), None), None), None), None), None), (22, 49, 17, 4, 1, None, None))), None)))
        self.pieces = [(17, 4), (17, 4), (17, 4), (12, 29), (12, 2), (18, 24), (12, 2), (12, 2), (12, 2), (25, 7), (14, 5), (12, 2), (22, 20), (12, 2), (12, 2), (5, 14), (5, 14), (2, 12), (2, 12), (2, 12), (2, 12), (2, 12), (2, 12), (2, 12), (2, 12), (12, 2), (17, 4)]
        self.sorted_pieces = sorted(self.pieces, key=cmp_to_key(self.__cmp))

    def test_lower_y(self):
        pieces = sorted(self.g.pieces(self.cut), key=cmp_to_key(self.__cmp))
        self.assertEqual(len(self.sorted_pieces), len(pieces))
        self.assertEqual(self.sorted_pieces, pieces)

    def test_cut(self):
        pieces = self.g.pieces(self.cut)
        self.assertEqual(self.cut, self.g.cut(pieces))

    def __cmp(self, a, b):
        if a[0] == b[0]:
            return a[1] - b[1]
        else:
            return a[0] - b[0]
Пример #2
0
    def loading_structural(self, structpath=None):
        """
        Loading structural data.
        """
        # load structural volume
        print "Loading structural information file"
        self.structpath = structpath
        self.img = nib.load(self.structpath)
        data = self.img.get_data()
        self.affine = self.img.get_affine()
        self.dims = data.shape[:3]

        # verifying if structural is color_fa created by TrackVis :: Diffusion Toolkit
        if data.dtype == [('R', '|u1'), ('G', '|u1'), ('B', '|u1')]:
            data1 = data.view((np.uint8, len(data.dtype.names)))
            data = data1
            del data1

        # Create the Guillotine object
        data = (np.interp(data, [data.min(), data.max()], [0, 255]))
        self.guil = Guillotine('Volume Slicer', data, np.copy(self.affine))
        self.scene.add_actor(self.guil)
Пример #3
0
from simulated_anealing_cut2d import SimulatedAnnealing
from plotter import Painter
from guillotine_draw import PainterGui

cut_file_path = 'cut.png'
plot_file_path = 'plot.png'

W, H, n, a, b, c, d, e = [int(s) for s in input().split(" ")]

rects = []

for i in range(0, int(n)):
    w, h, a, b, c = [int(s) for s in input().split(" ")]
    rects.append((w, h))

g = Guillotine((W, H), rects)
p = Painter()
s = SimulatedAnnealing(W, H, rects)
initial = s.initial_solution()
costs, temperatures, solution, stats = SimulatedAnnealing.execute(s, initial)

#stats: (self.__waste(solution), len(self.rects), WxH, self.__cost(start), average, self.__cost(solution))
cut = g.cut(solution)

gui = PainterGui(W, H)
gui.drawGuillotine(cut)
gui.save(cut_file_path)
gui.end()

p.draw(costs, temperatures, plot_file_path)
Пример #4
0
 def setUp(self):
     rects = [(2, 12), (12, 2), (4, 17), (17, 4), (5, 14), (14, 5), (7, 25), (25, 7), (22, 14), (14, 22), (12, 29), (29, 12), (18, 24), (24, 18), (22, 20), (20, 22), (26, 18), (18, 26), (18, 27), (27, 18)]
     self.g = Guillotine((60, 60), rects)
     self.cut = (0, 0, 17, 4, 1, (17, 0, 17, 4, 1, (34, 0, 17, 4, 1, None, None), None), (0, 4, 12, 29, 1, (12, 4, 12, 2, 1, None, (12, 6, 18, 24, 1, (30, 6, 12, 2, 1, (42, 6, 12, 2, 1, None, None), (30, 8, 12, 2, 1, None, (30, 10, 25, 7, 1, None, (30, 17, 14, 5, 1, (44, 17, 12, 2, 1, None, None), None)))), None)), (0, 33, 22, 20, 1, (22, 33, 12, 2, 1, (34, 33, 12, 2, 1, None, None), (22, 35, 5, 14, 1, (27, 35, 5, 14, 1, (32, 35, 2, 12, 1, (34, 35, 2, 12, 1, (36, 35, 2, 12, 1, (38, 35, 2, 12, 1, (40, 35, 2, 12, 1, (42, 35, 2, 12, 1, (44, 35, 2, 12, 1, (46, 35, 2, 12, 1, (48, 35, 12, 2, 1, None, None), None), None), None), None), None), None), None), None), None), (22, 49, 17, 4, 1, None, None))), None)))
     self.pieces = [(17, 4), (17, 4), (17, 4), (12, 29), (12, 2), (18, 24), (12, 2), (12, 2), (12, 2), (25, 7), (14, 5), (12, 2), (22, 20), (12, 2), (12, 2), (5, 14), (5, 14), (2, 12), (2, 12), (2, 12), (2, 12), (2, 12), (2, 12), (2, 12), (2, 12), (12, 2), (17, 4)]
     self.sorted_pieces = sorted(self.pieces, key=cmp_to_key(self.__cmp))
class SimulatedAnnealing:
    MAX_INTERATIONS = 400  # iterações
    MAX_RANDOMIZE = 1000  # perturbações
    MAX_SUCESS = 1000000  # sucessos
    ALPHA = 0.8

    def __init__(self, w, h, rects):
        self.w = w
        self.h = h
        self.rects = rects
        self.guillotine = Guillotine((w, h), rects)

    def __cut2d(self, pieces):
        return sum([x[0] * x[1] for x in pieces])

    def __cost(self, solution):
        return self.__cut2d(solution)

    def __diff_solution(self, solutionA, solutionB):
        return self.__cost(solutionA) - self.__cost(solutionB)

    def __randomize(self, solution):
        return self.guillotine.change(solution)

    def initial_solution(self):
        area = 0
        pieces = []
        while area < self.w * self.h:
            p = choice(self.rects)
            pieces.append(p)
            area += p[0] * p[1]

        cut = self.guillotine.cut(pieces)

        return self.guillotine.pieces(cut)

    def __diff_values(self, solution):
        self.start = solution
        for i in range(0, 100):
            initial_solution = solution
            solution = self.initial_solution()

            if self.__cost(solution) > self.__cost(self.start):
                self.start = solution

            diff_s = self.__diff_solution(initial_solution, solution)

            if diff_s > 0:
                yield diff_s

    def __initial_temperature(self, solution):
        SECRET = 4.48  # DESCRIBE THIS VARIABLE
        diffs = self.__diff_values(solution)
        l = list(diffs)
        return SECRET * (sum(l) / len(l))

    def __waste(self, solution):
        total = self.w * self.h
        return ((total - self.__cost(solution)) / total) * 100

    def execute(self, start):
        t0 = time.time()
        temperature = self.__initial_temperature(start)
        success_iterator = 0
        temperatures = []
        costs = []
        solutions = []
        solution = self.start

        no_change_counter = 0
        for j in range(0, self.MAX_INTERATIONS):
            for i in range(0, self.MAX_RANDOMIZE):
                new_solution = self.__randomize(solution)
                diff_s = self.__diff_solution(new_solution, solution)

                try:
                    if diff_s >= 0 or math.exp(
                            -diff_s / temperature) > uniform(0, 1):
                        solution = new_solution
                        success_iterator = success_iterator + 1

                except:
                    pass

                if success_iterator >= self.MAX_SUCESS:
                    break

            solutions.insert(j, self.__cost(solution))
            if j > 0 and solutions[j] == solutions[j - 1]:
                no_change_counter += 1
            else:
                no_change_counter = 0

            temperatures.append(temperature)
            costs.append(self.__cost(solution))

            temperature = self.ALPHA * temperature

            if success_iterator == 0 or 0.2 * self.MAX_INTERATIONS <= no_change_counter:  # stop condition
                break

            average = sum(solutions) / len(solutions)

        return temperatures, costs, solution, (self.__waste(solution),
                                               len(self.rects),
                                               "{0}x{1}".format(
                                                   self.w, self.h),
                                               self.__cost(start), average,
                                               self.__cost(solution),
                                               time.time() - t0, "{0}".format(
                                                   dict(Counter(solution))))
 def __init__(self, w, h, rects):
     self.w = w
     self.h = h
     self.rects = rects
     self.guillotine = Guillotine((w, h), rects)