2016-09-30 6 views
-1

私は、線形計画法を使って交通問題の公式化に取り組んでいます。主にウェブで検索し、code which is written in Javaが見つかりました。しかし、私はPythonですべてのものを書く必要があります。そして私はそれをPythonに変換しています。私は自分自身がPythonでJavaをうまく使っているとは思っていません。私は少し変換しようとしました。すべては問題ありませんが、以下のスニペットの変換方法はわかりません。JavaのLinkedListsとStream関数を扱っています。Java、Python - Java FlatMapをPython LinkedListに変換する方法

static LinkedList<Shipment> matrixToList() { 
    return stream(matrix) 
      .flatMap(row -> stream(row)) 
      .filter(s -> s != null) 
      .collect(toCollection(LinkedList::new)); 
} 

あなたは、私がリンクされたJavaコードの上に変換する方法を検討してに興味がある場合は、ここであなたは以下のShipmentクラスは私の(不完全)Pythonコードで見ることができます:

import sys 

class TransportationProblem: 

    demand = list() 
    supply = list() 
    costs = list(list()) 
    matrix = list(list()) 

    def __init__(self): 
     pass 

    class Shipment: 
     costPerUnit = 0.0 
     quantity = 0.0 
     r = 0 
     c = 0 

     def __init__(self, quantity, costPerUnit, r, c): 
      self.quantity = quantity 
      self.costPerUnit = costPerUnit 
      self.r = r 
      self.c = c 

    def init(self, f_name= ""): 
     try: 
      with open(f_name) as f: 
       val = [int(x) for x in f.readline().strip().split(' ')] 
       numSources, numDestinations = val[0], val[1] 
       src = list() 
       dst = list() 

       val = [int(x) for x in f.readline().strip().split(' ')] 
       for i in range(0,numSources): 
        src.append(val[i]) 

       val = [int(x) for x in f.readline().strip().split(' ')] 
       for i in range(0, numDestinations): 
        dst.append(val[i]) 

       totalSrc = sum(src) 
       totalDst = sum(dst) 

       if totalSrc > totalDst: 
        dst.append(totalSrc - totalDst) 
       elif totalDst > totalSrc: 
        src.append(totalDst - totalSrc) 

       self.supply = src 
       self.demand = dst 

       self.costs = [[0 for j in range(len(dst))] for i in range(len(src))] 
       self.matrix = [[self.Shipment() for j in range(len(dst))] for i in range(len(src))] 

       for i in range(0,len(src)): 
        val = [int(x) for x in f.readline().strip().split(' ')] 
        for j in range(0, len(dst)): 
         self.costs[i][j] = val[j] 

       print self.costs 
     except IOError: 
      print "Error: can\'t find file or read data" 

    def northWestCornerRule(self): 
     northwest = 0 
     for r in range(0, len(self.supply)): 
      for c in range(northwest, len(self.demand)): 
       quantity = min(self.supply[r], self.demand[c]) 
       if quantity > 0: 
        self.matrix[r][c] = self.Shipment(quantity=quantity, costPerUnit=self.costs[r][c], r=r, c=c) 
        self.supply[r] = self.supply[r] - quantity 
        self.demand[c] = self.demand[c] - quantity 
        if self.supply[r] == 0: 
         northwest = c 
         break 

    def steppingStone(self): 
     maxReduction = 0 
     move = [] 
     leaving = self.Shipment() 

     self.fixDegenerateCase() 
     for r in range(0,len(self.supply)): 
      for c in range(0,len(self.demand)): 
       if self.matrix[r][c] != None: 
        pass 

       trail = self.Shipment(quantity=0, costPerUnit=self.costs[r][c], r=r, c=c) 
       path = self.geClosedPath(trail) 

       reduction = 0 
       lowestQuantity = sys.maxint 
       leavingCandidate = None 

       plus = True 
       for s in path: 
        if plus == True: 
         reduction = reduction + s.costPerUnit 
        else: 
         reduction = reduction - s.costPerUnit 
         if s.quantity < lowestQuantity: 
          leavingCandidate = s 
          lowestQuantity = s.quantity 
        plus = not plus 
       if reduction < maxReduction: 
        move = path 
        leaving = leavingCandidate 
        maxReduction = reduction 

     if move != None: 
      q = leaving.quantity 
      plus = True 
      for s in move: 
       s.quantity = s.quantity + q if plus else s.quantity - q 
       self.matrix[s.r][s.c] = None if s.quantity == 0 else s 
       plus = not plus 
      self.steppingStone() 

    def fixDegenerateCase(self): 
     pass 

    def getClosedPath(self): 
     pass 

    def matrixToList(self): 
     pass 

答えて

1

我々はできますこれをステップに分けてください。まずmatrix変数から始まります。これは、タイプShipmentの反復可能変数を含むいくつかの反復可能変数です。

オブジェクトをストリーミングするとは、ストリームの各要素に対してアクションを実行することを意味します。

ストリーム上のmapは、各オブジェクト(たとえば、タイプがA)を取り、タイプBに変換することを意味します。 は、mapStream<B>を生成する場合に使用される特殊なケースです。 flatMapを使用すると、これらのストリームを1つのストリームに連結できます。

3つのオブジェクト{A1, A2} -> {{B11, B12, B13}, {B21, B22, B23}}

flatMapのストリームにそれぞれAマップmatrixrowオブジェクトのストリームを生成する。この場合、この一つのストリーム{A1, A2} -> {B11, B12, B13, B21, B22, B23}

を行います言います。各rowShipmentのストリームにマップされ、flatMapはそれらを連結するために使用されます。最後filter

空出荷を除去するために使用される(すなわち、値がヌルである)とcollect方法はListShipmentのストリームを変換するために呼び出されます。

ストリームせずにこれを再作成することは怒鳴るようになります。

static LinkedList<Shipment> matrixToList() { 
    LinkedList<Shipment> result = new LinkedList<>(); 
    for (List<Shipment> row : matrix) { 
     for (Shipment shipment : row) { 
      if (shipment != null) { 
       result.add(shipment); 
      } 
     } 
    } 
    return result; 
} 
関連する問題