%  This code original developed by The Center for Engineering Application &
%  Technology Solution (CEATS), Ho Chi Minh City, Open University, Vietnam.
%  https://ceats.ou.edu.vn/us/ 
%  programming: Hoang-Le Minh
%
%  paper:Optimization of high-rise concrete structure using a sequence design based on a proposed surrogate model
%        (PSM) and Finite element (FE) model updating
%
%    
%  Hoang-Le Minh, Thanh Sang-To, Binh Le-Van, Thanh Cuong-Le
%
%  Email: hoang.lm@ou.edu.vn, cuong.lt@ou.edu.vn
%
%  Journal: Archives of Civil and Mechanical Engineering

close all
clear all
clc

load('Best_model_KO_V03.mat')
load('input_section.txt')
load('output_DD.txt')
load('output_nonDD.txt')
input_data = input_section;

%% Normalize the data (scaling to max = 1)
output_nonDD_displacement = output_nonDD(1,:) ./ max(output_nonDD(1,:)); % max displacement - model 1
output_nonDD_driff        = output_nonDD(2,:) ./ max(output_nonDD(2,:)); % max drift - model 2
output_DD_driff           = output_DD(1,:) ./ max(output_DD(1,:));       % drift with DD - model 3
temp = output_DD;
temp(1,:) = []; % remove the first row
output_DD_Vd = temp ./ max(max(temp)); % Vd for model 4

%% --- Define architecture for model 1 (max displacement without DD)
input = input_data;
target = output_nonDD_displacement;
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio   = 15/100;
net.divideParam.testRatio  = 15/100;

%% Load model_1
% G_particle_pso.model_1.cost
% G_particle_pso.model_2.cost
% G_particle_pso.model_3.cost
% G_particle_pso.model_4.cost
% obj = G_particle_pso.model_1.cost + G_particle_pso.model_2.cost + G_particle_pso.model_3.cost + G_particle_pso.model_4.cost
% design_variable = G_particle_pso.pos
% [model_1, model_2, model_3, model_4, Obj] = fobj_DNN(design_variable)

%% ----------------------------------
G_best_kmean.model_1
% Define the surrogate model_1
% View the Network
network_model_1 = G_best_kmean.model_1.net;
tr = G_best_kmean.model_1.train_parameters;
train_parameters = tr;
plotperform(tr);

%% Plot regression for model 1
% Training set
train_num     = size(train_parameters.trainInd, 2);
predict_train = network_model_1(input(:, 1:train_num));
figure(2)
plotregression(target(:,1:train_num), predict_train, 'Train data')

% Validation set
valid_test = size(train_parameters.valInd, 2);
valid_num  = train_num + valid_test;
predict_train = network_model_1(input(:, train_num:valid_num));
figure(3)
plotregression(target(:, train_num:valid_num), predict_train, 'Validation data')

% Test set
train_test = size(train_parameters.testInd, 2);
test_num = valid_num + train_test;
predict_train = network_model_1(input(:, valid_num:test_num));
figure(4)
plotregression(target(:, valid_num:test_num), predict_train, 'Test data');

% All data
predict_all = network_model_1(input);
figure(5)
plotregression(target, predict_all, 'All data');




















