这几天做题遇到了较为复杂的规划问题,在想能不能用MATLAB实现像LINGO那样的偏向用户输入的编写方法,而非MATLAB矩阵式的,最终实现了基于问题的求解代码。
代码 主文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 m=[50 ;50 ;50 ;150 ;100 ;150 ;50 ;100 ;50 ;50 ;50 ]; MM=[450 ;500 ;300 ;500 ;550 ;350 ;450 ;250 ;350 ;400 ;250 ]; b=[77 ;98 ;117 ;145 ;156 ;167 ;178 ;126 ;103 ;129 ;133 ]; r=[0.4 ;0.6 ;0.5 ;0.6 ;0.7 ;0.8 ;0.9 ;0.6 ;0.2 ;0.3 ;0.4 ]; v = 1 ; jieguo = []; taoshu = []; for i = 0 :10 v = 1 - 0.01 *i ; x = optimvar('x' ,11 ,'LowerBound' , 0 , 'Type' ,'integer' ); prob = optimproblem('ObjectiveSense' , 'max' , 'Objective' , qiujie(x) ); t1 = sum(r .* x) ./ sum(x); con1 = [ m <= x x <= MM 0.7069 * v <= t1 sum(x(1 :8 ).*b(1 :8 ))./102077.6 - sum(x(9 :11 ).*b(9 :11 ))./102077.6 <= 2.28 ]; prob.Constraints.con1 = con1; [sol,fvl] = solve(prob); jieguo(i +1 ) = fvl; taoshu(:,i +1 ) = sol.x; end final = [jieguo;taoshu] 目标函数文件 function f = qiujie (x) b=[77 98 117 145 156 167 178 126 103 129 133 ]; c=[4263 4323 4532 5288 5268 5533 5685 4323 2663 2791 2982 ]; p=[12000 10800 11200 12800 12800 13600 14000 10400 6400 6800 7200 ]; sum1=0 ; for i =1 :8 ;sum1=sum1+x(i )*b(i ); end R=sum1/102077.6 B=0 ; for i =1 :11 ;B=B+x(i )*b(i ); end B b1=0 ; for i =1 :3 ;b1=b1+x(i )*b(i ); end b1 b2=0 ; for i =4 :8 ;b2=b2+x(i )*b(i ); end b2=b2+x(11 )*b(11 ) sum2=0 ; for i =4 :8 ;sum2=sum2+x(i )*b(i )*p(i ); end sum2=sum2+x(11 )*b(11 )*p(11 ); for i =9 :10 ;sum2=sum2+x(i )*b(i )*p(i )*b2/(b1+b2); end sum2 17 s1=0 ; for i =4 :8 s1=s1+x(i )*b(i ); end s1=s1+x(11 )*b(11 ); for i =9 :10 s1=s1+x(i )*b(i )*b2/(b1+b2); end s1=777179627 *s1/B; s2=0 ; for i =4 :7 ;s2=s2+x(i )*b(i )*c(i ); end for i =9 :10 s2=s2+x(i )*b(i )*c(i )*b2/(b1+b2); end s2 s3=(s1+s2)*0.3 ; s4=0 ; for i =4 :8 ;s4=s4+x(i )*b(i )*p(i ); end s4=s4+x(11 )*b(11 )*p(11 ); for i =9 :10 ;s4=s4+x(i )*b(i )*p(i )*b2/(b1+b2); end s4=s4*0.0565 ; sum3=s1+s2+s3+s4 R2=(sum2-sum3)/sum3 T2 = (sum2-sum3)*0.3 sum4=0 ; for i =1 :3 ;sum4=sum4+x(i )*b(i )*p(i ); end for i =9 :10 ;sum4=sum4+x(i )*b(i )*p(i )*b1/(b1+b2); end sum4 ss1=0 ; for i =1 :3 ss1=ss1+x(i )*b(i ); end for i =9 :10 ss1=ss1+x(i )*b(i )*b1/(b1+b2); end ss1=777179627 *ss1/B; ss2=0 ; for i =1 :2 ;ss2=ss2+x(i )*b(i )*c(i ); end for i =9 :10 ss2=ss2+x(i )*b(i )*c(i )*b1/(b1+b2); end ss3=(ss1+ss2)*0.3 ; ss4=0 ; for i =1 :3 ;ss4=ss4+x(i )*b(i )*p(i ); end for i =9 :10 ss4=ss4+x(i )*b(i )*p(i )*b1/(b1+b2); end ss4=ss4*0.0565 ; sum5=ss1+ss2+ss3+ss4 R1=(sum4-sum5)/sum5 T1 = (sum4-sum5)*0.3 T = T1 + T2 S1=0 ; for i =1 :11 ;S1=S1+x(i )*b(i )*c(i ); end S1 S2=(777179627 +S1)*0.1 S3=0 ; for i =1 :11 ;S3=S3+x(i )*b(i )*p(i ); end S3=S3*0.0565 chengben=777179627 +S1+S2+S3 zong=S3/0.0565 f = (zong-chengben - T)/(chengben + T)
参考 https://ww2.mathworks.cn/help/optim/ug/optimproblem.html https://zhuanlan.zhihu.com/p/359408474