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 效果: