Exemplo n.º 1
0
    def LoadFromFile(self, filename):
        ''' Load map from file.
            Example of proper constructed file:
            "   line_1:  100 200 270            // car: position (2 first), rotation (lat one)
                line_2:  300 400 90 2.5 0.5     // 1st barrier: position (2 first), rotation (middle), scale (2 last)
                line_3:  600 700 180 2 1        // 2nd barrier: position (2 first), rotation (middle), scale (2 last)
                line_4:  100 100 45 1.2 1.2     // 3rd barrier: position (2 first), rotation (middle), scale (2 last)
                .
                .
                .
                line_n+1:  350 100 30 1.5 1.5   // n'st barrier: position (2 first), rotation (middle), scale (2 last)
        '''

        lines = FilesManager.LinesFromFile(filename)

        # Divide the data into parts for the car and barriers respectively
        firstLine, rest = lines[0], lines[1:]

        # Convert to list of floats
        firstLine = BuiltInTypesConverter.StringToFloats(firstLine)

        # Unpack
        rawCarSuggestedPos, rawCarSuggestedRot = firstLine[0:2], firstLine[4]

        # Convert to our coordinate system.
        carSuggestedPos, carSuggestedRot = Map.ConvertPos(
            rawCarSuggestedPos), Map.ConvertRot(rawCarSuggestedRot)

        # Create  the right objects.
        self.carSuggestedPos, self.carSuggestedRot = Point(
            carSuggestedPos), carSuggestedRot

        # Steps in loop below are similar to steps above.
        for line in rest:
            line = BuiltInTypesConverter.StringToFloats(line)
            rawPos, rawRot, rawScale = line[0:2], line[4], line[2:4]
            pos, rot, scale = Map.ConvertPos(rawPos), Map.ConvertRot(
                rawRot), Map.ConvertScale(rawScale)

            barrier = Barrier()
            barrier.pos, barrier.rot, barrier.scale = Point(pos), rot, Vector(
                scale)

            # Calculate parameters necessary for algorithms.
            barrier.Create()

            self.listOfBarriers.append(barrier)
Exemplo n.º 2
0
    def LoadPopulation(cls, filename):
        ''' Load final population from previous experiment and set it as starting population.
            Be aware that every line in file contains saved wages of one individual.
        '''

        lines = FilesManager.LinesFromFile(filename)
        cls.startingPopulation = cls.toolbox.population(len(lines))
        for individual, line in zip(cls.startingPopulation, lines):
            importedWages = BuiltInTypesConverter.StringToFloats(line)
            for _ in range(len(individual)):
                # Remember that "Pattern" derives from "list", so we need to use old-fashioned style.
                individual.wages[_] = importedWages[_]

        # Starting population won't be randomly generated.
        cls.startFromRandomPopulation = False