MATLAB Answers

Unable to update while loop condition

1 view (last 30 days)
Mohamed Kamruddin
Mohamed Kamruddin on 21 Apr 2021
Answered: David Hill on 21 Apr 2021
To helpers out there, I am trying to implement a price negotiation algorithm on Matlab but unable to converge the code as the while loops value is not updating. The Oij_k1 value is the output of the while loop which updates in the end of the loop and runs back up as an input to compare the difference with Oij_k.
%% SELLER NEGOTIATION
close all
clc
%% Constants for Calculations
tol = 0.001;
Tij = 0.54;
u_Lambda_i = 0.1;
u_g_i = 0.1;
u_p_i = 0.01;
k=1;
Klim=30;
%% Conditions for sellers
alpha_i = 0.11;
beta_i = 6.1;
gi_k = 0.9;
gi_k16a = zeros();%updates later
Pi = 0;%min power of seller ALWAYS ZERO
Oij_k = [5 12.68];%calculated
Oij_k1 = zeros(1,2);%;FINAL OFFER TO SEND TO BUYER
Lambda_ij_k = 12.68;%Eqn on top in sellers
Pij_k = 5;%max power of seller
%% Conditions for buyers
alpha_j = 0.21;
beta_j = 14.2;
gj_k = 0.5;
Pji_k = 1;%min power of buyer
Lambda_ji_k = -(alpha_j*Pji_k)+beta_j*(1-gi_k)-Tij;%2.2
%% Equations
Pij_k11 = zeros();
Pij_k15a = zeros();
Pij_k17 = zeros();
Lambda_ijr_k= zeros();%9a
Lambda_ij_k11 = zeros();%11
Lambda_ij_k14 = zeros();%14
Lambda_ij_k15a = zeros();%15a
Lambda_ij_k17 = zeros();%17
Offer11 = zeros(1,2);
Offer15a = zeros(1,2);
Offer17 = zeros(1,2);
%%
while max(abs(Oij_k1-Oij_k))<tol || (k<Klim)
% RECEIVE O_JI_K FROM BUYER
%keyboard
Lambda_ijr_k=(alpha_i*Pji_k)+beta_i*(1+gi_k)+Tij;%9a
if abs(Lambda_ijr_k-Lambda_ji_k)<tol % 11
Pij_k11 = max(Pi,Pji_k);%11
Lambda_ij_k11=(alpha_i*Pij_k11)+beta_i*(1+gi_k)+Tij;%11
else
if abs(Pij_k-Pji_k)<tol
if Lambda_ji_k>=Lambda_ij_k
Lambda_ij_k14=(Lambda_ij_k+Lambda_ji_k)/2;%14
else
Lambda_ij_k15a=max(Lambda_ijr_k,Lambda_ij_k-u_Lambda_i);%15a
end
Pij_k15a = Pji_k;%FIX THESE TWO
gi_k1 = gi_k;%AND THIS
end
gi_k16a=max(0,gi_k-u_g_i);%16a
Pij_k17=max(Pi,Pij_k-u_p_i*(Pij_k-Pji_k));%17
Lambda_ij_k17=(alpha_i*Pij_k17)+beta_i*(1+gi_k16a)+Tij;%17
end
k=k+1;
if Lambda_ij_k11> 0
Offer11(2) = Lambda_ij_k11;
Offer11(1) = Pij_k11;
end
if Lambda_ij_k15a> 0
Offer15a(2) = Lambda_ij_k15a;
Offer15a(1) = Pij_k15a;
end
if Lambda_ij_k17> 0
Offer17(2) = Lambda_ij_k17;
Offer17(1) = Pij_k17;
end
offers = [Offer11;Offer15a;Offer17]
Oij_k1 = max(offers)
Pji_k = Oij_k1(1);
Lambda_ji_k = Oij_k1(2);
%keyboard
end
disp ([Offer11])
disp ([Offer15a])
disp ([Offer17])

Answers (2)

David Hill
David Hill on 21 Apr 2021
Oij_k1=[1 1];%need to define before using
while max(abs(Oij_k1-Oij_k))>tol || (k<Klim)%I believe you want >tol
  1 Comment
Mohamed Kamruddin
Mohamed Kamruddin on 21 Apr 2021
I did define the Oij_k1 = zeros (1,2) so that it could be updated later
Also, >tol does not take the iteration (k<Klim) into account

Sign in to comment.


David Hill
David Hill on 21 Apr 2021
Not sure what your question is then. The following works just fine for me.
while max(abs(Oij_k1-Oij_k))>tol && (k<Klim)

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by