2016-12-25 1 views
0

小さなコンテストでは、Pythonの問題を解決する必要があります。ただし、解決方法はのメモリを20MB以上使用しないでください。Pythonでのメモリ使用

私は良い解決策を思いつくために努力しました。私がそれを提出すると、それは最初の4つの例でうまく動作しますが、5番目の例は合格しません。理由:私のスクリプトは許可された以上のメモリを使いました。

私はそれをどうやって改善するか、そして一般的にどのように私のメモリ使用量をPythonで見るかはわかりません。だから、私にメモリの使用量を計算する方法、またはメモリを節約するために避けなければならないことのヒントを教えてもらえればそれはいいでしょう!

PS:私はSpyder 3.0.2で作業しています。私のスクリプトは、最小限の配列を使い、可能な限り縮小します。

編集:おかげで、あなたのあなたの答えのすべてのために、ここに私のコードでは、Windowsを使っているのであれば、あなたがアイデア

""" 
Small description of the problem 
You are a taxi 
You need to satisfy request 
A request is a person asking you to take him from pos1 to pos2 
There are gas station on the map, you start at the first one 
The goal is to satisfy every request while minimizing the amount of gas used BETWEEN two stations 
This is a quite non-intuitive problem, we dont care about the total amount of gas 
We care about the gas used between two stations 
In this problem, the gas used is the distance squared 

Example: 2 stations, one request 
Stations [0,0] , [2,2] 
Request [3,3] =>[2,1] 
Solution: You start at [0,0] 
[0,0] => [2,2] => [3,3] => [2,2] => [2,1] => [2,2] => [0,0] 
Station=>Station=> start =>Station=> end =>Station=>Station 
D: sqrt(8) sqrt(2) sqrt(2) 1  1  sqrt(8) 
gas: 8  (2*sqrt(2))**2  (2*1)**2   8 
""" 

def D(pos1, pos2): 
    # Returns the distance between two positions pos[x,y] 
    return ((pos1[0]-pos2[0])**2 + (pos1[1]-pos2[1])**2)**0.5 

def SPP(pos, stations): 
    # Returns the closest station to a certain point 
    indice = 0 
    mini = D(pos, stations[0]) 
    for i in range(1, len(stations)): 
     if D(pos, stations[i]) < mini: 
      mini = D(pos, stations[i]) 
      indice = i 
    return indice 

def SAcc(w, maxi, stations): 
    # Returns the list of accessible stations given a specific way 
    last = w[-1] 
    liste = [x for x in range(len(stations)) if x not in w] # Pas visitées 
    listem= [] 
    for i in range(len(liste)): 
     if D(stations[last] , stations[liste[i]]) < maxi: # Si proche 
      listem.append(liste[i]) 
    return listem 

def Dmaxw(w, stations): 
    # Returns the maximum distance between two stations of a given way 
    return max([ D(stations[w[i-1]], stations[w[i]]) for i in range(1,len(w)) ]) 

def OptiDeplS(a, b, stations): 
    # Optimize the movement from a station a to a station b, returns the minimal distance of the optimized way 
    optw = [a,b] 
    ways = [[a]] 
    maxi = D(stations[a], stations[b]) 
    while ways != []: 
     nways = [] 
     for w in ways: 
      for i in SAcc(w, maxi, stations): 
       nways.append(w + [i]) 
     ways = [] 
     for w in nways: 
      if w[-1] == b: 
       if Dmaxw(w, stations) < maxi: 
        maxi = Dmaxw(w, stations) 
        optw = w 
      elif Dmaxw(w, stations) < maxi: 
       ways.append(w) 
    return Dmaxw(optw, stations) 

def main(n, m, stations, request): 
    Dmax = 0 
    SPPr = [ [SPP(r[0],stations),SPP(r[1],stations)] for r in request ] 
    pos = 0 
    for i in range(m): 
     Dmax = max(Dmax, OptiDeplS(pos, SPPr[i][0], stations)) 
     Dmax = max(Dmax, 2*D(stations[SPPr[i][0]], requetes[i][0])) 
     Dmax = max(Dmax, OptiDeplS(SPPr[i][0], SPPr[i][1], stations)) 
     Dmax = max(Dmax, 2*D(stations[SPPr[i][1]], requetes[i][1])) 
     pos = SPPr[i][1] 
    Dmax = max(Dmax, OptiDeplS(SPPr[-1][0], pos, stations)) 
    return round(Dmax**2) 

n = 9 # nb of stations 
m = 5 # nb of request 
stations = [ [1,7],[7,6],[4,1],[2,2],[1,0],[0,3],[2,4],[9,1],[5,5] ] 
request = [ [[4,8],[9,7]],[[8,8],[1,6]],[[9,5],[1,4]],[[0,0],[3,5]],[[6,4],[10,0]] ] 
print(main(n, m, stations, request)) 
+0

これは非常に曖昧です。スクリプトを投稿すると、人々はどの部分がメモリ非効率であるかを知ることができます。 –

+0

'psutil'モジュールは、現在のプロセスを含むプロセスに関するすべての情報を提供できます。 –

+0

Pythonはコンテストで許可された唯一の言語ですか、あるいはCのようなものですか? –

答えて

0

を持っている場合、あなたはpythonのプロセス

def memory(): 
    import os 
    from wmi import WMI 
    w = WMI('.') 
    result = w.query("SELECT WorkingSet FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess=%d" % os.getpid()) 
    return int(result[0].WorkingSet) 
のメモリ使用量を見ることができます

スクリプトを提供すれば、誰かがメモリ使用量を改善できる場所を教えてくれるでしょう。

+0

彼は* nixを使用するとどうなりますか? – marmeladze

+0

私はスクリプトを追加:) – Philippe

関連する問題