Pulp: Menambahkan batas ke LpVariable.dicts ()

Sep 02 2020

Katakanlah saya memiliki kamus ini:

cars = ["car1","car2","car3","car4","car5"]

x = LpVariable.dicts("car",cars, cat='Integer', lowBound=0, upBound=800)

Apakah ada cara untuk menambahkan lowBound dan upBounds yang berbeda ke setiap mobil?

Catatan

Versi kode mudah terlihat seperti ini:

car1 = LpVariable("car1", 0, 40)   
car2 = LpVariable("car2", 0, 1000) 

Harap perhatikan bahwa car1 upBound adalah 40 dan mobil 2 upBound adalah 1000.

Jawaban

1 test1500 Sep 02 2020 at 20:18

Akhirnya, saya telah melakukannya, dengan menggunakan kode hebatnya: Bagaimana saya menghasilkan variabel PuLP dan batasan tanpa menggunakan exec? Terimakasih banyak DSM sob!

prob = LpProblem("problem", LpMaximize)

# Setting LP variables
lpVars =["car1","car2","car3"]
upbounds=[40,80,30]
xs = [LpVariable("car{}".format(i+1), lowBound = 0,  upBound = upbounds[i], cat='Integer'  ) for i in range(len(lpVars))]

 # add objective
    margin = [3,2,3]
    total_prof = sum(x * value for x,value in zip(xs, margin))
    prob += total_prof

# add constraint
    labour = [2,1,4]
    total_labour = sum(x * w for x,w in zip(xs, labour))
    prob += total_labour <= 100

 # Solve the problem
    prob.solve()

Langkah selanjutnya adalah mendapatkan variabel array dari aplikasi front end (upbounds, margin, labor, dll ..), terima kasih bro, intip my github