AI Learning–> Sep.2024

Author

Hossein Dehban (Dehbanh@gmail.com __ 09196596525 )


1-Using Artificial Intelligence(AI) as a general tools

https://chatgpt.com/

https://sider.ai/en

{10.202.10.202 , 10.202.10.102 } —> Change DNS

.

.

.


2-Using Artificial Intelligence(AI) as Machine Learning (ML), (predicting weather events)

ANN, SVR, Deep Learning( for prediction, simulation, …. Exp: Horological Forecasting)

Need some software –> R or Python, …

.

.

2-1-Install R

Windows:

  1. Go to the official R website (https://www.r-project.org/).

  2. Click on “Download R for Windows”.

  3. Click on “base” and download the latest version of R.

  4. Run the installer and follow the instructions.

    (https://cran.r-project.org/bin/windows/base/)

Linux-Ubuntu ****************************************************************

  1. Open the terminal.

  2. Run the following commands:

  • sudo apt update

  • sudo apt install r-base

************************************************************************

2-1-2- Install R Packages

Once R (and optionally RStudio) is installed, you can install the required packages. To do this:

  1. Open R or RStudio.

  2. In the console, run the following command to install the desired package:

# install.packages("package_name")
# library(package_name)
2-2-Example1: Optimization problem

[Using the optimization algorithm to estimate the regression coefficients]

************E-1-1-Example in Excel************


# ---------E-1-2- Example in R ---------

library(GA)
library(openxlsx)

setwd("C:/Users/WRI/Documents/Workshop.Sep.2024")

mydata <- read.xlsx(xlsxFile ='SampleData.xlsx',sheet = "Sheet1" )
n1 <- nrow(mydata)
x1 <- mydata[,1] 
y1 <- mydata[,2]

MyFunction <- function(x) {
  a <- x[1]
  b <- x[2]
  y1.sim <- vector(mode = "numeric")
  deff <- vector(mode = "numeric")
  
  for (i in 1:n1) {
    y1.sim[i] <- a*x1[i]+b
    deff[i] <- (y1.sim[i]-y1[i])^2
  }
  
  objF <- sum(deff)
  write.csv(y1.sim,file = "y1.sim.csv")
  return(objF)
}


# minimize
optmodel <- ga(type = "real-valued", fitness =  function(x) -MyFunction(x),
               lower =c(-20,-50) , upper =c(20,50),
               popSize = 20, maxiter = 50,pcrossover = 0.8,pmutation = 0.15,parallel =FALSE)
               
plot(optmodel)

a <- optmodel@solution[1]
b <- optmodel@solution[2]
a <- round(a,digits = 2)
b <- round(b,digits = 2)

#------
y1.sim <- vector(mode = "numeric")
deff <- vector(mode = "numeric")

for (i in 1:n1) {
  y1.sim[i] <- a*x1[i]+b
  deff[i] <- (y1.sim[i]-y1[i])^2
}

xx <- 1:n1

plot(xx, y1, 
     type = "l",                      
     col = "blue",                    
     lwd = 2,                         
     main = paste0("Optimization Example  Y=",a,"X+",b), 
     xlab = "X Data",                
     ylab = "Y Data")                 

lines(xx, y1.sim, 
      col = "red",                    
      lwd = 2,                        
      lty = 2)                        

legend("topleft",                     
       legend = c("y1", "y1.sim"), 
       col = c("blue", "red"),        
       lty = c(1, 2),                 
       lwd = 2) 

https://dehban.shinyapps.io/workshopsep-1/

Example2: ANN problem:
rm(list = ls())
library(neuralnet)
library(openxlsx)
library(ggplot2)

setwd("C:/Users/WRI/Documents/Workshop.Sep.2024")
mydata2 <- read.xlsx(xlsxFile ='SampleDataANN.xlsx',sheet = "Sheet1" )

set.seed(123)

normalize <- function(x) {
  return ((x - min(x)) / (max(x) - min(x)))
}

mydata2_norm <- as.data.frame(lapply(mydata2, normalize))

set.seed(42)

# Split data into training and testing sets
n <- nrow(mydata2_norm)
train_index <- sample(1:n, 0.8*n)

train_data <- mydata2_norm[train_index, ]
test_data <- mydata2_norm[-train_index, ]

ANN_Model <- neuralnet(target ~ ., data = train_data, hidden = c(4, 2),stepmax = 10e6)



#------ Evaluation on Test data  -------
sim.test <- predict(object = ANN_Model,newdata =test_data )
real.sim.test <- sim.test * (max(mydata2$target) - min(mydata2$target)) + min(mydata2$target)
real.obs.test <- test_data$target * (max(mydata2$target) - min(mydata2$target)) + min(mydata2$target)

cor.test<- cor(real.sim.test,real.obs.test)


#------ Evaluation on Trian data  -------
sim.train <- predict(object = ANN_Model,newdata =train_data )
real.sim.train <- sim.train * (max(mydata2$target) - min(mydata2$target)) + min(mydata2$target)
real.obs.train <- train_data$target * (max(mydata2$target) - min(mydata2$target)) + min(mydata2$target)
cor.train<- cor(real.sim.train,real.obs.train)


#------ Evaluation on All data  -------
sim.all <- predict(object = ANN_Model,newdata =mydata2_norm )
real.sim.all <- sim.all * (max(mydata2$target) - min(mydata2$target)) + min(mydata2$target)
real.obs.all <- mydata2_norm$target * (max(mydata2$target) - min(mydata2$target)) + min(mydata2$target)
cor.all<- cor(real.sim.all,real.obs.all)


cat("Correalation_Test:", cor.test, "\n")
cat("Correalation_Test:", cor.train, "\n")
cat("Correalation_All:", cor.all, "\n")

# save result.....
#---------------- Plot Results-------
plot(ANN_Model, show.weights = FALSE)

xx <- 1:n
# png(filename = "neural_net_model.png",width = 800,height = 600)
plot(xx, real.obs.all, 
     type = "l",                      
     col = "black",                    
     lwd = 2,                         
     main = "ANN Problem", 
     xlab = "",                
     ylab = "Target")                 

lines(xx, real.sim.all, 
      col = "red",                    
      lwd = 2)                        

legend("topleft",                     
       legend = c("obs", "sim"), 
       col = c("black", "red"),        
       lwd = 2) 

# dev.off()
#----- Save model as .RData or another format -----

save(ANN_Model, file = "neural_net_model.RData")
load("neural_net_model.RData")

https://dehban.shinyapps.io/R_MLP_ANN/

.

2-4- Development of artificial intelligence models in R (Introduction of stream-flow forecasting project~ IDIO)

Deep Learning ~ LSTM Model

https://idio.wri.ac.ir/

https://idio.wri.ac.ir/

https://dehban.shinyapps.io/GIS1/