clc
clear all
close all

load('results_3P_Side_C.mat')

global S_struct

Data_p = 5000; % Number of data points for history matching

mwell_i = [1];

inj = 35000*ones(Data_p,length(mwell_i));
qp = -q_p(:,1:Data_p)';
cwf = cs_hist(:,1:Data_p)';
cinj = 350;
T = T(1:Data_p)';
ni = length(mwell_i);
np = length(mwell_p);
TC = ni*np;

lx = [0.01*ones(TC,1);3*ones(TC,1);0.1*ones(TC,1);1*ones(TC,1);3*ones(TC,1)];
ux = [ones(TC,1);15*ones(TC,1);500*ones(TC,1);10*ones(TC,1);15*ones(TC,1)];


A = zeros(ni,length(lx));
for li = 1:ni
    for lp = 1:np
        A(li,(np*li)-(lp-1)) = 1;
    end
end
    
b = ones(ni,1);


Lwell = CalculateLength(mwell_i,mwell_p,H,I,dx,dy);  


tic
%% IMPLEMENTATION OF GA
rseed =  524643; 
rng(rseed,'twister');

disp('method is '), disp('GA'); %
disp('random seed is '), disp(rseed); %

options = optimoptions('ga','Display','iter','UseVectorized', true);
options.MaxGenerations = 80;

S_struct.inj = inj;
S_struct.qp = qp;
S_struct.T= T;
S_struct.cinj = cinj;
S_struct.cwf = cwf;
% S_struct.NDiv = NDiv;
S_struct.Lwell = Lwell;
S_struct.mwell_i = mwell_i;
S_struct.mwell_p = mwell_p;
warning('off','all')
warning


[xg,fval] = ga(@ObjFxn_HM_PROXY,length(lx),A,b,[],[],lx,ux,[],options);





toc
save('Match_Parameters_3P_Side_C.mat','xg');








