コード例 #1
0
ファイル: ga.py プロジェクト: rbonvall/grasp-crew-scheduling
        beta = [set() for col in range(n)]
        for row, col in transpose(A.nonzero()):
            alpha[row].add(col)
            beta[col].add(row)

        self.A = A
        self.costs = array(costs)
        self.nr_tasks, self.nr_rotations = m, n
        self.alpha = map(frozenset, alpha)
        self.beta = map(frozenset, beta)

    def __repr__(self):
        return "<CSP problem, %dx%d>" % (self.nr_tasks, self.nr_rotations)


Solution = namedtuple("Solution", "columns covering fitness unfitness")


def make_solution(problem, columns):
    covering = dot(problem.A, columns)
    fitness = dot(problem.costs, columns)
    unfitness = sum(abs(covering - 1))
    return Solution(columns, covering, fitness, unfitness)


def initial_solution(problem):
    columns = zeros(problem.nr_rotations, dtype="uint8")
    I = frozenset(range(problem.nr_tasks))
    S, U = set(), set(I)
    while U:
        i = choice(list(U))
コード例 #2
0
#!/usr/bin/env python

from csp import CrewSchedulingProblem, namedtuple
from random import choice, random
from functools import partial
from operator import attrgetter
from sys import stdout
range = xrange

Candidate = namedtuple('Candidate', 'rotation, greedy_cost')

def DEBUG_RCL(candidates, rcl, selected_candidate, stream=stdout):
    min_cost = candidates[0].greedy_cost
    max_cost = candidates[-1].greedy_cost
    def c_repr(c):
        r = c.rotation
        s = '%s:%d:%d' % (str(r.tasks), r.cost, c.greedy_cost)
        bold = 1 if c == selected_candidate else 0
        color = 31 if c.greedy_cost == max_cost else None
        if c in rcl:
            color = 32 if c.greedy_cost == min_cost else 36
        if color:
            return '\033[%d;49;%dm%s\033[0m' % (bold, color, s)
        return s
    data = (len(candidates), len(rcl), min_cost, max_cost)
    stream.write('#candidate rotations: %d, #rcl: %d, cost range: [%d, %d]\n' % data)
    stream.write(' '.join(c_repr(c) for c in candidates))
    stream.write('\n')

def DEBUG_SOLUTION(rotations, stream=stdout):
    cost = sum(r.cost for r in rotations)