예제 #1
0
파일: run.py 프로젝트: PaulGilmartin/alloa
 def __init__(self, *paths, **kwargs):
     self.data_objects = []
     self.randomised = kwargs.get('randomised', {})
     for i, path in enumerate(paths):
         randomise = self.randomised.get(i + 1, False)
         self.data_objects.append(FileData(path,
                                           level=i + 1,
                                           randomise=randomise))
     self.sequence = DataSequence(*self.data_objects)
     self.number_of_levels = len(self.sequence)
     self.graph = self.sequence.get_graph()
예제 #2
0
파일: run.py 프로젝트: PaulGilmartin/alloa
class Allocation(object):
    def __init__(self, *paths, **kwargs):
        self.data_objects = []
        self.randomised = kwargs.get('randomised', {})
        for i, path in enumerate(paths):
            randomise = self.randomised.get(i + 1, False)
            self.data_objects.append(FileData(path,
                                              level=i + 1,
                                              randomise=randomise))
        self.sequence = DataSequence(*self.data_objects)
        self.number_of_levels = len(self.sequence)
        self.graph = self.sequence.get_graph()

    def intro_string(self):
        print '''
################################################################
#                                                              #
# This is alloa by Mante Zelvyte, Uli Kraehmer and Paul Slevin #
#                                                              #
#    [email protected]                             #
#    [email protected]                                    #
#                                                              #
# Runtime with 100 agents at each hierarchy is a few ms        #
#                                                              #
################################################################\n
                '''
        for i in xrange(self.number_of_levels):
            print "{} agents of hierarchy {}".format(self.graph.hierarchies[
                                                         i].number_of_agents,
                                                     i + 1)

    def setup_allocation(self, *costs):
        assert len(costs) == len(self.data_objects) - 1
        self.graph.setup_graph(*costs)
        self.graph.set_flow()
        self.graph.simplify_flow()
        self.graph.allocate()

    def write_allocations(self):
        allocation = open(settings.ALLOCATION_PATH, "wb")
        writer = csv.writer(allocation, delimiter=",")
        for row in self.allocation:
            writer.writerow(row)
        allocation.close()

    def write_profile(self):
        profile = open(settings.ALLOCATION_PROFILE_PATH, "wb")
        writer = csv.writer(profile, delimiter="\n")
        writer.writerow(["Total number of assigned level 1 agents is " +
                         str(self.graph.max_flow),
                         "Total cost of assignment is " + str(
                             self.graph.flow_cost)])
        for i in xrange(self.number_of_levels - 1):
            writer.writerow(["", "Level {} Preference Count".format(i + 1)])
            for j in xrange(self.graph.hierarchies[i].max_preferences_length):
                split_point = self.number_of_levels
                column = [row[split_point + i] for row in self.allocation]
                count = str(column.count(j + 1))
                writer.writerow(["Number of level {} agents that were "
                                 "choice #".format(i + 2) + str(j + 1) + ": " +
                                 count])
        profile.close()

    @property
    def allocation(self):
        unnamed = self.graph.allocation
        split_point = self.number_of_levels
        rows = [[agent.name for agent in row[:split_point]] + row[split_point:]
                for row in unnamed]
        rows = sorted(rows,
                      key=lambda r: r[0][len(r[0]) - r[0][::-1].index(" "):])
        return rows