Go to login Go to sub menu Go to text

데이터 구조 및 분석: Non-Linear Structure, Optimization, and Algorithms

임시 이미지 KAIST 산업및시스템공학과 문일철 교수 KOOC (KAIST Open Online Course)
http://www.edwith.org/datastructure-2019s2/forum/94719
Thumb up 497 Learner 1498

안녕하세요! 강의 정말 잘 듣고있습니다.

정답 코드가 따로 동영상에 게시되지가 않아서 질문드립니다. 동영상과 결과값이 다른데, 코드 상 문제가 있는걸까요? 

def performEvolution(self, numIterations, numOffsprings, numPopulation, mutationFactor):
        if self.gui != '':
            self.gui.start()

        startTime = time.time()
        population = self.createInitialPopulation(numPopulation, len(self.dicLocations.keys()))
        while True:
            currentTime = time.time()
            if (currentTime - startTime) >= self.time:
                break
            offsprings = {}
            for itr in range(numOffsprings):
                # Put a correct method name and an argument
                # HInt: You need a parent to create an offspring
                p1, p2 = self.selectParents(population)

                # After selecting a parent pair, you need to create
                # an offspring. How to do that?
                # Hint: You need to exchange the genotypes from parents
                offsprings[itr] = self.crossoverParents(p1, p2)
                factor = int(mutationFactor * len(self.dicLocations.keys()))

                # You need to add a little bit of chagnes in the
                # genotype to see a potential random evolution
                # this does not need information from either population
                # or parent
                self.mutation(offsprings[itr], factor)

                # After creating an offspring set, what do you have to
                # perform?
                # HINT: You need to put the offspring in the population
            population = self.substitutePopulation(population, offsprings)

            # which method do you need to use the best solution? and
            # from where?
            mostFittest = self.findBestSolution(population)
            self.best = mostFittest
            print(self.calculateTotalDistance(self.best))
            if self.gui != '':
                self.gui.update()

        endTime = time.time()
        return self.best.getGenotype(), self.fitness(self.best), self.calculateTotalDistance(self.best), (endTime - startTime)
    
    def calculateTotalDistance(self, instance):
        # This genotype is created based upon a position based encoding
        # Fill in the following blanks to complete this method
        genotype = instance.getGenotype()
        currentCity = 0
        distance = 0.0
        for itr in range(len(genotype)-1):
            nextCity = genotype[currentCity]
            current = self.dicLocations[currentCity]
            next = self.dicLocations[nextCity]
            distance = distance + self.calculateDistance(current,next)
            currentCity = nextCity
        return distance
    
    def calculateDistance(self, coordinate1, coordinate2):
        # how to calculate the distance between two cities?
        # how to calculate the squre and the square root?
        distance = math.sqrt( math.pow(coordinate1[0]-coordinate2[0], 2) + math.pow(coordinate1[1]-coordinate2[1], 2) )
        return distance

이 외의 코드는 전부 최초 주어진 코드와 동일합니다.