def LoadFromLines(self, lines): ''' Helper function, used only in "Album.LoadFromFile" :param line: list list of strings. List is divided into parts referring to corresponding tracks. ''' first_line, rest = lines[0], lines[1:] linesPerSingleCarRecord, blackboxesPerTrack = BuiltInTypesConverter.StringToInts( first_line) # Read one track record per single iteration while len(rest) >= 1: firstLineFromNextTrack = rest[0] numberOfRocsInNextTrack = BuiltInTypesConverter.StringToInts( firstLineFromNextTrack)[1] # Comment about line below: # " + blackboxesPerTrack" results from fact, that black box need one additional line with information which # are necessary for properly loading. We have "blackboxesPerTrack" black boxes in next track, so that # addition. # " + 1" results from fact, that track also requires one additional line. numberOfLinesForNextTrack = linesPerSingleCarRecord * numberOfRocsInNextTrack + blackboxesPerTrack + 1 track = Track() track.LoadFromLines(rest[:numberOfLinesForNextTrack]) self.AddTrack(track) # Remove already read data from rest of data to read. rest = rest[numberOfLinesForNextTrack:]
def LoadFromLines(self, lines): ''' Helper function, used only in "Album.LoadFromLines" :param line: list list of strings. List is divided into parts referring to corresponding black boxes. ''' first_line, rest = lines[0], lines[1:] linesPerSingleCarRecord = BuiltInTypesConverter.StringToInts( first_line)[0] # Read one black box record per single iteration while len(rest) >= 1: numberOfCarRecordsInNextBlackBox = BuiltInTypesConverter.StringToInts( rest[0])[0] # Comment: " + 1" on the end of next line of code results from fact, that black box need one additional line # with information which are necessary to properly loading. numberOfLinesForNextBlackBox = numberOfCarRecordsInNextBlackBox * linesPerSingleCarRecord + 1 blackbox = BlackBox() blackbox.LoadFromLines(rest[:numberOfLinesForNextBlackBox]) self.AddBlackBox(blackbox) # Remove already read data from rest of data to read. rest = rest[numberOfLinesForNextBlackBox:]
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)
def LoadFromLine(self, line): ''' Helper function, used only in RadarRecord.LoadFromLines() :param line: str string which contains data about rangefinder in specific order ''' self.pos.x, self.pos.y, self.rot, self.posOfBarrier.x, self.posOfBarrier.y = BuiltInTypesConverter.StringToInts( line)
def SaveToFile(self, filename): ''' Save object's parameters in specific order. ''' listOfData = [ self.pos[0], self.pos[1], self.rot, self.posOfBarrier[0], self.posOfBarrier[1] ] FilesManager.AddLineToFile( BuiltInTypesConverter.IntsToString(listOfData), filename)
def LoadFromLines(self, lines): first_line, rest = lines[0], lines[1:] # Load data about car exclusively self.pos.x, self.pos.y, self.rot = BuiltInTypesConverter.StringToInts(first_line) # Load data about radar self.radarRecord.LoadFromLines(rest)
def LoadFromLines(self, lines): first_line, rest = lines[0], lines[1:] linesPerSingleCarRecord = BuiltInTypesConverter.StringToInts(first_line)[0] # Read one black box record per single iteration while len(rest) >= 1: numberOfCarRecordsInNextBlackBox = BuiltInTypesConverter.StringToInts(rest[0])[0] # Comment: " + 1" on the end of next line of code results from fact, that black box need one additional line # with information which are necessary to properly loading. numberOfLinesForNextBlackBox = numberOfCarRecordsInNextBlackBox * linesPerSingleCarRecord + 1 blackbox = BlackBox() blackbox.LoadFromLines(rest[:numberOfLinesForNextBlackBox]) self.AddBlackBox(blackbox) # Remove already read data from rest of data to read. rest = rest[numberOfLinesForNextBlackBox:]
def SavePopulation(cls, filename): ''' Save final population for future purposes Be aware that every line in file contains saved wages of one individual. ''' FilesManager.ClearFile(filename) for individual in cls.finalPopulation: nextLine = BuiltInTypesConverter.FloatsToString(individual.wages) FilesManager.AddLineToFile(nextLine, filename)
def SaveToFile(self, filename): ''' Save object's parameters in specific order ''' # Save data about car exclusively with open(filename, "a") as file: listOfData = [self.pos[0], self.pos[1], self.rot] file.write(BuiltInTypesConverter.IntsToString(listOfData) + "\n") # Save data about radar self.radarRecord.SaveToFile(filename)
def LoadFromLines(self, lines): first_line, rest = lines[0], lines[1:] # Load data about radar exclusively self.pos.x, self.pos.y, self.rot = BuiltInTypesConverter.StringToInts(first_line) # Load data about rangefinders for _, line in zip(range(self.__class__.numberOfRangefinderRecords), rest): rangefinderRecord = RangefinderRecord() rangefinderRecord.LoadFromLine(line) self.listOfRangefinderRecords[_] = rangefinderRecord
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
def LoadFromLines(self, lines): ''' Helper function, used only in "BlackBox.LoadFromLines()" :param line: list list of strings which contains data about car in specific order ''' first_line, rest = lines[0], lines[1:] # Load data about car exclusively self.pos.x, self.pos.y, self.rot = BuiltInTypesConverter.StringToInts( first_line) # Load data about radar self.radarRecord.LoadFromLines(rest)
def LoadFromLines(self, lines): first_line, rest = lines[0], lines[1:] numberOfCarRecords = BuiltInTypesConverter.StringToInts(first_line)[0] linesPerSingleCarRecord = int(len(rest)/numberOfCarRecords) #Read one car record per single iteration while len(rest) >= 1: carRecord = CarRecord() carRecord.LoadFromLines(rest[:linesPerSingleCarRecord]) self.AddCarRecord(carRecord) #Remove already read data from rest of data to read. rest = rest[linesPerSingleCarRecord:]
def LoadFromLines(self, lines): ''' Helper function, used only in CarRecord.LoadFromLines() :param lines: list list of strings which contains data about radar in specific order ''' first_line, rest = lines[0], lines[1:] # Load data about radar exclusively self.pos.x, self.pos.y, self.rot = BuiltInTypesConverter.StringToInts( first_line) # Load data about rangefinders for _, line in zip(range(self.__class__.numberOfRangefinderRecords), rest): rangefinderRecord = RangefinderRecord() rangefinderRecord.LoadFromLine(line) self.listOfRangefinderRecords[_] = rangefinderRecord
def LoadFromLines(self, lines): ''' Helper function, used only in "Track.LoadFromLines()" :param line: list list of strings. List is divided into parts referring to corresponding car records. ''' first_line, rest = lines[0], lines[1:] numberOfCarRecords = BuiltInTypesConverter.StringToInts(first_line)[0] linesPerSingleCarRecord = int(len(rest) / numberOfCarRecords) #Read one car record per single iteration while len(rest) >= 1: carRecord = CarRecord() carRecord.LoadFromLines(rest[:linesPerSingleCarRecord]) self.AddCarRecord(carRecord) #Remove already read data from rest of data to read. rest = rest[linesPerSingleCarRecord:]
def test_1(self): list0 = [1.002, 23.00000000003, -0.009, 0, -4.0002, -3.999] self.assertEqual(BuiltInTypesConverter.FloatsToString(list0), "1.002 23.00000000003 -0.009 0 -4.0002 -3.999 ")
def test_1(self): list0 = [100, 23.03, 435, 0, -4, -3.999] self.assertEqual(BuiltInTypesConverter.IntsToString(list0), "100 23 435 0 -4 -3 ")
def test_1(self): line = "122 123 124" self.assertEqual(BuiltInTypesConverter.StringToInts(line), [122, 123, 124])
def LoadFromLine(self, line): self.pos.x, self.pos.y, self.rot, self.posOfBarrier.x, self.posOfBarrier.y = BuiltInTypesConverter.StringToInts(line)