2016-10-27 1 views
1

説明Pythonは - 私は私の大学のプロジェクト研究に問題がある異なるリスト(アルゴリズム)

に値を配布します。ここでは簡単な例です:

python - distribute values to different lists

  • 場所(ソース - 左側)がある項目の具体的な金額とは。
  • これらのソースは、一部の(すべてではない)宛先ロケーション(右側)に接続されています。
  • 宛先に最低限必要な金額があります。
  • 特に「緑色」のもの(Dest. X)が優先されています。ソースからのアイテムがあまりにも少ない場合、この宛先の最小金額を他の宛先よりも満たすことが重要です。
  • いくつかの宛先には、最大限の項目があります。

私はすでにそれを処理するためのPythonスクリプトを使用して開始しました問題/アプローチ

。ランダムな量のアイテムを含むsourcesdestinationsを作成します。しかし、私はmininum amountsmaximum_amountsを考慮してすべてのアイテムを配布する方法を知らないが、優先順位付けはもちろん、すべての送信元がすべての送信先に接続されているわけではない。

任意のアプローチは、好ましくは、数行のコードで、歓迎している:)ここでは、事前

答えて

0

おかげで私のアプローチです:

import random 
nums = [100]+[200]+[500] 
#used to check if the vars have been chekd b4 
xc = 0 
yc = 0 
zc = 0 

#define Sources: 
a = random.choice(nums) #I decided to define them at random 
b = random.choice(nums) 
c = random.choice(nums) 
d = random.choice(nums) 

#Define Destinations: 
x = 0 #min: 500 
y = 0 #min: 100 #max: 200 
z = 0 #min: 300 

#prioritize: 
pri = 0 
if a+b+c < 500: 
    pri = 1 

#fill x: 
if pri == 0: 
    if a >= 500 and xc == 0: 
     x += a 
     a -= a 
     xc = 1 
    if b >= 500 and xc == 0: 
     x += b 
     b -= b 
     xc = 1 
    if c >= 500 and xc == 0: 
     x += c 
     c -= c 
     xc = 1 
if pri == 1: 
    x += a+b+c 
    a -= a 
    b -= b 
    c -= c 
#we didnt test for d bcus its not cond 

#fill y: 
if b >= 100 and b <= 200 and yc == 0: 
    y += b 
    b -= b 
    yc = 1 
if c >= 100 and b <= 200 and yc == 0: 
    y += c 
    c -= c 
    yc = 1 
if d >= 100 and b <= 200 and yc == 0: 
    y += d 
    d -= d 
    yc = 1 

#fill z: 
if a >= 300 and zc == 0: 
    z += a 
    a -= a 
    zc = 1 
if c >= 300 and zc == 0: 
    z += c 
    c -= c 
    zc = 1 
if d >= 300 and zc == 0: 
    z += d 
    d -= d 
    zc = 1 

#test if it is filled: 
print("X: "+"{}".format(x)+"/500+") 
print("Y: "+"{}".format(y)+"/100_200") 
print("Z: "+"{}".format(z)+"/300+") 
#X: 
if x >= 500: 
    print("X filled correctly!") 
if x < 500: 
    print("X not filled!") 
#Y: 
if y >= 100 and y <= 200: 
    print("Y filled correctly!") 
if y < 100: 
    print("Y not filled!") 
if y > 200: 
    print("Y over-filled!") 
#Z: 
if z >= 300: 
    print("Z filled correctly!") 
if z < 300: 
    print("Z not filled!") 

input()#waits for you to end it manually 

時々、Xは、1にもかかわらず、満たされることはありません値が500であるのは、dがXに接続されていないためです(dは500になることもあります)

ごめんなさい!私は数学をあまり良くしていないし、OPが望んでいたことを(うまくいけば)理解するためにしばらくかかった。 私は少なくともいくつかの面で笑顔で願っています。 (多くの代数を使用しなければならなかった)

+0

あなたの努力のおかげでたくさん!この問題を解決する方法を理解するのに役立ちました。私はおそらくソースと目的地が異なるかもしれないことを忘れた。私はあなたのバージョンをソースとデスティネーションの任意の量(最大100)に適応させ、あなたにすべてを知らせるようにします。 :) – Kroenig

+0

そして、私にXDをするための何かを与えてくれてありがとう –

+0

これを作るのは楽しいものでした。 –

関連する問題