将决策变量与字典值相乘

时间:2018-02-24 16:38:59

标签: python multiplication gurobi

我在Python中编写了一个优化问题,我将用Gurobi解决。但是我确实有一个表达式的问题,我不知道为什么它不起作用。

我的决策变量// My guess at employee structure typedef struct { char name[100]; char telno[25]; int id; double salary; } Employee; // NOTE: MSVC "forces" me to use safe version of some funtions: "_s" suffix int readin(Employee *p) { Employee e = *p; int count = 0; // <-- Never used char line[80]; // Why are you looping, you only ever modify 1 employee? while (e.name[0] != '#') // <- Fixed comparison { printf("Enter name:\n"); //scanf("\n"); <- Not needed gets_s(e.name); //*Test for '#' here and exit loop before entering other fields? printf("Enter tel:\n"); //scanf("\n"); <- Not needed //scanf_s("%s", &e.telno, 25); gets_s(e.telno, 25); printf("Enter id:\n"); //scanf("\n"); <- Not needed //scanf_s("%d", &e.id); //*scanf leaves trailing white (newline), I usually prefer gets/sscanf combination gets_s(line, sizeof(line)); sscanf_s(line, "%d", &e.id); printf("Enter salary:\n"); //scanf("\n"); <- Not needed //scanf_s("%lf", &e.salary); //*scanf leaves trailing white (newline), I usually prefer gets/sscanf combination gets_s(line, sizeof(line)); // sscanf_s(line, "%lf", &e.salary); } *p = e; return 0; // <- Added, function declared to return an int, what do you want to return? } 是二进制的。我有一个参数x[i,j],它是一个字典,包含字符串名称a[j]的键,每个j的值是一个浮点数。 我的表情看起来像这样:

j

我希望模型采用x[i,j]*a[j] 的{​​{1}}并将其与j中的相应x[i,j]相乘。但是,该模型将始终采用j的最后一个元素。为什么这样做以及我需要更改什么才能使模型采用与a[j]中相同的a[j]?我也尝试过for循环但不会改变任何东西。

更详细地解释一下:

我的变量看起来像这样。我从excel文件中读到了一个:

j

....约束是这样制定的:

x[i,j]

但问题是表达式x [i,j] * a [j] ..它不起作用。正如我所说,我也试过for-loops for i和j,但没有成功。

J = [ “J1”, “J2”,...]

a = {“j1”:1.0; “J2”:2.0; ...}

3 个答案:

答案 0 :(得分:0)

问题可能来自导入[j] = worksheet2.cell_value(x,2)的值。该cell_value(x,2)是固定的。输入a之后,如果出现问题,请打印i以验证..

答案 1 :(得分:0)

问题是否可能是由于x的数据类型是一个tupledict?

答案 2 :(得分:0)

不,我已经做了这个例子来测试它,它有效。 x的指数是元组     M =模型( 'Mine_LongTermes_5')

vX={}
for bl in Blocs:
   for e in OpEq:
     for t in Te:
         vX[bl,t,e]= m.addVar(vtype = GRB.BINARY,name = "vX_i{}_e{}_t{}".format(bl,e,t))
m.update()        
for bl in Blocs:
   for e in OpEq:
     for t in Te:
        m.addConstr((vX[bl,t,e]*OpEq_C[e]) <=  (58.0),name = "C04_1_IniX_i{}_t{}_e{}".format(bl,t,e))
m.update()
m.setObjective(quicksum([OpEq_P[e]*vX[bl,t,e] for bl in BlocsR for e in OpEq for t in Te]), GRB.MAXIMIZE)
m.update()
m.optimize()