How to create Fuzzy Cognitive Map from the Adjacency Matrix

9 views (last 30 days)
Hi everyone,
I have developed an unsupervised neural model and declared it has Fuzzy Cognitive Map. It could forecast the corrupted value to the nearest value after trained using Differential Hebbian Learning Rule. I have a doubt can you show me which part of the source code shows the Fuzzy?
Thank you.

Answers (1)

Sam Chak
Sam Chak on 18 Apr 2025
At the time your question was posted, it was challenging for @Arkadiy Turevskiy and others to assist you because the function to create the Fuzzy Cognitive Map (FCMap) had not yet been included in MATLAB. Your original code, 'coding.m,' successfully created the adjacency matrix W using Differential Hebbian Learning. However, it is incorrect to display the FCMap as a surface diagram as it is more accurately described as a conceptual network that illustrates causal relationships between concepts.
With the introduction of the digraph() function in R2015b release, the FCMap can now be represented as a directed graph-based structure, where the nodes represent concepts and the edges (arrows) between nodes represent causal relationships, with weights that describe the strength and type of influence. The fuzzy weight values should be between and 1 to represent degrees of causality. Thus, it is necessary to rescale the weights in the adjacency matrix, W.
% type coding.m
clc
clear all
'====================Training The Dataset============================='
ans = '====================Training The Dataset============================='
% DATASET STRUCTURE
DataSet = [ % this is a raw dataset of 5 variables and 48 samples
% var1: temp in var2:humidity in var3:cfm of ceiling fan var4:temp out var5:humidity out ...
0.2900 0.5900 0.1725 0.3500 0.5500 %0.54
0.2900 0.5900 0.2535 0.3500 0.5500 %0.54
0.2900 0.5800 0.3600 0.3500 0.5500 %0.55
0.3000 0.5100 0.1725 0.3600 0.5700 %0.47
0.3000 0.4900 0.2535 0.3600 0.5700 %0.47
0.3000 0.4900 0.3600 0.3600 0.5700 %0.48
0.3600 0.4200 0.1725 0.3400 0.5200 %0.59
0.3600 0.4200 0.2535 0.3400 0.5200 %0.59
0.3400 0.4300 0.3600 0.3400 0.5200 %0.57
0.3600 0.4400 0.1725 0.3500 0.5400 %0.58
0.3600 0.4400 0.2535 0.3500 0.5400 %0.59
0.3500 0.4400 0.3600 0.3500 0.5400 %0.58
0.3500 0.4200 0.1725 0.3500 0.5700 %0.54
0.3500 0.4400 0.2535 0.3500 0.5700 %0.57
0.3300 0.4500 0.3600 0.3500 0.5700 %0.54
0.3200 0.4800 0.1725 0.2600 0.7100 %0.69
0.3200 0.4800 0.2535 0.2600 0.7100 %0.71
0.3000 0.4900 0.3600 0.2600 0.7100 %0.67
0.3000 0.5700 0.1725 0.2500 0.7800
0.3000 0.5700 0.2535 0.2500 0.7800
0.3000 0.7900 0.3600 0.2500 0.7800
0.3100 0.5900 0.1725 0.2200 0.8400
0.3100 0.5900 0.2535 0.2200 0.8400
0.3000 0.5900 0.3600 0.2200 0.8400
0.2900 0.7200 0.1725 0.2900 0.7500
0.2900 0.7200 0.2535 0.2900 0.7500
0.2900 0.7200 0.3600 0.2900 0.7500
0.2900 0.6800 0.1725 0.2900 0.7700
0.3000 0.6900 0.2535 0.2900 0.7700 %s
0.3100 0.6500 0.3600 0.2900 0.7900
0.3200 0.5200 0.1725 0.3000 0.6500
0.3200 0.4200 0.2535 0.3000 0.6600
0.3300 0.4300 0.3600 0.3000 0.6600
0.3000 0.6000 0.1725 0.3000 0.6000
0.2900 0.7600 0.2535 0.3100 0.6000
0.2900 0.7600 0.3600 0.3100 0.6400
0.2900 0.7600 0.1725 0.3000 0.6700
0.2900 0.7500 0.2535 0.3000 0.6700
0.2900 0.7500 0.3600 0.2900 0.6900
0.2900 0.7500 0.1725 0.2800 0.7100
0.2900 0.7000 0.2535 0.2800 0.7500
0.2900 0.7000 0.3600 0.2800 0.7500
0.2800 0.6700 0.1725 0.2700 0.7800
0.2800 0.6700 0.2535 0.2700 0.7800
0.2900 0.7400 0.3600 0.2700 0.7800
0.2700 0.6700 0.1725 0.2500 0.8400
0.2700 0.7500 0.2535 0.2500 0.9400
0.2600 0.7500 0.3600 0.2400 0.9700];
% surf (DataSet)
% IMPORTANT: the data must be within a same range. for example, if you
% notice the middle column I divided by 100 so it became almost same range
% as the others.
% also to avoid overflow error, I devided all figures by 100 so they become
% within the standard range (0,1). I used matlab command>> DataSet=DataSet/100
'-------------------------TRAINING INITIALIZATION------------------------'
ans = '-------------------------TRAINING INITIALIZATION------------------------'
Rec = size(DataSet,1); % returns Rec=48
Var = size(DataSet,2); % returns Var=5
alpha = 0.1; % alpha: learning rate
W = repmat(0,Var,Var); % W is adjacency weights matrix
% TRAINING
start = clock; % read system time
'------------------Differential Hebbian Learning Rule--------------------'
ans = '------------------Differential Hebbian Learning Rule--------------------'
for Epoch = 1:10000 % for 10000 times we repeat training for all samples
for sample = 1:Rec % starting loop for learning all samples
X = DataSet(sample,:); % pick a sample
Y = X*W; % Y= DataSet(sample,:) * Adjacency Weights Matrix
for j = 1:Var
for i = 1:Var
if i ~= j % i is not equal to j
delta = alpha*(X(j) - Y(j))*X(i); % δ = α * (X(j) - Y(j)) * X(i)
W(i,j) = W(i,j) + delta; % W(i,j) = W(i,j)+ δ
end
end
end
end
end
stop = clock; % read system time (STOP time)
Run_Time= 60*(stop(5) - start(5)) + stop(6) - start(6) % computational cost
Run_Time = 0.1492
surf(W) % W is the knowledge base (adjacency model)
%% ----- Extra code by Sam Chak ----- Start here -----
% Define concept labels
concepts= {'Temperature In', 'Humidity In', 'CFM of Ceiling Fan', 'Temperature Out', 'Humidity Out'};
% Rescale the adjacency matrix W with fuzzy weights
disp(W)
0 -1.6497 -0.0205 1.0295 2.4296 -0.2000 0 0.0245 0.2397 0.8096 0.0096 0.0620 0 0.0330 0.1122 0.8503 1.5452 0.4742 0 -1.8939 0.2394 0.9244 0.1668 -0.2487 0
maxW = max(W(:)) % max value in W
maxW = 2.4296
minW = min(W(:)) % min value in W
minW = -1.8939
if abs(maxW) >= abs(minW)
bound = abs(maxW);
else
bound = abs(minW);
end
lowerB = -1; % lower bound for scaling purposes
upperB = 1; % upper bound for scaling purposes
FCMap = rescale(W, lowerB, upperB, "InputMin", -bound, "InputMax", bound)
FCMap = 5×5
0 -0.6790 -0.0085 0.4237 1.0000 -0.0823 0 0.0101 0.0987 0.3332 0.0040 0.0255 0 0.0136 0.0462 0.3500 0.6360 0.1952 0 -0.7795 0.0985 0.3805 0.0687 -0.1023 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Create directed graph
G = digraph(FCMap, concepts);
% Plot the FCM
figure;
plot(G, 'Layout', 'force', 'EdgeLabel', G.Edges.Weight, 'LineWidth', 1.5, 'ArrowSize', 12);
title('Fuzzy Cognitive Map - HVAC System Model');
%% ----- Extra code by Sam Chak ----- End here -----
Memory = DataSet*W;
MAPE = 100*sum(abs((Memory - DataSet)./DataSet))/Rec % MAPE error in percentage
MAPE = 1×5
6.2541 11.1254 27.6989 6.9268 7.9816
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
%--------------------------------------------------------------------------
'--------------------Testing for column 5---------------------'
ans = '--------------------Testing for column 5---------------------'
% include the sample that want to test:
test = [0.3000 0.4900 0.3600 0.2600 0.7100]
test = 1×5
0.3000 0.4900 0.3600 0.2600 0.7100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% assume the fifth column is corrupted or missing:
corrupted_test = [0.3000 0.4900 0.3600 0.2600 0.00]
corrupted_test = 1×5
0.3000 0.4900 0.3600 0.2600 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Here, the coding wiil try to fix it back:
test_fixed = corrupted_test*W;
fifth_column = test_fixed(5)
fifth_column = 0.6736
% now compare against the original values
MAPE = abs(test(5) - test_fixed(5))/test(5) % Mean Absolute Percentage Error
MAPE = 0.0513

Categories

Find more on Fuzzy Logic Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!