2

.sort()またはsorted()を使用してオブジェクトのリストを並べ替えるこのコードを取得できません。ここで何が欠けていますか?

PS私のsolution.distance()メソッドは、誰かが何か提案があれば、美容整形も使用できます。

ありがとう!

import random
import math

POPULATION_SIZE = 100

data = [[1, 565.0, 575.0],
        [2, 25.0, 185.0],
        [3, 345.0, 750.0],
        [4, 945.0, 685.0],
        [5, 845.0, 655.0],
        [6, 880.0, 660.0],
        [7, 25.0, 230.0],
        [8, 525.0, 1000.0],
        [9, 580.0, 1175.0],
        [10, 650.0, 1130.0]
        ]

class Solution():

  def __init__(self):
    self.dna = []
    self.randomize()

  def randomize(self):
    temp = data[:]
    while len(temp) > 0:
      self.dna.append( temp.pop( random.randint( 0,len(temp)-1 ) ) ) 

  def distance(self): 
    total = 0 
    #There has to be a better way to access two adjacent elements.
    for i, points in enumerate(self.dna):
      if i < (len(self.dna)-1): 
        total += math.sqrt( (points[1]-self.dna[i+1][1])**2 + (points[2]-self.dna[i+1][2])**2 )
      else:
        total += math.sqrt( (points[1]-self.dna[0][1])**2 + (points[2]-self.dna[0][2])**2 )
    return int(total)


class Population():

  def __init__(self):
    self.solutions = []
    self.generation = 0

    #Populate with solutions
    self.solutions = [Solution() for i in range(POPULATION_SIZE)]


  def __str__(self):

    result = ''

    #This is the part that is not returning sorted results.  I tried sorted() too.
    self.solutions.sort(key=lambda solution: solution.distance, reverse=True)


    for solution in self.solutions:
      result += 'ID: %s - Distance: %s\n' % ( id(solution),  solution.distance() )

    return result


if __name__ == '__main__':

  p = Population()
  print p
4

3 に答える 3

3

変化する

key=lambda solution: solution.distance

key=lambda solution: solution.distance()

(関数を呼び出すには括弧が必要です。)

distanceまたは、メソッドをプロパティにすることもできます。

  @property
  def distance(self): 
      ....

この場合、のすべてのオカレンスをに変更しsolution.distance()ますsolution.distance。この代替ソリューションは、距離について話したいときに毎回2文字のクラッター(パレン)を削除するので、もう少し良いと思います。

PS。のそれぞれkey=lambda solution: solution.distanceのバインドされたメソッドを返していました。それぞれのキーとして同じオブジェクトが返されていたため、目的の順序は発生し​​ませんでした。solution.distancesolutionself.solutionssolution

于 2010-07-07T15:19:38.110 に答える
1

関数型プログラミング手法を使用して、クラスをクリーンアップする試みは次のとおりです。

import random

class Solution():

  def __init__(self):
    self.dna = []
    self.randomize()

  def randomize(self):
    self.dna = data
    random.shuffle(self.dna)

  def distance(self):
    # Return the distance between two points.
    def point_distance((p1, p2)):
      return math.sqrt((p1[1]-p2[1])**2) + (p1[2]-p2[2])**2)
    # sums the distances between consecutive points.
    # zip pairs consecutive points together, wrapping around at end.
    return int(sum(map(point_distance, zip(self.dna, self.dna[1:]+self.dna[0:1])))

これはテストされていませんが、動作に近いはずです。また、データに3要素リストの代わりにクラスを使用することをお勧めします。これにより、コードがより明確になります。

   def point_distance((p1, p2)):
      return math.sqrt((p1.x-p2.x)**2) + (p1.y-p2.y)**2)
于 2010-07-07T15:28:50.210 に答える
1

ループを記述するためのより良い方法は次のとおりです。itertoolsdistance()のドキュメントから取得した次の関数定義をコードに入れます。

from itertools import izip, tee
def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

distance次に、次のように、Pythonの効率的なイテレータ操作ルーチンを利用するように記述できます。

from itertools import chain, islice
def distance(self): 
    all_pairs = islice(pairwise(chain(self.dna, self.dna)), 0, len(self.dna))
    return sum(math.sqrt((p[1]-q[1])**2 + (p[2]-q[2])**2) for p,q in all_pairs)

dnaアレイが非常に長い場合でも、これはかなり効率的です。

于 2010-07-07T15:29:18.027 に答える