Value of randomly chosen weight in the chromosome is being change into new value with the same sign
Selection is the stage of a genetic algorithm in which individual genomes are chosen from a population for later breeding (using the crossover operator).
Roulette wheel selection
Selecting with weight probability, depending on the fitness of the individual gene Fitness values [1, 2, 3, 4], then the sum is (1 + 2 + 3 + 4 = 10). Therefore, you would want the probabilities or chances to be [1/10, 2/10, 3/10, 4/10] or [0.1, 0.2, 0.3, 0.4] The chosen one is taken for the crossover
Tournament selection
Winner of the tournament is being selected for the crossover two variants of the selection: with and without replacement
Fitness is calculated as difference between concept values on each step of the simulation
For more information about the algorithms, please check out LEARNING AND AGGREGATION OF FUZZY COGNITIVE MAPS – AN EVOLUTIONARY APPROACH by Wojciech Stach
from fcmpy.ml.genetic.rcga import rcga,reshapeW,simulateFCM
import numpy as np
We used initial FCM presented in the article from arcticle
Fuzzy cognitive map based approach for determining the risk of ischemic stroke Mahsa Khodadadi 1, Heidarali Shayanfar 2, Keivan Maghooli 3, Amir Hooshang Mazinan 1
https://pubmed.ncbi.nlm.nih.gov/31778126/
Do not worry about the empty plot, if you would run it in another IDE, such as VSCode, plot would interactivly display current fitness function
testc = 7
#
# test 2
nofsteps = 2
# go to the paper and create an initial A0 (shape (1,n)
# and weight matrix n x n
A0 = np.asarray([[0.47, 0.51, 0.13, 0, 1, 0.37, 0.1]])
W_init = np.asarray([[0,0,0.20,0.3,0,0,0.45],
[0,0,0,0.40,0,0,0.35],
[0,0,0,0.30,0,0,0.30],
[0,0,0,0,0,0,0.40],
[0.30,0,0.15,0.35,0,0,0.25],
[0.20,0,0.25,0.30,0,0,0.20],
[0,0,0,0,0,0,0]])
# we reshape weight matrix in such a way that 0 - filled - diagonal is removed and
W_init = reshapeW(W_init,'in')
# we prepare the placeholder for historical data
historicaldata = np.zeros((A0.shape[0],A0.shape[1]))
# we prepare the historical data for learning process
for concepts,i in zip(A0,range(A0.shape[0])):
historicaldata[i] = simulateFCM(concepts, W_init, nofsteps)
# run the learning algorithm
# %%capture
GA = rcga(A0,historicaldata=historicaldata,numberofsteps=nofsteps,maxfitness=0.97)
W_final = GA.run()
We are at 100/100000 max fitness function so far is 0.9416627800656449 We are at 200/100000 max fitness function so far is 0.9656261906515792
W = reshapeW(W_final[-1],'out')
W
array([[ 0. , 0.2392139 , 0.0186609 , -0.22615999, 0.14688781, 0.40326178, 0.01711518], [ 0.43515269, 0. , -0.51941326, 0.17227596, -0.78192652, -0.26074009, 0.202243 ], [ 0.99645221, -0.74905677, 0. , 0.59727595, 0.70527696, -0.7515263 , 0.60774873], [ 0.61203435, 0.73934232, -0.93735818, 0. , -0.31688036, -0.86817381, -0.25375124], [ 0.3541491 , -0.05118418, 0.53135025, 0.97417019, 0. , 0.04842812, 0.8999771 ], [-0.70795986, 0.16143704, -0.0915654 , -0.49343288, 0.81211933, 0. , -0.90462744], [-0.68661796, -0.23099666, 0.93660758, -0.06985412, -0.63285064, -0.10909749, 0. ]])