import numpy as np
patchs = [[1,0,0,0,1],[2,3,0.4,0.004,1],[3,5,0.6,0.02,1]]
dictpatch = {1:[0,0,0,1],2:[3,0.4,0.004,1],3:[5,0.6,0.02,1]}
Xcritical = 3
Xmax = 10
"""Scénario de base : détermination de la matrice de décision """
def Fitness (X,Xcritical,Xmax,patch_p,Fvectors):
"""Explication de la fonction"""
XFood = X - patch_p[3] + patch_p[0]
XFood = min(XFood,Xmax)
XFood = max(XFood,Xcritical)
XNoFood = X - patch_p[3]
XNoFood = max(XNoFood,Xcritical)
Term1 = patch_p[1] * [elt for elt in Fvectors if elt[0]==XFood][0][1]
Term2 = (1-patch_p[1]) * [elt for elt in Fvectors if elt[0]==XFood][0][1]
W = (1-patch_p[2]) * (Term1 + Term2)
return W
# Function to iterate over patches
def OVER_PATCHES (X, Fvectors, Xcritical, Xmax, Npatch, dictpatch):
"""Explication"""
RHS = []
for i in range(1,Npatch+1): # Cycle over patches
# Call Fitness function
RHS.append(Fitness(X, Xcritical, Xmax, dictpatch[i], Fvectors))
# Now find optimal patch Best row is in Best[1]
BestPatch = RHS[0]
Number = 0
for elt in RHS :
if elt > BestPatch :
BestFitness = elt
BestPatch = Number + 1
Number += 1
#Fvectors[X,0] = Number + 1
# Concatenate F(x,t) and the optimal patch number
Temp = np.array([[BestPatch, BestFitness]])
# Add Temp to bottom of F.vectors and rename to Temp
#Concatenation = np.concatenate((Fvectors,Temp))
return (Temp)
#Function to iterate over states of X
def OVER_STATES(Fvectors, Xcritical, Xmax, Npatch,dictpatch):
Store = np.array([[0,0] for i in range(1,Xmax + 1)])
for X in range(Xcritical + 1, Xmax):
# For given X call Over.Patches to determine F(x,t) and best patch
Temp = OVER_PATCHES(X, Fvectors, Xcritical, Xmax, Npatch, dictpatch)
# Extract components. Last row is F(x,t) and best patch
Store[X,] = Temp # Save F(x,t) and best patch
# Add Store values to end of F.vectors for pass back to main program
print(Store)
Temp = np.concatenate((Fvectors, Store),axis = 1) # Combined by columns
return(Temp) # Return F.vectors and Store
# MAIN PROGRAM
# Initialize parameters
Xmax = 10 # Maximum value of X
Xcritical = 3 # Value of X at which death occurs
Xmin = Xcritical + 1 # Smallest value of X allowed
#For each patch we have :
# type, benefit, Pbenefit, Pmortality, Cost
# benefit if food is discovered
# Pbenefit: probability of finding food
# Pmortality: probability of mortality
# Cost : cost of period
dictpatch = {1:[0,0,0,1],2:[3,0.4,0.004,1],3:[5,0.6,0.02,1]}
Npatch = 3 # Number of patches
RHS = [0 for i in range(0,Npatch)] # Pre-allocate Right Hand Side of equn
Horizon = 20 # Number of time step
# Set up matrix for fitnesses
# Column 1 is F(x, t). Column 2 is F(x,tþ1)
Fvectors = np.zeros((Xmax,2)) # Set all values to zero
Fvectors[Xmin:Xmax,2] = 1 # Set values > Xmin equal 1
# Create matrices for output
FxtT = np.zeros((Horizon,Xmax)) # F(x,t)
Best_Patch = np.zeros((Horizon,Xmax)) # Best patch number
Time = Horizon #Initialize Time
while Time>1:
Time = Time - 1 # Decrement Time by 1 unit
# Call OVER.STATES to get best values for this time step
Temp = OVER_STATES(Fvectors, Xcritical, Xmax, Npatch, dictpatch)
# Extract F.vectors
TempF = Temp[:,1]
# Update F1
for i in range(Xmin, Xmax):
Fvectors[i,2] <- TempF[i,1]
# Store results
Best_Patch[Time,:] = Temp[:,4]
FxtT[Time,:] = Temp[:,3]
# Output information. For display add states (¼wts) to last row of matrices
X = np.arange(1,Xmax)
Best_Patch[Horizon,:] = X
FxtT[Horizon, : ] = X
Best_Patch[: ,Xmin:Xmax] # Print Decision matrix
np.round(FxtT[:,Xmin:Xmax],3) # Print Fxt of Decision matrix: 3 sig places
*
######Transition density matrix P15-16
Xmax = 10
Xcritical = 3
Xmin = Xcritical + 1
patch_p[3] = 1
Time = 2
Npatch = 3
patch_p[2] = [0, 0.004, 0.02]
patch_p[1] = [1, 0.4, 0.6]
patch_p[0] = [0, 3, 5]
Trans_density = np.array([0, Xmax, Xmax])
#BestPatch = np.array([[1,1],[2,2],[3,3]])
for z in range (Xmin,Xmax):
print(BestPatch)
K = BestPatch[Time, z]
print(K)
x = min(z - patch_p[3] + dictpatch[K][0], Xmax)
print(x)
Trans_density[z, x] = (1-dictpatch[K][2]) * (dictpatch[K][1])
x = z - patch_p[3]
if x > Xcritical:
Trans_density[z,x] = (1- dictpatch[K][2]) * (1-dictpatch[K][1])
Trans_density [z, Xcritical] = dictpatch[K][2]
else:
Trans_density[z, Xcritical] = (dictpatch[K][2]) + ((1-dictpatch[K][2]) * (1 - dictpatch[K][1]))
end
######Résultat après 4 premières heures de cours
import numpy as np
#Paramètres de bases
#Définition des patchs : liste de liste ou dictionaire
#Pour chaque patch : type, benefit, Pbenefit, Pmortality, Cost
#numeropatch[0]= benefit
#[1]= Pbenefit
patchs = [[1,0,0,0,1],[2,3,0.4,0.004,1],[3,5,0.6,0.02,1]]
dictpatch = {1:[0,0,0,1],2:[3,0.4,0.004,1],3:[5,0.6,0.02,1]}
Xcritical = 3
Xmax = 10
"""Scénario de base : détermination de la matrice de décision """
def Fitness (X,Xcritical,Xmax,patch_p,Fvectors):
"""Explication de la fonction"""
XFood = X - patch_p[3] + patch_p[0]
XFood = min(XFood,Xmax)
XFood = max(XFood,Xcritical)
XNoFood = X - patch_p[3]
XNoFood = max(XNoFood,Xcritical)
Term1 = patch_p[1] * Fvectors[XFood-1][1]
Term2 = (1-patch_p[1]) * Fvectors[XNoFood-1][1]
W = (1-patch_p[2]) * (Term1 + Term2)
return W
# Function to iterate over patches
def OVER_PATCHES (X, Fvectors, Xcritical, Xmax, Npatch, dictpatch):
"""Explication"""
RHS = []
for i in range(1,Npatch+1): # Cycle over patches
# Call Fitness function
RHS.append(Fitness(X, Xcritical, Xmax, dictpatch[i], Fvectors))
# Now find optimal patch Best row is in Best[1]
BestPatch = RHS[0]
Number = 0
for elt in RHS :
if elt >= BestPatch :
BestFitness = elt
BestPatch = Number + 1
Number += 1
#Fvectors[X,0] = Number + 1
# Concatenate F(x,t) and the optimal patch number
Temp = np.array([[BestPatch, BestFitness]])
# Add Temp to bottom of F.vectors and rename to Temp
#Concatenation = np.concatenate((Fvectors,Temp))
return (Temp)
#Function to iterate over states of X
def OVER_STATES(Fvectors, Xcritical, Xmax, Npatch,dictpatch):
Store = np.array([[0,0] for i in range(1,Xmax + 1)])
for X in range(Xcritical + 1, Xmax):
# For given X call Over.Patches to determine F(x,t) and best patch
Temp = OVER_PATCHES(X, Fvectors, Xcritical, Xmax, Npatch, dictpatch)
# Extract components. Last row is F(x,t) and best patch
Store[X,] = Temp # Save F(x,t) and best patch
# Add Store values to end of F.vectors for pass back to main program
Temp = np.concatenate((Fvectors, Store),axis = 1) # Combined by columns
return(Temp) # Return F.vectors and Store
X = 4
patch_p = dictpatch[1]
Npatch = 3
# MAIN PROGRAM
# Initialize parameters
Xmax = 10 # Maximum value of X
Xcritical = 3 # Value of X at which death occurs
Xmin = Xcritical + 1 # Smallest value of X allowed
#For each patch we have :
# type, benefit, Pbenefit, Pmortality, Cost
# benefit if food is discovered
# Pbenefit: probability of finding food
# Pmortality: probability of mortality
# Cost : cost of period
dictpatch = {1:[0,0,0,1],2:[3,0.4,0.004,1],3:[5,0.6,0.02,1]}
Npatch = 3 # Number of patches
Horizon = 20 # Number of time step
# Set up matrix for fitnesses
# Column 1 is F(x, t). Column 2 is F(x,tþ1)
Fvectors = np.zeros((Xmax,2)) # Set all values to zero
Fvectors[Xmin:Xmax,1] = 1 # Set values > Xmin equal 1
# Create matrices for output
FxtT = np.zeros((Horizon,Xmax)) # F(x,t)
Best_Patch = np.zeros((Horizon,Xmax)) # Best patch number
Time = Horizon #Initialize Time
while Time>1:
Time = Time - 1 # Decrement Time by 1 unit
# Call OVER.STATES to get best values for this time step
Temp = OVER_STATES(Fvectors, Xcritical, Xmax, Npatch, dictpatch)
# Extract F.vectors
TempF = Temp[:,0:1]
# Update F1
for i in range(Xmin, Xmax):
Fvectors[i,1] = TempF[i,0]
# Store results
Best_Patch[Time,:] = np.transpose(Temp[:,3])
FxtT[Time,:] = Temp[:,2]
# Output information. For display add states (¼wts) to last row of matrices
X = np.arange(1,Xmax+1)
Best_Patch[Horizon - 1,:] = X
FxtT[Horizon - 1, : ] = X
Best_Patch[: ,Xmin:Xmax] # Print Decision matrix
np.round(FxtT[:,Xmin:Xmax],3) # Print Fxt of Decision matrix: 3 sig places
# ######Transition density matrix P15-16
# Xmax = 3
# Xcritical = 0
# Xmin = Xcritical + 1
# patch_p[3] = 1
# Time = 2
# Npatch = 3
# patch_p[2] = [0, 0.004, 0.02]
# patch_p[1] = [1, 0.4, 0.6]
# patch_p[0] = [0, 3, 5]
# Trans_density = np.array([0, Xmax, Xmax])
# BestPatch = np.array([[1,1],[2,2],[3,3]])
# for z in range (Xmin,Xmax):
# print(BestPatch)
# K = BestPatch[Time, z]
# print(K)
# x = min(z - patch_p[3] + dictpatch[K][0], Xmax)
# print(x)
# Trans_density[z, x] = (1-dictpatch[K][2]) * (dictpatch[K][1])
# x = z - patch_p[3]
# if x > Xcritical:
# Trans_density[z,x] = (1- dictpatch[K][2]) * (1-dictpatch[K][1])
# Trans_density [z, Xcritical] = dictpatch[K][2]
# else:
# Trans_density[z, Xcritical] = (dictpatch[K][2]) + ((1-dictpatch[K][2]) * (1 - dictpatch[K][1]))
#################### MODIFICATION DU 01/10/2021 ###################
import numpy as np
import matplotlib.pyplot as plt
import random
"""Scénario de base : détermination de la matrice de décision """
def Fitness (X,Xcritical,Xmax,patch_p,Fvectors):
"""Explication de la fonction"""
XFood = X - patch_p[3] + patch_p[0]
XFood = min(XFood,Xmax)
XFood = max(XFood,Xcritical)
XNoFood = X - patch_p[3]
XNoFood = max(XNoFood,Xcritical)
Term1 = patch_p[1] * Fvectors[XFood - 1,1]
Term2 = (1-patch_p[1]) * Fvectors[XNoFood - 1,1]
W = (1-patch_p[2]) * (Term1 + Term2)
return W
# Function to iterate over patches
def OVER_PATCHES (X, Fvectors, Xcritical, Xmax, Npatch, dictpatch):
"""Explication"""
RHS = []
for i in range(1,Npatch+1): # Cycle over patches
# Call Fitness function
RHS.append(Fitness(X, Xcritical, Xmax, dictpatch[i], Fvectors))
# Now find optimal patch Best row is in Best[1]
value=max(RHS) # valeur de la fitness du meilleur patch
Fvectors[X-1,0] = value
BestPatch=RHS.index(value) + 1 #renvoit le numéro du meilleur Patch
# Concatenate F(x,t) and the optimal patch number
Temp = [Fvectors[X-1,0], BestPatch]
Temp = np.append(Fvectors,[Temp],axis= 0)
return (Temp)
#Function to iterate over states of X
def OVER_STATES(Fvectors, Xcritical, Xmax, Npatch,dictpatch):
Store = np.zeros((Xmax,2))
for X in range(Xcritical + 1, Xmax + 1):
# For given X call Over.Patches to determine F(x,t) and best patch
Temp = OVER_PATCHES(X, Fvectors, Xcritical, Xmax, Npatch, dictpatch)
n = Temp.shape[0] - 1 # nombre de lignes total - 1
Fvectors =Temp[0:n,:]
Store[X-1,:] =Temp[n,:]
Temp = np.c_[Fvectors, Store]
# Add Store values to end of F.vectors for pass back to main program
# Combined by columns
return(Temp) # Return F.vectors and Store
# MAIN PROGRAM
# Initialize parameters
Xmax = 10 # Maximum value of X
Xcritical = 3 # Value of X at which death occurs
Xmin = Xcritical + 1 # Smallest value of X allowed
#For each patch we have :
## type: [benefit, Pbenefit, Pmortality, Cost]
# benefit if food is discovered
# Pbenefit: probability of finding food
# Pmortality: probability of mortality
# Cost : cost of period
dictpatch = {1:[0,1,0,1],2:[3,0.4,0.004,1],3:[5,0.6,0.02,1]}
Npatch = 3 # Number of patches
Horizon = 20 # Number of time step
# Set up matrix for fitnesses
# Column 1 is F(x, t). Column 2 is F(x,tþ1)
Fvectors = np.zeros((Xmax,2)) # Set all values to zero
Fvectors[Xmin-1:Xmax,1] = 1 # Set values > Xmin equal 1
# Create matrices for output
FxtT = np.zeros((Horizon,Xmax)) # F(x,t)
Best_Patch = np.zeros((Horizon,Xmax)) # Best patch number
Time = Horizon #Initialize Time
while Time>1:
Time = Time - 1 # Decrement Time by 1 unit
# Call OVER.STATES to get best values for this time step
Temp = OVER_STATES(Fvectors, Xcritical, Xmax, Npatch, dictpatch)
# Extract F.vectors
TempF = Temp[:,0:1]
# Update F1
for i in range(Xmin, Xmax+1):
Fvectors[i-1,1] = TempF[i-1,0]
# Store results
Best_Patch[Time-1,:] = Temp[:,3]
FxtT[Time-1,:] = Temp[:,2]
# Output information. For display add states (¼wts) to last row of matrices
X = np.arange(1,Xmax+1)
Best_Patch[Horizon - 1,:] = X
FxtT[Horizon - 1, : ] = X
Best_Patch[: ,Xmin-1:Xmax] # Print Decision matrix
np.round(FxtT[:,Xmin-1:Xmax],3) # Print Fxt of Decision matrix: 3 sig places
print(Best_Patch)
# Initialize parameters
random.seed(10) # Set random number seed
Xmax = 10 # Maximum value of X
Xcritical = 3 # Value of X at which death occurs
Xmin = Xcritical + 1 # Smallest value of X allowed
#For each patch we have :
## type:[benefit, Pbenefit, Pmortality, Cost ]
# benefit if food is discovered
# Pbenefit: probability of finding food
# Pmortality: probability of mortality
# Cost : cost of period
dictpatch = {1:[0,1,0,1],2:[3,0.4,0.004,1],3:[5,0.6,0.02,1]}
Npatch = 3 # Number of patches
Horizon = 15 # Number of time steps
Output = np.zeros((Horizon,10)) # Matrix to hold output
Time = np.arange(1,Horizon + 1) # Values for x axis in plot
for Replicate in range(1,11) :
X = 4 # Animal starts in state 4
for i in range(1,Horizon + 1) :
if X > Xcritical : # Iterate over time
Patch= Best_Patch[i-1,X-1] # Select patch
# Check if animal survives predation
# Generate random number
#if random.random() < dictpatch[Patch][3] :
#print("Dead from predator")
# Now find new weight
# Set multiplier to zero, which corresponds to no food found
Index = 0
if (random.random() < dictpatch[Patch][1]):
Index = 1 # food is discovered
X = X - dictpatch[Patch][3] + dictpatch[Patch][0] * Index
# If X greater than Xmax then X must be set to Xmax
X = min(X, Xmax)
# If X less than X then animal dies
#if (X < Xmin):
#print("Dead from starvation")
Output[i-1,Replicate-1] = Patch # Store data
#Représentation des graphiques
Axe_combinaison=[[0,0],[1,0],[2,0],[3,0],[4,0],[0,1],[1,1],[2,1],[3,1],[4,1]]
i=0 # compteur pour récupérer les colonnes de OUTPUT
fig, axs = plt.subplots(5, 2, figsize=(10, 10))
fig.suptitle('Simulations using the decision matrix with a starting initial state of X = 4.')
for elt in Axe_combinaison:
axs[elt[0],elt[1]].plot(Time,Output[:,i])
axs[elt[0],elt[1]].set_ylabel("Selected Patch")
axs[elt[0],elt[1]].set_xlabel("Time")
i=i+1 # On va ensuite prendre la prochaine colonne de OUTPUT
#####Transition density matrix P15-16
Xmax = 10
Xcritical = 3
Xmin = Xcritical + 1
Time = 2
Npatch = 3
#For each patch we have :
## type:[benefit, Pbenefit, Pmortality, Cost ]
# benefit if food is discovered
# Pbenefit: probability of finding food
# Pmortality: probability of mortality
# Cost : cost of period
dictpatch = {1:[0,1,0,1],2:[3,0.4,0.004,1],3:[5,0.6,0.02,1]}
Trans_density = np.zeros((Xmax, Xmax))
for z in range (Xmin,Xmax + 1):
K = Best_Patch[Time-1, z-1]
print(K)
x = min(z - dictpatch[K][3] + dictpatch[K][0], Xmax)
print(x)
#Assign probability
Trans_density[z-1, x-1] = (1-dictpatch[K][2]) * (dictpatch[K][1])
# Food not found
x = z - dictpatch[K][3]
if x > Xcritical:
# Animal survives
Trans_density[z-1,x-1] = (1- dictpatch[K][2]) * (1-dictpatch[K][1])
# Animal does not survive
Trans_density [z-1, Xcritical-1] = dictpatch[K][2]
else:
Trans_density[z-1, Xcritical-1] = (dictpatch[K][2]) + ((1-dictpatch[K][2]) * (1 - dictpatch[K][1]))
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 2 13:29:16 2021
@author: Hélène
"""
import numpy as np
import matplotlib.pyplot as plt
import random
"""Scénario de base : détermination de la matrice de décision """
"contexte"
def Fitness (X,Xcritical,Xmax,patch_p,Fvectors):
"""Fitness permet de renvoyer fitness du patch
Arguments:
état actuel, état critique, état maximun, paramétres du patch,
matrice des probabilités
Renvoi:
scalaire égal au fitness du patch"""
XFood = X - patch_p[3] + patch_p[0]
XFood = min(XFood,Xmax) #Si XFood est supérieur à Xmax, il prend la valeur de Xmax
XFood = max(XFood,Xcritical) # De même, X ne doit pas être inférieur à Xcritique
XNoFood = X - patch_p[3]
XNoFood = max(XNoFood,Xcritical)
Term1 = patch_p[1] * Fvectors[XFood - 1,1] #Pbenefice * probabilité d'obtenir de la nourriture
Term2 = (1-patch_p[1]) * Fvectors[XNoFood - 1,1] #(1-Pbenefit) * proba NoFood
W = (1-patch_p[2]) * (Term1 + Term2) #(1-Pmortality) * (Term1 + Term2)
return W
def OVER_PATCHES (X, Fvectors, Xcritical, Xmax, Npatch, dictpatch):
"""OVER_Patches return the fitness of all patchs and select the optimal patch
Attributes:
scalar : actual state
matrix : Fvectors
sacalr : critical state, maximum state, number of patchs
dictionnary : patchs parameters
Return:
matrix of 2 columns 'temp' """
RHS = []
for i in range(1,Npatch+1): # Cycle over patches
# Call Fitness function
RHS.append(Fitness(X, Xcritical, Xmax, dictpatch[i], Fvectors))
# Now find optimal patch Best row is in Best[1]
value=max(RHS) # Fitness value of the optimal patch
Fvectors[X-1,0] = value
BestPatch=RHS.index(value) + 1 # return the number of the optimal patch
# Concatenate F(x,t) and the optimal patch number
Temp = [Fvectors[X-1,0], BestPatch]
Temp = np.append(Fvectors,[Temp],axis= 0)
return (Temp)
def OVER_STATES(Fvectors, Xcritical, Xmax, Npatch, dictpatch):
"""OVER_STATES return the optimal decision for all states and the associated probability
Attributes:
matrix : Fvectors
saclar : critical state, maximum state, number of patchs
dictionnary : patchs parameters
Return:
matrix of 4 colums : 'temp' """
Store = np.zeros((Xmax,2))
for X in range(Xcritical + 1, Xmax + 1):
# For given X call Over.Patches to determine F(x,t) and best patch
Temp = OVER_PATCHES(X, Fvectors, Xcritical, Xmax, Npatch, dictpatch)
n = Temp.shape[0] - 1 # Number total of lignes - 1
Fvectors =Temp[0:n,:]
Store[X-1,:] =Temp[n,:]
Temp = np.c_[Fvectors, Store]
# Add Store values to end of F.vectors for pass back to main program
# Combined by columns
return(Temp) # Return actual state, probability of each optimal patch, futur state and its associated probability
# MAIN PROGRAM
random.seed(10) # Set random number seed
# Initialize parameters
Xmax = 10 # Maximum value of X
Xcritical = 3 # Value of X at which death occurs
Xmin = Xcritical + 1 # Smallest value of X allowed
random.seed(10) # Set random number seed
#For each patch we have :
## type: [benefit, Pbenefit, Pmortality, Cost]
# benefit if food is discovered
# Pbenefit: probability of finding food
# Pmortality: probability of mortality
# Cost : cost of period
dictpatch = {1:[0,1,0,1],2:[3,0.4,0.004,1],3:[5,0.6,0.02,1]}
Npatch = 3 # Number of patches
Horizon = 20 # Number of time step
# Set up matrix for fitnesses
# Column 1 is F(x, t). Column 2 is F(x,tþ1)
Fvectors = np.zeros((Xmax,2)) # Set all values to zero
Fvectors[Xmin-1:Xmax,1] = [i for i in range(Xmin,Xmax +1)]
# Create matrices for output
FxtT = np.zeros((Horizon,Xmax)) # F(x,t)
Best_Patch = np.zeros((Horizon,Xmax)) # Best patch number
Time = Horizon #Initialize Time
while Time>1:
Time = Time - 1 # Decrement Time by 1 unit
# Call OVER.STATES to get best values for this time step
Temp = OVER_STATES(Fvectors, Xcritical, Xmax, Npatch, dictpatch)
# Extract F.vectors
TempF = Temp[:,0:1]
# Update F1
for i in range(Xmin, Xmax+1):
Fvectors[i-1,1] = TempF[i-1,0]
# Store results
Best_Patch[Time-1,:] = Temp[:,3]
FxtT[Time-1,:] = Temp[:,2]
# Output information. For display add states (¼wts) to last row of matrices
X = np.arange(1,Xmax+1)
Best_Patch[Horizon - 1,:] = X
FxtT[Horizon - 1, : ] = X
Best_Patch[: ,Xmin-1:Xmax]
np.round(FxtT[:,Xmin-1:Xmax],3)
print(Best_Patch)
Output = np.zeros((Horizon,10)) # Matrix to hold output
Time = np.arange(1,Horizon + 1) # Values for x axis in plot
print(Best_Patch)
print(Best_Patch[19,8])
for Replicate in range(1,11) :
X = 4 # Animal starts in state 4
for i in range(1,Horizon + 1) :
if X > Xcritical : # Iterate over time
Patch = Best_Patch[i-1,X-1] # Select patch
# print(Best_Patch)
# print(i-1)
# print(X-1)
# print(Best_Patch[i-1,X-1])
# print(Best_Patch[19,8])
# print(Patch)
# Check if animal survives predation
# Generate random number
#if random.random() < dictpatch[Patch][3] :
#print("Dead from predator")
# Now find new weight
# Set multiplier to zero, which corresponds to no food found
Index = 0
print(Patch)
print(dictpatch[Patch][1])
if random.random() < dictpatch[Patch][1]:
Index = 1 # food is discovered
X = X - dictpatch[Patch][3] + dictpatch[Patch][0] * Index
# If X greater than Xmax then X must be set to Xmax
X = min(X, Xmax)
# If X less than X then animal dies
#if (X < Xmin):
#print("Dead from starvation")
Output[i-1,Replicate-1] = Patch # Store data
#Représentation des graphiques
Axe_combinaison=[[0,0],[1,0],[2,0],[3,0],[4,0],[0,1],[1,1],[2,1],[3,1],[4,1]]
i=0 # counter
fig, axs = plt.subplots(5, 2, figsize=(10, 10))
fig.suptitle('Simulations using the decision matrix with a starting initial state of X = 4.')
for elt in Axe_combinaison:
axs[elt[0],elt[1]].plot(Time,Output[:,i])
axs[elt[0],elt[1]].set_ylabel("Selected Patch")
axs[elt[0],elt[1]].set_xlabel("Time")
i=i+1 # The second column of OUTPUT will be selectioned
"""---------------------Transition density matrix---------------------------"""
Trans_density = np.zeros((Xmax, Xmax))
for z in range (Xmin,Xmax + 1):
K = Best_Patch[Time-1, z-1]
print(K)
x = min(z - dictpatch[K][3] + dictpatch[K][0], Xmax)
print(x)
#Assign probability
Trans_density[z-1, x-1] = (1-dictpatch[K][2]) * (dictpatch[K][1])
# Food not found
x = z - dictpatch[K][3]
if x > Xcritical:
# Animal survives
Trans_density[z-1,x-1] = (1- dictpatch[K][2]) * (1-dictpatch[K][1])
# Animal does not survive
Trans_density [z-1, Xcritical-1] = dictpatch[K][2]
else:
Trans_density[z-1, Xcritical-1] = (dictpatch[K][2]) + \
((1-dictpatch[K][2]) * (1 - dictpatch[K][1]))