2013-10-09 38 views
1

我正在學習優化,我是OpenOpt的新手。如何使用OpenOpt解決3D多倉裝箱問題

我想表示,其中每一個具有3個資源使用指標(CPU,存儲器和網絡)過程,以及我想根據下列限制數處理分配給組/箱:

sum(cpu) within a group < 100 
sum(mem) within a group < 100 
sum(net) within a group < 100 
Minimize(number of groups) or maximize the sum of each resource within a group. 

理想情況下,我想有這種類型的輸出:

VM 1 assigned to group 1 
VM 2 assigned to group 1 
VM 3 assigned to group 1 
VM 4 assigned to group 2 
VM 5 assigned to group 2 
VM 6 assigned to group 3 
... and so on 

問:我該怎麼做?如果OpenOpt無法做到這一點,有沒有其他的lib可以幫助我呢?

這裏我最初的代碼: https://github.com/vonpupp/mdbp/blob/master/ksp_2.py

非常感謝!

回答

0

這個想法是爲每個項目分配一個權重(在這種情況下,全部都是相同的),並且最大限度地限制約束條目的數量。

這是怎樣的項目創建:

def gen_vms(): 
    tg = tracegen.TraceGenerator() 
    trace = tg.gen_trace() 
    items = [{ 
        'name': 'item %d' % i, 
        'weight': 1, 
        'cpu': t[0], 
        'mem': t[1], 
        'disk': t[2], 
        'net': t[3], 
        'n': 1 
      } for i,t in islice(enumerate(trace), 200)] 
    return items 

這是怎麼約束創建:

def add_constraint(values, constraint): 
    return values[constraint] < 99 

def add_constraints(values, constraint_list): 
    return [add_constraint(values, constraint) for constraint in constraint_list] 

def gen_costraints(constraint_list): 
    global constraints 
    constraints = lambda values: (add_constraints(values, constraint_list)) 

求解過程是這樣的:

def solve_host(): 
    global items 
    global constraints 
    p = KSP('weight', list(items), constraints = constraints) 
    result = p.solve('glpk', iprint = -1) 

進一步關於如何做到這一點的細節可以在這裏找到:https://github.com/vonpupp/2013-sbrc-experiments/blob/e2e8a2be320c8f77d67a5bc6bb822510564e80f3/myksp_oo.py

+0

其中是您的要點lib.tracegen.tracegen? – JohnO

+1

您可以直接在github上瀏覽該特定提交的代碼(這不是要點,而是完整的回購)。如果你正在尋找tracegen.py,你可以在這裏找到它:https://github.com/vonpupp/2013-sbrc-experiments/blob/e2e8a2be320c8f77d67a5bc6bb822510564e80f3/lib/tracegen/tracegen.py –