2017-06-29 8 views
1

私はパルプを使って、1-D、2-D、3-D変数が混在する、多くの変数を持つ特定の問題を解決しています。解が得られたら、これらの変数をk次元の次元数の配列にしたいと思います。パープとnumpyの配列

変数:(解決後)

u = pulp.LpVariable.dicts("u", (ns, ns), 0, 1, "Integer") 
v = pulp.LpVariable.dicts("v", (ns, ns), 0, 1, "Integer") 
w = pulp.LpVariable.dicts("w", (ns, ns, ps), 0, 1, "Integer") 
x = pulp.LpVariable.dicts("x", (ns, ps), 0, 1, "Integer") 
y = pulp.LpVariable.dicts("y", (ms, ps), 0, 1, "Integer") 
z = pulp.LpVariable.dicts("z", (ns), parameters.hold_duration_min, 
          parameters.hold_duration_max, "Continuous") 

varnames = {"u": u, "v": v, "w": w, "x": x, "y": y, "z": z} 

を入力:

problem.variables() 

出力:

[u_0_1, u_0_2, u_0_3, u_0_4, u_0_5, u_0_6, u_1_2, u_1_3, u_1_4, u_1_5, u_1_6, u_2_3, u_2_4, u_2_5, u_2_6, u_3_4, u_3_5, u_3_6, u_4_5, u_4_6, u_5_6, v_0_1, v_0_2, v_0_3, v_0_4, v_0_5, v_0_6, v_1_2, v_1_3, v_1_4, v_1_5, v_1_6, v_2_3, v_2_4, v_2_5, v_2_6, v_3_4, v_3_5, v_3_6, v_4_5, v_4_6, v_5_6, w_0_1_0, w_0_1_1, w_0_1_2, w_0_1_3, w_0_1_4, w_0_1_5, w_0_1_6, w_0_2_0, w_0_2_1, w_0_2_2, w_0_2_3, w_0_2_4, w_0_2_5, w_0_2_6, w_0_3_0, w_0_3_1, w_0_3_2, w_0_3_3, w_0_3_4, w_0_3_5, w_0_3_6, w_0_4_0, w_0_4_1, w_0_4_2, w_0_4_3, w_0_4_4, w_0_4_5, w_0_4_6, w_0_5_0, w_0_5_1, w_0_5_2, w_0_5_3, w_0_5_4, w_0_5_5, w_0_5_6, w_0_6_0, w_0_6_1, w_0_6_2, w_0_6_3, w_0_6_4, w_0_6_5, w_0_6_6, w_1_0_0, w_1_0_1, w_1_0_2, w_1_0_3, w_1_0_4, w_1_0_5, w_1_0_6, w_1_2_0, w_1_2_1, w_1_2_2, w_1_2_3, w_1_2_4, w_1_2_5, w_1_2_6, w_1_3_0, w_1_3_1, w_1_3_2, w_1_3_3, w_1_3_4, w_1_3_5, w_1_3_6, w_1_4_0, w_1_4_1, w_1_4_2, w_1_4_3, w_1_4_4, w_1_4_5, w_1_4_6, w_1_5_0, w_1_5_1, w_1_5_2, w_1_5_3, w_1_5_4, w_1_5_5, w_1_5_6, w_1_6_0, w_1_6_1, w_1_6_2, w_1_6_3, w_1_6_4, w_1_6_5, w_1_6_6, w_2_0_0, w_2_0_1, w_2_0_2, w_2_0_3, w_2_0_4, w_2_0_5, w_2_0_6, w_2_1_0, w_2_1_1, w_2_1_2, w_2_1_3, w_2_1_4, w_2_1_5, w_2_1_6, w_2_3_0, w_2_3_1, w_2_3_2, w_2_3_3, w_2_3_4, w_2_3_5, w_2_3_6, w_2_4_0, w_2_4_1, w_2_4_2, w_2_4_3, w_2_4_4, w_2_4_5, w_2_4_6, w_2_5_0, w_2_5_1, w_2_5_2, w_2_5_3, w_2_5_4, w_2_5_5, w_2_5_6, w_2_6_0, w_2_6_1, w_2_6_2, w_2_6_3, w_2_6_4, w_2_6_5, w_2_6_6, w_3_0_0, w_3_0_1, w_3_0_2, w_3_0_3, w_3_0_4, w_3_0_5, w_3_0_6, w_3_1_0, w_3_1_1, w_3_1_2, w_3_1_3, w_3_1_4, w_3_1_5, w_3_1_6, w_3_2_0, w_3_2_1, w_3_2_2, w_3_2_3, w_3_2_4, w_3_2_5, w_3_2_6, w_3_4_0, w_3_4_1, w_3_4_2, w_3_4_3, w_3_4_4, w_3_4_5, w_3_4_6, w_3_5_0, w_3_5_1, w_3_5_2, w_3_5_3, w_3_5_4, w_3_5_5, w_3_5_6, w_3_6_0, w_3_6_1, w_3_6_2, w_3_6_3, w_3_6_4, w_3_6_5, w_3_6_6, w_4_0_0, w_4_0_1, w_4_0_2, w_4_0_3, w_4_0_4, w_4_0_5, w_4_0_6, w_4_1_0, w_4_1_1, w_4_1_2, w_4_1_3, w_4_1_4, w_4_1_5, w_4_1_6, w_4_2_0, w_4_2_1, w_4_2_2, w_4_2_3, w_4_2_4, w_4_2_5, w_4_2_6, w_4_3_0, w_4_3_1, w_4_3_2, w_4_3_3, w_4_3_4, w_4_3_5, w_4_3_6, w_4_5_0, w_4_5_1, w_4_5_2, w_4_5_3, w_4_5_4, w_4_5_5, w_4_5_6, w_4_6_0, w_4_6_1, w_4_6_2, w_4_6_3, w_4_6_4, w_4_6_5, w_4_6_6, w_5_0_0, w_5_0_1, w_5_0_2, w_5_0_3, w_5_0_4, w_5_0_5, w_5_0_6, w_5_1_0, w_5_1_1, w_5_1_2, w_5_1_3, w_5_1_4, w_5_1_5, w_5_1_6, w_5_2_0, w_5_2_1, w_5_2_2, w_5_2_3, w_5_2_4, w_5_2_5, w_5_2_6, w_5_3_0, w_5_3_1, w_5_3_2, w_5_3_3, w_5_3_4, w_5_3_5, w_5_3_6, w_5_4_0, w_5_4_1, w_5_4_2, w_5_4_3, w_5_4_4, w_5_4_5, w_5_4_6, w_5_6_0, w_5_6_1, w_5_6_2, w_5_6_3, w_5_6_4, w_5_6_5, w_5_6_6, w_6_0_0, w_6_0_1, w_6_0_2, w_6_0_3, w_6_0_4, w_6_0_5, w_6_0_6, w_6_1_0, w_6_1_1, w_6_1_2, w_6_1_3, w_6_1_4, w_6_1_5, w_6_1_6, w_6_2_0, w_6_2_1, w_6_2_2, w_6_2_3, w_6_2_4, w_6_2_5, w_6_2_6, w_6_3_0, w_6_3_1, w_6_3_2, w_6_3_3, w_6_3_4, w_6_3_5, w_6_3_6, w_6_4_0, w_6_4_1, w_6_4_2, w_6_4_3, w_6_4_4, w_6_4_5, w_6_4_6, w_6_5_0, w_6_5_1, w_6_5_2, w_6_5_3, w_6_5_4, w_6_5_5, w_6_5_6, x_0_0, x_0_1, x_0_2, x_0_3, x_0_4, x_0_5, x_0_6, x_1_0, x_1_1, x_1_2, x_1_3, x_1_4, x_1_5, x_1_6, x_2_0, x_2_1, x_2_2, x_2_3, x_2_4, x_2_5, x_2_6, x_3_0, x_3_1, x_3_2, x_3_3, x_3_4, x_3_5, x_3_6, x_4_0, x_4_1, x_4_2, x_4_3, x_4_4, x_4_5, x_4_6, x_5_0, x_5_1, x_5_2, x_5_3, x_5_4, x_5_5, x_5_6, x_6_0, x_6_1, x_6_2, x_6_3, x_6_4, x_6_5, x_6_6, y_0_0, y_0_1, y_0_2, y_0_3, y_0_4, y_0_5, y_0_6, y_1_0, y_1_1, y_1_2, y_1_3, y_1_4, y_1_5, y_1_6, y_2_0, y_2_1, y_2_2, y_2_3, y_2_4, y_2_5, y_2_6, y_3_0, y_3_1, y_3_2, y_3_3, y_3_4, y_3_5, y_3_6, y_4_0, y_4_1, y_4_2, y_4_3, y_4_4, y_4_5, y_4_6, y_5_0, y_5_1, y_5_2, y_5_3, y_5_4, y_5_5, y_5_6, y_6_0, y_6_1, y_6_2, y_6_3, y_6_4, y_6_5, y_6_6, y_7_0, y_7_1, y_7_2, y_7_3, y_7_4, y_7_5, y_7_6, y_8_0, y_8_1, y_8_2, y_8_3, y_8_4, y_8_5, y_8_6, z_0, z_1, z_2, z_3, z_4, z_5, z_6] 

