clc
clear all
close all

Results = xlsread('Results_Case_CMG_1.xlsx');

global S_struct

Data_p = (1/3)*size(Results,1); % Number of data points for history matching

inj = Results(1:Data_p,2:4);
qp = Results(1:Data_p,5:6);
cwf = Results(1:Data_p,7:8);
cinj = Results(1:Data_p,9);
T = Results(1:Data_p,1);
ni = 3; %length(mwell_i);
np = 2; %length(mwell_p);
TC = ni*np;

lx = [0.01*ones(TC,1);5*ones(TC,1);0.1*ones(TC,1);2*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);

%Location of Wells
hi = [1153565.888683;1167041.425060;1165981.176032];
ii = [-24665171.424087;-24657424.130630;-24664917.158948];
hp = [1160762.307872;1164372.385021];
ip = [-24663256.643936;-24660351.459976];

Lwell = CalculateLength(hi,ii,hp,ip);  

tic
%% IMPLEMENTATION OF GA
rseed =  22; 
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;
warning('off','all')
warning

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







save('Match_Parameters_3I2P_CMG_3.mat','xg');

toc






