MATLAB绘制大小图配置

本文通过MATLAB脚本实现对不同算法的成本进行读取、计算平均值并绘图对比,展示了离线最优算法与离线贪心算法在不同参数设置下的成本变化趋势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/liyanzhong/article/details/51972091

clear;
% ==================================== A. Read the data :
filename = 'Cost_trace_1_TS_step.tr';
%%%% X is tsLen (\ell)
X = textread(filename,'%*s%*s %*s%*s %*s%*s %*s%*s  %*s%f  %*[^\n]');
OPT_C = textread(filename,'%*s%*s %*s%*s %*s%*s %*s%*s  %*s%*s %*s%f %*s%*s');
Greedy_C = textread(filename,'%*s%*s %*s%*s %*s%*s %*s%*s %*s%*s  %*s%*s %*s%f' );
% === A. Read the data :~


% ==================================== B.1 Calculate the Avg-Costs for the Big Figure :
Avg_OPT_C = [];
Avg_Greedy_C = [];


% ---- given x_axis points
X_items =[0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.5,2.0,2.5,3.0,3.5,4.0,5.0];


alpha = 10;
beta = 50;


CNT = length(X_items);
for item = 1:CNT
Val_item = X_items(item);
idx_it = find( X == Val_item );

% --- 1 OPT_C
OPT_costs_its = [];
OPT_costs_its = OPT_C( idx_it );
Len_opt_cost_its = length(OPT_costs_its);
Avg_OPT_C(item) = sum(OPT_costs_its) / Len_opt_cost_its;

% --- 2 Greedy_C
Gdy_costs_its = [];
Gdy_costs_its = Greedy_C( idx_it );
Len_opt_cost_its = length(Gdy_costs_its);
Avg_Greedy_C(item) = sum(Gdy_costs_its) / Len_opt_cost_its;
end
% === B.1 Calculate the Avg-Costs for the Big Figure :~


% ==================================== B.2 Calculate the Avg-Costs for the Small Figure :
Avg_OPT_C2 = [];
Avg_Greedy_C2 = [];


%%%% X_items2 is partial tsLen (\ell)
X_items2=[ 0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.5 ];
CNT2 = length(X_items2);
for item = 1:CNT2
Val_item = X_items2(item);
idx_it = find( X == Val_item );

% --- 1 OPT_C
OPT_costs_its = [];
OPT_costs_its = OPT_C( idx_it );
Len_opt_cost_its = length(OPT_costs_its);
Avg_OPT_C2(item) = sum(OPT_costs_its) / Len_opt_cost_its;

% --- 2 Greedy_C
Gdy_costs_its = [];
Gdy_costs_its = Greedy_C( idx_it );
Len_opt_cost_its = length(Gdy_costs_its);
Avg_Greedy_C2(item) = sum(Gdy_costs_its) / Len_opt_cost_its;
end
% === B.2 Calculate the Avg-Costs for the Small Figure :~


% ==================================== C. Plot ====================================
figure
TextFontSize=20;
LegendFontSize = 18;
% ------------- C.1 Plot the big one : -------------  
set(0,'DefaultAxesFontName','Times',...
    'DefaultLineLineWidth',1,...
    'DefaultLineMarkerSize',8);
set(gca,'FontName','Times New Roman','FontSize',TextFontSize);
set(gcf,'Units','inches','Position',[0 0 6.0 4.0]);

% --- 1 Avg-OPT_costs_1m
plot(X_items, Avg_OPT_C, '-dk')
hold on

% --- 2 Avg-OPT_costs_2m
plot(X_items, Avg_Greedy_C, '--sb')
hold on


grid on
xlabel('{\it l} (seconds)')
ylabel('Cost')
hg1 = legend('Offline OPT', 'Offline Greedy',1);
set(hg1,'FontSize',LegendFontSize);
% --- C.1 Plot the big one :~  


% ------------- C.2 Plot the small one : ------------- 
set(0,'DefaultTextFontName','Times',...
    'DefaultAxesFontName','Times',...
    'DefaultLineLineWidth',1,...
    'DefaultLineMarkerSize',4.5);


h1=axes('position',[0.55 0.245 0.31 0.3]); % set the size of the small figure
set(h1,'FontName','Times New Roman','FontSize',16);
axis(h1);


plot(X_items2, Avg_OPT_C2, '-dk');
hold on
plot(X_items2, Avg_Greedy_C2, '--sb');
hold on
xlim([0.2 1.5]);
ylim([200 360]);
% --- C.2 Plot the small one :~  

%% ----- 1 效果:



         


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值