dL0 = 0;
dL1 = 0;
dL2 = 0;
dL3 = 0;
%任务0-6的周期
c0 =2.000000;
c1 =1.000000;
c2 =9.000000;
c3 =5.000000;
c4 =2.000000;
c5 =2.000000;
c6 =8.000000;
c7 =1.000000; %消息7-10的传输时间
c8 =0.500000;
c9 =1.000000;
c10 =1.000000;
%GP算法求反应时间时使用的偏移
a_0_1 = 0.010;
a_0_2 = 0.010;
a_1_2 = 0.010;
a_3_4 = 0.010;
a_0_5 = 0.010;
a_1_5 = 0.010;
a_2_5 = 0.010;
a_3_6 = 0.010;
a_4_6 = 0.010;
aYm_7 = 0.010;
aIm_8_7 = 0.010;
aIm_9_7 = 0.010;
aIm_10_7 = 0.010;
aYm_8 = 0.010;
aYm_9 = 0.010;
aIm_8_9 = 0.010;
aYm_10 = 0.010;
aIm_8_10 = 0.010;
aIm_9_10 = 0.010;
maxError = 0.100000;
e = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
outerTimes = 1;
value_now=0;
innerIterTimes = 0;iterTimes = 0;
while outerTimes < 100
outerTimes = outerTimes + 1;
gpvar p0 r0 p1 r1 p2 r2 p3 r3 p4 r4 p5 r5 p6 r6 p7 r7 p8 r8 p9 r9 p10 r10 s0 s1 Im7 Ym7 Im8 Ym8 Im9 Ym9 Im10 Ym10 ;
objective = p0+r0+p1+r1+p2+r2+p0+r0+p1+r1+p8+r8+p3+r3+p4+r4+p9+r9+p5+r5+p10+r10+p6+r6+p0+r0+p1+r1+p8+r8+p3+r3+p4+r4+p6+r6+p0+r0+p7+r7+p6+r6
%约束条件矩阵
constr=[
%CPU利用率限制
c0/p0 + c1/p1 + c2/p2 + c5/p5<=1;
c3/p3 + c4/p4 + c6/p6<=1;
c0 <= r0;
%任务反应时间限制
c1 + (r1/p0 + a_0_1) * c0 <= r1;
c2 + (r2/p0 + a_0_2) * c0 + (r2/p1 + a_1_2) * c1 <= r2;
c3 <= r3;
c4 + (r4/p3 + a_3_4) * c3 <= r4;
c5 + (r5/p0 + a_0_5) * c0 + (r5/p1 + a_1_5) * c1 + (r5/p2 + a_2_5) * c2 <= r5;
c6 + (r6/p3 + a_3_6) * c3 + (r6/p4 + a_4_6) * c4 <= r6;
%equation(4) 消息反应时间限制 消息8,9,10 > 消息7
(Im7/p8 + aIm_8_7) * c8 + (Im7/p9 + aIm_9_7) * c9 + (Im7/p10 + aIm_10_7) * c10 <= Im7;%求消息7被消息8,9,10抢占的时间
((c7 + Im7)/s0 + aYm_7) * (s0 + s1) <= Ym7; %求消息7被其它节点slot抢占时间
c7 + Ym7 <= r7; %求消息7反应时间
%消息8没有被抢占
(c8/s0 + aYm_8) * (s0 + s1) <= Ym8;
c8 + Ym8 <= r8;
%消息8 > 消息9
(Im9/p8 + aIm_8_9) * c8 <= Im9;
((c9 + Im9)/s1 + aYm_9) * (s0 + s1) <= Ym9;
c9 + Ym9 <= r9;
%消息8,9 > 消息10
(Im10/p8 + aIm_8_10) * c8 + (Im10/p9 + aIm_9_10) * c9 <= Im10;
((c10 + Im10)/s0 + aYm_10) * (s0 + s1) <= Ym10;
c10 + Ym10 <= r10;
%消息传输时间 < slot time
c7 <= s0;
c8 <= s0;
c9 <= s1;
c10 <= s0;
%response time < period
r0 <= p0;
r1 <= p1;
r2 <= p2;
r3 <= p3;
r4 <= p4;
r5 <= p5;
r6 <= p6;
%for message
r7 <= p7;
r8 <= p8;
r9 <= p9;
r10 <= p10;
];
%目标
[min_objective, solution, status] = gpsolve(objective, constr, 'min')
assign(solution);
r0_bef = 0; r0_aft = 0;
r1_bef = 0; r1_aft = 0;
r2_bef = 0; r2_aft = 0;
r3_bef = 0; r3_aft = 0;
r4_bef = 0; r4_aft = 0;
r5_bef = 0; r5_aft = 0;
r6_bef = 0; r6_aft = 0;
r7_bef = 0; r7_aft = 0;
r8_bef = 0; r8_aft = 0;
r9_bef = 0; r9_aft = 0;
r10_bef = 0; r10_aft = 0;
Im7_aft=0; Ym7_aft=0;
Im8_aft=0; Ym8_aft=0;
Im9_aft=0; Ym9_aft=0;
Im10_aft=0; Ym10_aft=0;
times = 1;
%ne()不相等判断 相等返回0,不相等返回1
while (ne(r0_aft, r0_bef) | ne(r1_aft, r1_bef) | ne(r2_aft, r2_bef) | ne(r3_aft, r3_bef) | ne(r4_aft, r4_bef) | ne(r5_aft, r5_bef) | ne(r6_aft, r6_bef) | ne(r7_aft, r7_bef) | ne(r8_aft, r8_bef) | ne(r9_aft, r9_bef) | ne(r10_aft, r10_bef) | times ==1) && times<=20
%20次迭代求反应时间
times = times + 1;
r0_bef = r0_aft;
r1_bef = r1_aft;
r2_bef = r2_aft;
r3_bef = r3_aft;
r4_bef = r4_aft;
r5_bef = r5_aft;
r6_bef = r6_aft;
r7_bef = r7_aft;
r8_bef = r8_aft;
r9_bef = r9_aft;
r10_bef = r10_aft;
r0_aft = c0;
r1_aft = c1 + ceil(r1_bef/p0) * c0;
r2_aft = c2 + ceil(r2_bef/p0) * c0 + ceil(r2_bef/p1) * c1;
r3_aft = c3;
r4_aft = c4 + ceil(r4_bef/p3) * c3;
r5_aft = c5 + ceil(r5_bef/p0) * c0 + ceil(r5_bef/p1) * c1 + ceil(r5_bef/p2) * c2;
r6_aft = c6 + ceil(r6_bef/p3) * c3 + ceil(r6_bef/p4) * c4;
%求消息7的 两种抢占时间和反应时间
%equation(4)
Im7_aft = ceil(Im7_aft/p8) * c8 + ceil(Im7_aft/p9) * c9 + ceil(Im7_aft/p10) * c10;
%equation(5)
Ym7_aft = ceil((c7 + Im7_aft)/s0) * (s0 + s1);
%equation(6)
r7_aft = c7 + Ym7_aft;
Ym8_aft = ceil(c8/s0) * (s0 + s1);
r8_aft = c8 + Ym8_aft;
Im9_aft = ceil(Im9_aft/p8) * c8;
Ym9_aft = ceil((c9 + Im9_aft)/s1) * (s0 + s1);
r9_aft = c9 + Ym9_aft;
Im10_aft = ceil(Im10_aft/p8) * c8 + ceil(Im10_aft/p9) * c9;
Ym10_aft = ceil((c10 + Im10_aft)/s0) * (s0 + s1);
r10_aft = c10 + Ym10_aft;
end; %end while
path_aft0 = p0 + r0_aft + p1 + r1_aft + p2 + r2_aft;
path_aft1 = p0 + r0_aft + p1 + r1_aft + p8 + r8_aft + p3 + r3_aft + p4 + r4_aft + p9 + r9_aft + p5 + r5_aft + p10 + r10_aft + p6 + r6_aft;
path_aft2 = p0 + r0_aft + p1 + r1_aft + p8 + r8_aft + p3 + r3_aft + p4 + r4_aft + p6 + r6_aft;
path_aft3 = p0 + r0_aft + p7 + r7_aft + p6 + r6_aft;
fprintf('path_aft0 = %f\n', path_aft0);
fprintf('path_aft1 = %f\n', path_aft1);
fprintf('path_aft2 = %f\n', path_aft2);
fprintf('path_aft3 = %f\n', path_aft3);
pathSum = path_aft0 + path_aft1 + path_aft2 + path_aft3;
fprintf('min_objective_aft now is: %f\n', pathSum);
%relative approximation error, equation(22,23)
e(1) = (r0 - r0_aft)/r0;
e(2) = (r1 - r1_aft)/r1;
e(3) = (r2 - r2_aft)/r2;
e(4) = (r3 - r3_aft)/r3;
e(5) = (r4 - r4_aft)/r4;
e(6) = (r5 - r5_aft)/r5;
e(7) = (r6 - r6_aft)/r6;
e(8) = (r7 - r7_aft)/r7;
e(9) = (r8 - r8_aft)/r8;
e(10) = (r9 - r9_aft)/r9;
e(11) = (r10 - r10_aft)/r10;
fprintf('e(0) = %f\n',e(1));
fprintf('e(1) = %f\n',e(2));
fprintf('e(2) = %f\n',e(3));
fprintf('e(3) = %f\n',e(4));
fprintf('e(4) = %f\n',e(5));
fprintf('e(5) = %f\n',e(6));
fprintf('e(6) = %f\n',e(7));
fprintf('e(7) = %f\n',e(8));
fprintf('e(8) = %f\n',e(9));
fprintf('e(9) = %f\n',e(10));
fprintf('e(10) = %f\n',e(11));
i=1;
value_now=0;
while i<=11
if(abs(e(i))<=0.100000)
value_now = value_now + 1;
end;
i=i+1;
end;
fprintf('satisfied task num: %d\n', value_now);
%if (value_now == nodeNum)
if (value_now == 11)
fprintf('terminiated, all the errors have been reduced to acceptable range\n');
end;
%更新a_0_1等值 对1求模
a_0_1 = mod(abs(a_0_1 - e(2)), 1);
a_0_2 = mod(abs(a_0_2 - e(3)), 1);
a_1_2 = mod(abs(a_1_2 - e(3)), 1);
a_3_4 = mod(abs(a_3_4 - e(5)), 1);
a_0_5 = mod(abs(a_0_5 - e(6)), 1);
a_1_5 = mod(abs(a_1_5 - e(6)), 1);
a_2_5 = mod(abs(a_2_5 - e(6)), 1);
a_3_6 = mod(abs(a_3_6 - e(7)), 1);
a_4_6 = mod(abs(a_4_6 - e(7)), 1);
aYm_7 = mod(abs(aYm_7 - e(8)),1);
aIm_8_7 = mod(abs(aIm_8_7 - e(8)), 1);
aIm_9_7 = mod(abs(aIm_9_7 - e(8)), 1);
aIm_10_7 = mod(abs(aIm_10_7 - e(8)), 1);
aYm_8 = mod(abs(aYm_8 - e(9)),1);
aYm_9 = mod(abs(aYm_9 - e(10)),1);
aIm_8_9 = mod(abs(aIm_8_9 - e(10)), 1);
aYm_10 = mod(abs(aYm_10 - e(11)),1);
aIm_8_10 = mod(abs(aIm_8_10 - e(11)), 1);
aIm_9_10 = mod(abs(aIm_9_10 - e(11)), 1);
end; %end while
%end;
%end;