Output is not as expected
3 次查看(过去 30 天)
显示 更早的评论
Sahithi Kandhukuri
2024-1-8
I m trying to do ant colony optimisation based upqc but the out is not as expected ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585331/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585331/image.jpeg)
I am getting above output i.e, zeroes but the required one is as shown below
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585336/image.jpeg)
13 个评论
Sam Chak
2024-1-8
I haven't tested everything yet, but I experimented with a basic quadratic function and observed that the optimal solution obtained through Ant Colony Optimization does not yield the lowest cost. Until this issue is addressed, it does not make sense to proceed with the current optimization problem.
format long g
%% ACO parameters
n_iter = 5; % number of iteration
NA = 5; % Number of Ants
alpha = 0.8;
beta = 0.2;
roh = 0.7; % Evaporation rate
n_param = 1; % Number of parameters
LB = 0.1*ones(1,n_param); % lower band
UB = 2.0*ones(1,n_param); % upper band
n_node = 1000; % number of nodes for each param
%% intilalizing some variables
cost_best_prev = inf;
ant = zeros(NA,n_param);
cost = zeros(NA,1);
tour_selected_param = zeros(1,n_param);
param_mat = zeros(n_iter,n_param);
Nodes = zeros(n_node,n_param);
prob = zeros(n_node,n_param);
%% Generating nodes
T = ones(n_node, n_param).*eps;
dT = zeros(n_node, n_param);
for i = 1:n_param
Nodes(:,1) = linspace(LB(i), UB(i), n_node);
end
%% iteration loop
for iter = 1:n_iter
for tour_i = 1:n_param
prob(:,tour_i) = (T(:,tour_i).^alpha).*((1./Nodes(:,tour_i)).^beta);
prob(:,tour_i) = prob(:,tour_i)./sum(prob(:,tour_i));
end
for A = 1:NA
for tour_i = 1:n_param
node_se1 = rand;
node_ind = 1;
prob_sum = 0;
for j = 1:n_node
prob_sum = prob_sum + prob(j,tour_i);
if prob_sum >= node_se1
node_ind = j;
break
end
end
ant(A,tour_i) = node_ind;
tour_selected_param(tour_i) = Nodes(node_ind, tour_i);
%%
end
%% Put the Cost function here:
% cost(A) = cost_func(tour_selected_param);
cost(A) = costume(tour_selected_param);
disp(['Ant number: ' num2str(A)])
disp(['Ant Cost: ' num2str(cost(A))])
disp(['Ant parameters: ' num2str(tour_selected_param)])
if iter~=1
disp(['iteration:' num2str(iter)])
disp('________________')
disp(['Best cost:' num2str(cost_best)])
for i=1:n_param
tour_selected_param(i) = Nodes(ant(cost_best_ind,1), i);
end
disp(['Best parameters:' num2str(tour_selected_param)])
end
end
[cost_best, cost_best_ind] = min(cost);
% Elistem
if (cost_best > cost_best_prev) && (iter ~= 1)
[cost_worst, cost_worst_ind] = max(cost);
ant(cost_worst_ind,:) = best_prev_ant;
cost_best = cost_best_prev;
cost_best_ind = cost_worst_ind;
else
cost_best_prev = cost_best;
best_prev_ant = ant(cost_best_ind,:);
end
dT = zeros(n_node,n_param); % Change of Phormone
for tour_i = 1:n_param
for A = 1:NA
dT(ant(A, tour_i), tour_i) = dT(ant(A, tour_i), tour_i) + cost_best/cost(A);
end
end
T = roh.*T + dT;
end
Ant number: 1
Ant Cost: 1.4451
Ant parameters: 1.6672
Ant number: 2
Ant Cost: 1.0667
Ant parameters: 1.2583
Ant number: 3
Ant Cost: 1.5524
Ant parameters: 1.7432
Ant number: 4
Ant Cost: 1.9254
Ant parameters: 1.962
Ant number: 5
Ant Cost: 1.7482
Ant parameters: 1.865
Ant number: 1
Ant Cost: 1.5524
Ant parameters: 1.7432
iteration:2
________________
Best cost:1.0667
Best parameters:1.2583
Ant number: 2
Ant Cost: 1.4451
Ant parameters: 1.6672
iteration:2
________________
Best cost:1.0667
Best parameters:1.6672
Ant number: 3
Ant Cost: 1.0667
Ant parameters: 1.2583
iteration:2
________________
Best cost:1.0667
Best parameters:1.6672
Ant number: 4
Ant Cost: 1.0667
Ant parameters: 1.2583
iteration:2
________________
Best cost:1.0667
Best parameters:1.6672
Ant number: 5
Ant Cost: 1.9254
Ant parameters: 1.962
iteration:2
________________
Best cost:1.0667
Best parameters:1.6672
Ant number: 1
Ant Cost: 1.7482
Ant parameters: 1.865
iteration:3
________________
Best cost:1.0667
Best parameters:1.2583
Ant number: 2
Ant Cost: 1.0667
Ant parameters: 1.2583
iteration:3
________________
Best cost:1.0667
Best parameters:1.2583
Ant number: 3
Ant Cost: 1.7482
Ant parameters: 1.865
iteration:3
________________
Best cost:1.0667
Best parameters:1.865
Ant number: 4
Ant Cost: 1.0667
Ant parameters: 1.2583
iteration:3
________________
Best cost:1.0667
Best parameters:1.865
Ant number: 5
Ant Cost: 1.4451
Ant parameters: 1.6672
iteration:3
________________
Best cost:1.0667
Best parameters:1.865
Ant number: 1
Ant Cost: 1.5524
Ant parameters: 1.7432
iteration:4
________________
Best cost:1.0667
Best parameters:1.2583
Ant number: 2
Ant Cost: 1.4451
Ant parameters: 1.6672
iteration:4
________________
Best cost:1.0667
Best parameters:1.6672
Ant number: 3
Ant Cost: 1.0667
Ant parameters: 1.2583
iteration:4
________________
Best cost:1.0667
Best parameters:1.6672
Ant number: 4
Ant Cost: 1.0667
Ant parameters: 1.2583
iteration:4
________________
Best cost:1.0667
Best parameters:1.6672
Ant number: 5
Ant Cost: 1.4451
Ant parameters: 1.6672
iteration:4
________________
Best cost:1.0667
Best parameters:1.6672
Ant number: 1
Ant Cost: 1.5524
Ant parameters: 1.7432
iteration:5
________________
Best cost:1.0667
Best parameters:1.2583
Ant number: 2
Ant Cost: 1.0667
Ant parameters: 1.2583
iteration:5
________________
Best cost:1.0667
Best parameters:1.2583
Ant number: 3
Ant Cost: 1.9254
Ant parameters: 1.962
iteration:5
________________
Best cost:1.0667
Best parameters:1.962
Ant number: 4
Ant Cost: 1.0667
Ant parameters: 1.2583
iteration:5
________________
Best cost:1.0667
Best parameters:1.962
Ant number: 5
Ant Cost: 1.9254
Ant parameters: 1.962
iteration:5
________________
Best cost:1.0667
Best parameters:1.962
%% ACO Result
k = tour_selected_param
k =
1.96196196196196
J = costume(k)
J =
1.92537081626171
%% Compare with other Optimizer
[kopt, fval] = fminunc(@costume, 2)
Local minimum found.
Optimization completed because the size of the gradient is less than
the value of the optimality tolerance.
kopt =
1
fval =
1
%% Custom Optimization Solution for Targeted User Minimum Equation
function J = costume(k)
J = (k - 1)^2 + 1;
end
Sam Chak
2024-1-8
Okay, I gave it a shot. I experimented with your Ant Colony Optimization algorithm (ACO) using a basic quadratic function. See the graph below. The minimum value occurs when
, and at that point, the cost is
. If the ACO works well, it should return
after numerous iterations.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585636/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585641/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585646/image.png)
J = @(k) (k - 1).^2 + 1;
k = 0:0.01:2;
plot(k, J(k)), hold on
plot(1, 1, 'ro', 'markersize', 12, 'linewidth', 2), grid
xlabel k, ylabel J
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1585651/image.png)
Sahithi Kandhukuri
2024-1-8
Can you please suggest me a way forward
How to make that ACO code work fine
Torsten
2024-1-8
编辑:Torsten
2024-1-8
Why not asking the author of the code ?
I don't know exactly what it means in the code description under the above address:
MATLAB implementation of ACO for Discrete and Combinatorial Optimization Problems
Maybe it's not suited for your (and the OP's) continuous optimization problem.
Sam Chak
2024-1-9
Upon reviewing the code, Yarpiz developed an iteration of Ant Colony Optimization (ACO) designed for addressing Combinatorial Optimization Problems, such as the well-known Travelling Salesman and Binary Knapsack problems. Overall, I am of the opinion that the ACO concept can be applied to optimize specific control design parameters within continuous-time dynamical systems.
This problem involves numerous design factors that impact the solution, especially when there are no error messages. It's not as straightforward as a simple equation like 1 + 2 = 3. For instance, at the 11:40 timestamp in the video, the optimal solution is presented as follows:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1586396/image.png)
If the Simulink parameters are configured appropriately,
%% Best solution by ACO
k = [77 230 193 30 330 290];
%% Parameters in Simulink
ke = k(1,1);
kce = k(1,2);
ku = k(1,3);
X = k(1,4);
z_a = abs(k(1,5));
v_a = abs(k(1,6));
%% Fuzzy Logic parameters in Simulink
% negative big
NB = -X;
% negative medium
NM = -X/2;
% positive medium
PM = X/2;
% positive big
PB = X;
% Sam: Zero
Z = 0; % set by User
we would anticipate witnessing identical results (for deterministic systems) as demonstrated in the YouTube video:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1586401/image.png)
However, the outcomes (Load Voltage and Injected Voltage) I achieved in R2023b are evidently different.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1586406/image.png)
Sam Chak
2024-1-11
In theory, yes. The keys to achieving the same results lie in your contributions to this work. The test suggests that despite using the same optimal values, your Simulink model may differ from the YouTube version.
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)