Ejemplo n.º 1
0
    def __init__(self,
                 jobs,
                 x,
                 y,
                 xspacing,
                 yspacing,
                 searchTimeout,
                 baseTiling=None):
        # Initialize the base class first
        super().__init__(jobs, x, y, xspacing, yspacing, searchTimeout)

        # Track the possible permutations given the `jobs` provided
        # There are (2**N)*(N!) possible permutations where N is the number of jobs.
        # This is assuming all jobs are unique and each job has a rotation (i.e., is not
        # square). Practically, these assumptions make no difference because the software
        # currently doesn't optimize for cases of repeated jobs.
        self.possiblePermutations = (2**len(jobs)) * factorial(len(jobs))

        # Track the number of permutations checked so far
        self.permutations = 0

        # Store the tiling we've already encountered, otherwise generate
        # a default tiling
        if baseTiling:
            self.baseTiling = baseTiling
        else:
            self.baseTiling = tiling.Tiling(self.x, self.y, self.xspacing,
                                            self.yspacing)
Ejemplo n.º 2
0
    def __init__(self, jobs, x, y, xspacing, yspacing, searchTimeout,
                 exhaustiveSearchJobs):
        super().__init__(jobs, x, y, xspacing, yspacing, searchTimeout)

        # We also store a blank tiling to start
        self.baseTiling = tiling.Tiling(x, y, self.xspacing, self.yspacing)

        # Record the number of jobs that should be searched exhaustively
        self.RandomSearchExhaustiveJobs = exhaustiveSearchJobs
Ejemplo n.º 3
0
def tile_search1(Jobs, X, Y):
    """Wrapper around _tile_search1 to handle keyboard interrupt, etc."""
    global _StartTime, _CkpointTime, _Placements, _TBestTiling, _TBestScore, _Permutations, _PossiblePermutations

    initialize()

    _StartTime = time.time()
    _CkpointTime = _StartTime + 3
    # There are (2**N)*(N!) possible permutations where N is the number of jobs.
    # This is assuming all jobs are unique and each job has a rotation (i.e., is not
    # square). Practically, these assumptions make no difference because the software
    # currently doesn't optimize for cases of repeated jobs.
    _PossiblePermutations = (2L**len(Jobs)) * factorial(len(Jobs))
    #print "Possible permutations:", _PossiblePermutations

    print '=' * 70
    print "Starting placement using exhaustive search."
    print "There are %ld possible permutations..." % _PossiblePermutations,
    if _PossiblePermutations < 1e4:
        print "this'll take no time at all."
    elif _PossiblePermutations < 1e5:
        print "surf the web for a few minutes."
    elif _PossiblePermutations < 1e6:
        print "take a long lunch."
    elif _PossiblePermutations < 1e7:
        print "come back tomorrow."
    else:
        print "don't hold your breath."
    print "Press Ctrl-C to stop and use the best placement so far."
    print "Estimated maximum possible utilization is %.1f%%." % (
        tiling.maxUtilization(Jobs) * 100)

    try:
        _tile_search1(Jobs, tiling.Tiling(X, Y), 1)
        printTilingStats()
        print
    except KeyboardInterrupt:
        printTilingStats()
        print
        print "Interrupted."

    computeTime = time.time() - _StartTime
    print "Computed %ld placements in %d seconds / %.1f placements/second" % (
        _Placements, computeTime, _Placements / computeTime)
    print '=' * 70

    return _TBestTiling
Ejemplo n.º 4
0
def _tile_search2(Jobs, X, Y, cfg=config.Config):
    global _CkpointTime, _Placements, _TBestTiling, _TBestScore

    r = random.Random()
    N = len(Jobs)

    # M is the number of jobs that will be placed randomly.
    # N-M is the number of jobs that will be searched exhaustively.
    M = N - config.RandomSearchExhaustiveJobs
    M = max(M, 0)

    xspacing = cfg['xspacing']
    yspacing = cfg['yspacing']

    # Must escape with Ctrl-C
    while 1:
        T = tiling.Tiling(X, Y)
        joborder = r.sample(range(N), N)

        minInletSize = tiling.minDimension(Jobs)

        for ix in joborder[:M]:
            Xdim, Ydim, job, rjob = Jobs[ix]

            T.removeInlets(minInletSize)

            if r.choice([0, 1]):
                addpoints = T.validAddPoints(Xdim + xspacing, Ydim + yspacing)
                if not addpoints:
                    break

                pt = r.choice(addpoints)
                T.addJob(pt, Xdim + xspacing, Ydim + yspacing, job)
            else:
                addpoints = T.validAddPoints(Ydim + xspacing, Xdim + yspacing)
                if not addpoints:
                    break

                pt = r.choice(addpoints)
                T.addJob(pt, Ydim + xspacing, Xdim + yspacing, rjob)
        else:
            # Do exhaustive search on remaining jobs
            if N - M:
                remainingJobs = []
                for ix in joborder[M:]:
                    remainingJobs.append(Jobs[ix])

                tilesearch1.initialize(0)
                tilesearch1._tile_search1(remainingJobs, T, 1)
                T = tilesearch1.bestTiling()

            if T:
                score = T.area()

                if score < _TBestScore:
                    _TBestTiling, _TBestScore = T, score
                elif score == _TBestScore:
                    if T.corners() < _TBestTiling.corners():
                        _TBestTiling, _TBestScore = T, score

        _Placements += 1

        # If we've been at this for 3 seconds, print some status information
        if time.time() > _CkpointTime:
            printTilingStats()

            # Check for timeout - changed to file config
            if (config.Config['searchtimeout'] > 0) and (
                (time.time() - _StartTime) > config.Config['searchtimeout']):
                raise KeyboardInterrupt

        gerbmerge.updateGUI("Performing automatic layout...")
Ejemplo n.º 5
0
    def draw_column(self, x, y, height, img):
        height = int(height)
        for i in range(1, height + 1):
            img = self.draw_lozenge1(x + self.xsize, y - i * self.size, img)
            img = self.draw_lozenge2(x - self.xsize, y - i * self.size, img)
        return self.draw_lozenge3(x, y - height * self.size - self.size / 2,
                                  img)


def show_image(lattice):
    p = Painter(lattice)
    cv.imshow('lattice', p.img)
    cv.waitKey(0)


def save_image(lattice, filename):
    p = Painter(lattice)
    cv.imwrite(filename, p.img)


tiling = tiling.Tiling(20, 100000)
tiling.metropolis(100000, thermalization=10000)
print('done')
save_image(
    tiling.to_3d_lattice(
        np.round(tiling.average_configuration).astype(np.int32)),
    "lattice.jpg")
show_image(
    tiling.to_3d_lattice(
        np.round(tiling.average_configuration).astype(np.int32)))
Ejemplo n.º 6
0
import numpy as np
from multiprocessing import Pool

ITERATIONS = 100000


def write_results(temperatures, energies, capacities, filename):
    df = pd.DataFrame({
        'temperature': temperatures,
        'energy': energies,
        'capacity': capacities
    })
    df.to_csv(filename)


def call_metropolis(tiling):
    tiling.metropolis(ITERATIONS)
    return tiling


if __name__ == '__main__':
    p = Pool(3)
    n = 10
    temperatures = np.linspace(0.5, 10, 50)
    tilings = [tiling.Tiling(n, t) for t in temperatures]
    tilings = p.map(call_metropolis, tilings)
    energies = [tiling.average_energy for tiling in tilings]
    capacities = [tiling.capacity() for tiling in tilings]
    write_results(temperatures, energies, capacities,
                  'energy_' + str(n) + '.csv')