私が発生しているよ何を保持するためにnumpyの配列の辞書です結果、例えばたとえば、

results["x"][1][4] == pulp.value(x[1][4]) 
type(results["x"]) == numpy.ndarray 
results["z"][2] == pulp.value(z[2]) 
results(["w"][6][0][2]) == pulp.value(w[6][0][2]) 

などを使用して試したことがあります。パンダが、それは1-D、2Dおよび3-Dアレイの私のミックスのために動作させることはできません。

results = {} 
vfunc = numpy.vectorize(lambda i: pulp.value(i)) 
for k, v in varnames.items(): 
    try: 
     results[k] = vfunc(pandas.DataFrame(v).values) 
    except: 
     pass 

... 2D変数について上記の作品ではなく、1 - Dまたは3Dのものである。

答えて

0

多くの歯が切れた後、変数のさまざまな次元を処理するために使用できる再帰的ジェネレータを開発しました。ここでは2つのバージョンが提供されています。最初のバージョンは簡単で、コード内の別の場所で使用できます。一連の変数オブジェクトが順番に生成されます。第2のジェネレータは、その座標と共に変数オブジェクトを順番に生成する。後者を使用して配列を生成することができます。

# Generator for accessing components of a variable 
def variable_iterator(variable, dimensions): 
    if len(dimensions) > 1: 
     for i in range(dimensions[0]): 
      yield from variable_iterator(variable[i], dimensions[1:]) 
    else: 
     for i in range(dimensions[0]): 
      yield variable[i] 

