clear all;clc;  %全程序循环
time = cputime;

%% parameters
n=900;
m=100;
windows = 9+1;  % instance + target
K = 50;
w = 1;

%% preliminary
load CoupingCosine.mat;
data = coupling;
%% step1: verification and train datasets
for i=1:n-windows+1
    train(i,1:windows) = data(i:i+windows-1);
end
for i=1:m
    verification(i,1:windows) = data(n+i-windows+1:n+i);
end
[n,p] = size(train);
X = train(:,1:end-1);
label = train(:,end);
[m,p] = size(verification);
Xv = verification(:,1:end-1);
real = verification(:,end);

%% step2: k-means++ clustering / Recurrent Phase
[ C,index ] = ClusterOP( X,K,w );
[K,p] = size(C);
%% step3: metric matrix
D = zeros(n,K);
for i=1:n
    D(i,:) = SimpleSide_vOP(X(i,:),C,w);
end
Dv = zeros(m,K);
for i=1:m
    Dv(i,:) = SimpleSide_vOP(Xv(i,:),C,w);
end
temp = max(max(D));
D = D/temp;
Dv = Dv/temp;
save predata D Dv label real C;

%% step4: Harmonic Phase
d = 1;
x_input_domain = [0,1;0,1e+06];
Ehaha = @(gamma)OA(gamma,1); 
%[gamma, fx, X_sample, F_sample, Result] = IMGPO_default_run ( Ehaha, d, x_input_domain,200,1,1,1,1);
[gamma, fx] = IMGPO_default_run ( Ehaha, d, x_input_domain,100,1,1,1,1);

%% step5: Prediction
load predata.mat;
[ alpha,result(1,:),estimate ] = OptimizationAlpha( D,label,gamma );  %返还测评
[ result(2,:),prediction ] = forecasting( Dv, gamma, alpha, real );   %预测

time = cputime - time

figure;
plot(estimate,'blue');hold on; plot(label,'red');close all;
figure;
plot(prediction,'blue');hold on; plot(real,'red');
%save K50w1 train verification result alpha estimate prediction gamma K C windows;

