Voici le dernier script obtenu:
# Nettoyage initial
rm(list = ls())
graphics.off()
# Chargement des packages
if (!require("fields")) install.packages("fields")
library(fields)
### PARAMÈTRES ###
sizeenvironment <- 50 # Taille de la grille
r <- 0.25 # Taux de croissance
K <- 500 # Capacité porteuse
dt <- 0.1 # Pas de temps
dureesimulation <- 50 # Durée totale
tauxdispersion <- 0.2 # Probabilité de dispersion
### INITIALISATION ###
create_matrix <- function(size, val) {
m <- matrix(val, nrow = size + 2, ncol = size + 2)
m[c(1, size + 2), ] <- m[, c(1, size + 2)] <- 0 # Bordures à 0
return(m)
}
# Populations - Densité visible dès le départ
pop1 <- create_matrix(sizeenvironment, K * 0.6) # B.subtilis (60% de remplissage)
pop2 <- create_matrix(sizeenvironment, 0) # E.coli
pop2[(sizeenvironment/2-1)

sizeenvironment/2+1),
(sizeenvironment/2-1)

sizeenvironment/2+1)] <- K/4 # Tache centrale 3x3
### COULEURS AMÉLIORÉES ###
setup_display <- function() {
par(mar = c(3, 3, 3, 5))
list(
colorRampPalette(c("#FFCCCC", "#FF0000", "#990000"))(100), # B.sub (rouge)
colorRampPalette(c("#CCE5FF", "#0066FF", "#003399"))(100), # E.coli (bleu)
rgb(0.2, 0.7, 1, alpha = seq(0.1, 0.8, length.out = 50)) # Inhibiteur
)
}
### FONCTIONS ###
safe_growth <- function(Ni, Nj, r, K, dt, aji) {
Ni <- max(0, Ni, na.rm = TRUE)
Nj <- max(0, Nj, na.rm = TRUE)
growth <- Ni * r * dt * (1 - (Ni + aji * Nj)/K)
return(max(0, Ni + growth))
}
### BOUCLE PRINCIPALE ###
colors <- setup_display()
for (t in seq(0, dureesimulation, dt)) {
# Copie des populations
old_pop1 <- pop1
old_pop2 <- pop2
# Croissance
for (i in 2

sizeenvironment + 1)) {
for (j in 2

sizeenvironment + 1)) {
pop1[i,j] <- rpois(1, lambda = safe_growth(old_pop1[i,j], old_pop2[i,j], r, K, dt, 1.5))
pop2[i,j] <- rpois(1, lambda = safe_growth(old_pop2[i,j], old_pop1[i,j], r, K, dt, 0.8))
}
}
# Dispersion simplifiée
disperse <- function(pop) {
new_pop <- pop
for (i in 2

sizeenvironment + 1)) {
for (j in 2

sizeenvironment + 1)) {
if (pop[i,j] > 0) {
n_disp <- rbinom(1, size = min(pop[i,j], 20), prob = tauxdispersion * dt)
if (n_disp > 0) {
for (k in 1:n_disp) {
di <- sample(-1:1, 1)
dj <- sample(-1:1, 1)
new_pop[i + di, j + dj] <- new_pop[i + di, j + dj] + 1
}
new_pop[i,j] <- new_pop[i,j] - n_disp
}
}
}
}
return(new_pop)
}
pop1 <- disperse(pop1)
pop2 <- disperse(pop2)
# Affichage (tous les 2 pas de temps)
if (t %% 2 == 0) {
# Préparation des données
display_pop1 <- sqrt(pop1[2

sizeenvironment+1), 2

sizeenvironment+1)] / K) # Normalisation + sqrt
display_pop2 <- sqrt(pop2[2

sizeenvironment+1), 2

sizeenvironment+1)] / K)
# Plot principal
image(display_pop1, col = colors[[1]], zlim = c(0, 1),
main = paste("Simulation - Temps =", round(t, 1)),
xlab = "", ylab = "", axes = FALSE)
image(display_pop2, col = colors[[2]], add = TRUE)
# Légende
legend("topright",
legend = c("B.subtilis", "E.coli"),
fill = c("#FF0000", "#0066FF"),
bg = "white", cex = 0.9)
# Échelle de couleur
image.plot(zlim = c(0, 1), legend.only = TRUE,
col = colors[[1]],
smallplot = c(0.88, 0.9, 0.3, 0.7),
legend.args = list(text = "Densité (√)", side = 4, cex = 0.8))
Sys.sleep(0.1) # Pause pour visualisation
}
}