# Generator for accessing components of a variable, retunring the component and 
# its position 
def variable_iterator_loc(variable, dimensions): 
    index = [0]* len(dimensions) 
    depth = 0 
    yield from _variable_iterator_loc(variable, dimensions, index, depth) 


# Recursive function called by variable_iter_loc 
def _variable_iterator_loc(variable, dimensions, index, depth): 
    depth += 1 
    if len(dimensions) == 1: 
     for i in range(dimensions[0]): 
      index[depth - 1] = i 
      yield variable[i], tuple(index) 
    else: 
     for i in range(dimensions[0]): 
      index[depth - 1] = i 
      yield from _variable_iterator_loc(variable[i], dimensions[1:], 
               index, depth) 


def unflatten_variable(variable, dimensions): 
    unflattened = numpy.empty(dimensions) 
    variter = variable_iterator_loc(variable, dimensions) 
    for i in range(prod(dimensions)): 
     varobject, position = next(variter) 
     unflattened[position] = pulp.value(varobject) 
    return unflattened 
0

最初に結果がPulpで辞書に保存されますが、配列にはソートされません。だから、あなたはあなたのndarrayの適切な場所でそれらを抽出する必要があります。さらに私の場合、変数の中には気にしない緩やかな変数があるので、最初に意味を完全に抽出する必要があります。

#storing Optimisation result (alpha & beta) in a new dictionary 
varDic = {} 
for v in prob.variables(): 
    if v.name[:5] == 'alpha' or v.name == 'b': 
     varDic[v.name] = v.varValue 

#passing solution (alpha, beta) to proper ndarray 
b = varDic['b'] 
del varDic['b'] #remove beta from dictionary so only alpha is left 

x = np.zeros(N) 
#storing the value of alpha_i in x(i) 
for key in varDic: 
    x[int(key[6:])] = varDic[key]