蝴蝶破蛹¥
2021-01-13 20:49
采纳率: 33.3%
浏览 653
已采纳

如何用python调用cplex?

我的思路是在python中先随机生成两组数据  再将随机生成的两组数据带入到cplex中  最后将两组结果求平均   求这个思路的python代码?我的cplex代码如下

模型文件:

{int}Experts={1,2,3,4,5};
 {int}Alternatives={1,2,3,4,5,6};
 {int}Attribute={1,2,3};
 {int}Boundary_parameter={1,2,3};
 
  float opinion[Experts][Alternatives][Attribute]=...;
 float lambda[Experts]=...;
 float w[Attribute]=...;
 float uk[Experts]=...;
 
 dvar float+ u;
 dvar float+ adjusted_opinions[Experts][Alternatives][Attribute];
  dvar int+ F[Experts][Alternatives];
  dvar int+ FC[Alternatives];
  dvar float+ PV[Experts][Alternatives];
  dvar float+ PVC[Alternatives];
  dvar boolean Y[Experts][Alternatives][Boundary_parameter];
  dvar boolean YC[Alternatives][Boundary_parameter];
  dvar float+ theta[Experts][Boundary_parameter];
  dvar float+ theta_C[Boundary_parameter];
  dvar float+ A[Experts][Alternatives];
   dvar float+ B[Experts][Alternatives][Attribute];
  dvar boolean X[Experts][Alternatives][Attribute];
   dvar float+ AE;
   
 minimize u;
 subject to {
   c1:
     sum(K in Experts,I in Alternatives,J in Attribute)
         abs(opinion[K][I][J]-adjusted_opinions[K][I][J])/6==u;
   
   c2:
   forall(K in Experts,I in Alternatives,J in Attribute)
       X[K][I][J]<=1000*B[K][I][J];     
  
   c3:
     forall(K in Experts,I in Alternatives,J in Attribute)
         abs(opinion[K][I][J]-adjusted_opinions[K][I][J])<=1000*X[K][I][J];    
         
   c4:
     forall(K in Experts,I in Alternatives)
     abs(F[K][I]-FC[I])==A[K][I];
     
   c5:
     sum(K in Experts,I in Alternatives)
        A[K][I]<=(1-0.9)*5*6*2;
        
   c6:
     forall(K in Experts,I in Alternatives)
       sum(J in Attribute)
         w[J]*adjusted_opinions[K][I][J]/6==PV[K][I];
         
   c7:
      forall(I in Alternatives)
       sum(K in Experts)
         lambda[K]*PV[K][I]==PVC[I];
         
   c8:
     forall(K in Experts,I in Alternatives,J in Attribute)
     abs(opinion[K][I][J]-adjusted_opinions[K][I][J])==B[K][I][J];
     
   c9:
     forall(K in Experts,I in Alternatives,J in Attribute)
      B[K][I][J]/6<=uk[K];
      
   c10:
     forall(K in Experts,I in Alternatives)
     sum(P in Boundary_parameter)
       Y[K][I][P]==F[K][I]; 
       
   c11:
     forall(I in Alternatives)
     sum(P in Boundary_parameter)
       YC[I][P]==FC[I];
       
   c12:
     forall(K in Experts,I in Alternatives,P in Boundary_parameter)
       PV[K][I]-theta[K][P]<=Y[K][I][P];
       
   c13:
      forall(K in Experts,I in Alternatives,P in Boundary_parameter)
       Y[K][I][P]-1<=PV[K][I]-theta[K][P];
       
   c14:
      forall(I in Alternatives,P in Boundary_parameter)
       PVC[I]-theta_C[P]<=YC[I][P];
       
   c15:
      forall(I in Alternatives,P in Boundary_parameter)
       YC[I][P]-1<=PVC[I]-theta_C[P];
       
   c16:
      forall(K in Experts,P in Boundary_parameter){
      if(P == 1){
     sum(I in Alternatives)
            Y[K][I][P]==6; 
          }} 
     
    c17:
    forall(K in Experts,P in Boundary_parameter){
      if(P == 2){
     sum(I in Alternatives)
            Y[K][I][P]==4; 
          }}      
   
     
    c18:
    forall(K in Experts,P in Boundary_parameter){
      if(P == 3){
     sum(I in Alternatives)
            Y[K][I][P]==2; 
          }}
          
    c19:
      forall(P in Boundary_parameter){
      if(P == 1){
     sum(I in Alternatives)
            YC[I][P]==6; 
          }} 
     
    c20:
    forall(P in Boundary_parameter){
      if(P == 2){
     sum(I in Alternatives)
            YC[I][P]==4; 
          }}      
     
     
    c21:
    forall(P in Boundary_parameter){
      if(P == 3){
     sum(I in Alternatives)
            YC[I][P]==2; 
          }}
          
    c22:
      forall(K in Experts,I in Alternatives,J in Attribute)
      adjusted_opinions[K][I][J]<=6; 
      
    c23:
      sum(K in Experts,I in Alternatives,J in Attribute)
       X[K][I][J]==AE;              
  }                  

 

数据文件:

lambda=[0.2    0.2    0.2    0.2 0.2];
uk=[0.1 0.1 0.1 0.1 0.1];
w=[0.3    0.4    0.3];

opinion=[[[5.02    5.66    2.47]
[3.15    3.61    2.79]
[2.98    5.96    1.82]
[1.53    4.24    1.67]
[0.4    4.85    1.66]
[5.09    1.24    0.99]]
        
[[5.16    0.8    0.02]
[2.88    0.67    2.13]
[3.27    1.96    5.48]
[5.73    1.7    2.03]
[4.38    4.34    0.59]
[2.37    5.08    5.09]]
        
[[0.66    0.42    4.41]
[3.97    0.28    5.42]
[1.16    3.81    4.29]
[4.1    4.67    4.29]
[3.6    5.43    4.77]
[5.44    5.62    1.86]]
        
[[5.65    4.45    4.28]
[0.71    3.37    1.67]
[0.14    0.53    5.3]
[0.28    2.58    5.24]
[5.85    5.12    5.63]
[3.87    2.42    5.18]]
        
[[1.37    2.1    4.03]
[2.55    0.44    4.63]
[2.37    5.29    2]
[3.02    0.76    4.02]
[0.5    5.35    3.83]
[0.74    5.04    1.83]]];

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

32条回答 默认 最新

  • bj_0163_bj 2021-01-15 12:35
    最佳回答
    res_x=[] ##定义一个初始空向量
    for i in range(100): ###循环100次
        建立规划方程
        求解变量值x  ###求解出变量的值5个变量,x的格式就是 类似这种 [2,3,4,5,2]
        res_x.append(x) ##x 的值存下来
        ###res_x的最终格式是[[],[],[]] 这种
    print(np.array(res_x).mean(axis=0))  ###按列求平均 ,求出来的值是 [2,3,4,5,2] 这种格式,也就是每个变量的平均值
    
    评论
    解决 无用
    打赏 举报
查看更多回答(31条)

相关推荐 更多相似问题