design controller for artificial pancreas using fuzzy logic -凯发k8网页登录
this example shows how to design and optimize a fuzzy inference system (fis) tree to control an artificial pancreas. the artificial pancreas regulates the blood glucose level of an individual with type 1 diabetes using subcutaneous infusion of insulin.
a fis tree is a distributed, hierarchical representation of a monolithic fis with multiple fiss, each with a smaller rule base. hence, a fis tree provides easier understanding of the inference process and allows faster performance optimization with a smaller number of tunable parameters as compared to a monolithic fis.
video walkthrough
for a visual walkthough of the example, watch the video.
background
type 1 diabetes is a widespread health problem that occurs when the pancreas fails to produce enough insulin to regulate blood glucose levels. an uncontrolled high blood glucose level (hyperglycemia) can cause significant damage to the human body. therefore, an artificial pancreas system that continuously monitors and regulates blood glucose level with appropriate subcutaneous insulin infusion is a major goal in healthcare device development.
the following figure, which is adapted from [1], shows the components of an artificial pancreas system.
the continuous glucose monitoring (cgm) system periodically measures the blood glucose level of the diabetic patient and passes this information to a controller. the controller drives an insulin pump, which injects an optimal insulin dosage to the patient, thus regulating the blood glucose level. the controller provides the following two types of insulin dosage.
basal dosage — long-term small amount of insulin for the fasting period of a diabetic patient
bolus dosage — short-term large amount of insulin necessary for absorption of a major meal
therefore, the task for the controller is to generate corrective insulin doses for the following cases.
hyperglycemia — when the blood sugar level is high, the controller provides a high insulin dose in bolus mode. generally, this dose is in the range of 125 to 200 mg/dl, which can vary depending on the fasting and meal conditions for the patient.
hypoglycemia — when the blood glucose level is low, generally less than 50–70 mg/dl, the controller stops providing insulin.
normal condition — in the normal condition, the blood glucose level is generally in the range of 80 to 100 mg/dl and the controller provides a low insulin dosage in basal mode.
artificial pancreas model
the artificialpancreaswithfistreecontrol
simulink® model implements an artificial pancreas system.
model = 'artificialpancreaswithfistreecontrol';
load_system(model)
this model contains the following subsystems.
diabetic patient — models the kinetics of insulin and its effect on glucose in the human body for type-1 diabetes as described in [2] and [3].
meal — generates glucose absorption from meals. for this example, the meals are scheduled for the 1st, 5th, and 12th hours of the day.
glucose monitoring system — provides noise-free samples of blood glucose levels every 5 minutes using a perfect transducer.
controller — generate corrective insulin doses using a hierarchical fis tree.
insulin pump — infuses the exact amount of insulin recommended by the fis controller using an ideal pump model.
to view how the blood glucose level of a diabetic patient changes when the body absorbs glucose without corrective insulin infusion, simulate the model with a constant zero control action. to do so, open the control loop by setting closeloop
to 0
.
closeloop = 0; openloopoutput = sim(model); plotabsorbedandbloodglucose(openloopoutput)
without corrective insulin infusion, the patient blood glucose level increases and remains in a hyperglycemic state.
close the control loop for tuning and simulation.
closeloop = 1;
create fis tree controller structure
the controller has the following inputs, as described in [4].
blood glucose level (mg/dl)
rate of change of blood glucose level (mg/dl/min)
acceleration rate of blood glucose level (mg/dl/min/min).
the output of the controller is an optimal insulin infusion dosage that maintains the blood glucose level of a diabetic patient at a normal level.
to produce an optimal insulin dosage based on the observed inputs, the fuzzy controller described in [4] uses expert knowledge to construct a single fis with 75 rules. however, creating a large rule base using expert knowledge is a complicated process due to the manual construction of each fuzzy rule for all combinations of input membership functions (mfs).
alternatively, using a fis tree produces a system with multiple fiss, each with a smaller rule base. the hierarchical structure of the fis tree and the smaller rule bases allow for a more intuitive understanding of the inference process.
this example uses an incremental design approach to combine the controller inputs using two mamdani fis objects in an incremental tree structure. for more information on fuzzy tree structures, see fuzzy trees.
the blood glucose level and its rate of change both contribute more to the control actions compared to the acceleration rate, which is often small and can create noise in the output. therefore, in the first level of the fis tree, you precalculate the insulin infusion rate precalculated_dose
by combining the effects of the blood glucose level bg_level
and its rate of change bg_rate
. the acceleration rate bg_accel
is included in the second layer of the fis tree.
create the fis (fis1
) for the first level of the tree structure. the inputs of fis1
each use three uniformly distributed triangular mfs. the output of fis1
uses five such mfs, named as follows:
for the input
bg_level
,l
,m
, andh
for low, medium, and high levels, respectivelyfor the input
bg_rate
,n
,z
, andp
for negative, zero, and positive rates, respectivelyfor the output
precalculated_dose
,l
,m
, andh
, plusvl
for very low andvh
for very high dosages
% specify the maximum dose level. maxdose = 2; % define membership function names for the input variables. mfnames1 = ["l","m","h"]; % low, medium, high mfnames2 = ["n","z","p"]; % negative, zero, positive % create first fis. fis1 = mamfis('name','fis1','numinputs',2,'numoutputs',1, ... 'numinputmfs',3,'numoutputmfs',5); % configure input and output variables. fis1 = updateinput(fis1,1,'bg_level',[80 120],mfnames1); fis1 = updateinput(fis1,2,'bg_rate',[-0.5 0.5],mfnames2); fis1 = updateoutput(fis1,1,'precalculated_dose',[0 maxdose]); figure plotfis(fis1)
create the fis (fis2
) for the second level of the tree. using fis2
, you generate the final insulin dosage by combining the precalculated dose from the first layer with the effect of the blood glucose acceleration rate. in this case, the inputs and the output also use three and five uniformly distributed triangular mfs, respectively.
% create second fis. fis2 = mamfis('name','fis2','numinputs',2,'numoutputs',1, ... 'numinputmfs',3,'numoutputmfs',5); % configure input and output variables. fis2 = updateinput(fis2,1,'precalculated_dose',[0 maxdose],mfnames1); fis2 = updateinput(fis2,2,'bg_accel',[-0.005 0.005],mfnames2); fis2 = updateoutput(fis2,1,'insulin_dose',[0 maxdose]); figure plotfis(fis2)
combine fis1
and fis2
into a fis tree structure.
connection = [fis1.name "/" fis1.outputs(1).name ... fis2.name "/" fis2.inputs(1).name]; fistinit = fistree([fis1 fis2],connection); figure plotfis(fistinit)
the initial fuzzy systems are constructed with default fuzzy rules that are not tuned to produce optimal insulin dosages.
tune controller rules
once you have a fis tree structure, you can optimize the controller behavior by tuning the rules and mf parameters of the component fis objects. to do so, you can use the tunefis
function.
in general, uniformly distributed mfs provide a meaningful initial condition for tuning a fuzzy system. therefore, as an initial tuning step, you can learn the rules of the fis objects while keeping their default mf parameters.
to learn the rules, first get the tunable settings from the fuzzy systems.
[in,out,rule] = gettunablesettings(fistinit);
next, update the rule settings to optimize only the rule consequents. by doing so, you keep the existing rule antecedents, which already include all possible input mf combinations for their corresponding fis inputs.
for rid = 1:numel(rule) rule(rid).antecedent.free = false; end
create an option set for the tuning process.
options = tunefisoptions;
use the default genetic algorithm tuning method for learning the rules. set the maximum number of generations to 3 and use a population size of 100.
options.methodoptions.maxgenerations = 3; options.methodoptions.populationsize = 100;
next, create a cost function to evaluate each candidate rule base. at the end of the optimization process, the rule base with the minimum cost is selected for the fuzzy systems in the fis tree.
for this example, the cost function (costfcn.m
) simulates the artificialpancreaswithfistreecontrol
model using the candidate rule base. using the resulting simulation output, the cost function computes the cost using the following steps.
calculate errors in the observed glucose level from a nominal glucose level.
if the error value is negative (glucose below the nominal level), set the error value to a high value.
calculate the cost as the root mean square of the error values.
using this cost function, the tuning process selects rule bases that maintain a normal condition and avoid high glucose levels. also, the high error values used in step 2 help discard rule bases that produce low blood glucose levels.
% specify the nominal and minimum glucose levels. reflevel = 90; minlevel = 80; % calculate error from the nominal value. err = glucose - reflevel; % specify high error values for the glucose levels below the nominal level. err(glucose
tuning is a time-consuming process, so for this example, load a pretuned fis tree. to tune the fis tree yourself instead, set runtunefis
to true
.
runtunefis = false; % load pretuned fis tree data data = load('fuzzypancreasexampledata.mat'); mindata = mincostdata; wsvars = ["fist" "closeloop"]; minlevel = 80; reflevel = 90; if runtunefis rng('default') fistruletuned = tunefis(fistinit,rule,... @(fis)costfcn(fis,model,minlevel,reflevel,wsvars,mindata),options); else fistruletuned = data.fistruletuned; mincost = costfcn(fistruletuned,model,minlevel,reflevel,wsvars) end
mincost = 26.3335
simulate the model using the fis tree with tuned rule bases.
fist = fistruletuned; ruletunedoutput = sim(model);
plot the resulting regulated glucose levels and insulin infusion rate.
plotglucoseandinsulin(ruletunedoutput,... 'blood glucose and insulin dosage with learned rule base')
with the tuned rule base, the glucose level is now maintained below 160 mg/dl, and it settles close to 90 mg/dl after the third meal. the controller generates a short-term high insulin dosage (bolus mode) at each meal time and a long-term reduced insulin dosage (basal mode) during the fasting periods.
analyze and modify rule base
to visualize the behavior of the tuned rule base, plot the control surface of each fuzzy system in the fis tree.
figure('position',[300 300 600 300]); subplot(1,2,1) gensurf(fistruletuned.fis(1)) title('control surface - fis1') subplot(1,2,2) gensurf(fistruletuned.fis(2)) title('control surface - fis2')
the following tables show the corresponding rule bases of fis1
and fis2
.
showrulebase(fistruletuned.fis(1))
rule base of fis1: bg_rate: n bg_rate: z bg_rate: p ______________________ _____________________ ______________________ bg_level: l precalculated_dose: vh precalculated_dose: m precalculated_dose: m bg_level: m precalculated_dose: vl precalculated_dose: m precalculated_dose: h bg_level: h precalculated_dose: m precalculated_dose: h precalculated_dose: vl
showrulebase(fistruletuned.fis(2))
rule base of fis2: bg_accel: n bg_accel: z bg_accel: p ________________ ________________ ________________ precalculated_dose: l insulin_dose: vh insulin_dose: vl insulin_dose: h precalculated_dose: m insulin_dose: vl insulin_dose: l insulin_dose: l precalculated_dose: h insulin_dose: l insulin_dose: vl insulin_dose: vh
these tables show that some of the control actions are nonintuitive. for example:
for negative blood glucose rates of change,
fis1
does not increase insulin dosage monotonically with increasing blood glucose levels.for a high blood glucose level and a high positive blood glucose rate of change,
fis1
sets the insulin dosage to medium instead of very high.for negative blood glucose acceleration rates,
fis2
does not monotonically increase insulin dosage with increasing precalculated insulin dosage.for a low precalculated dose and a negative blood glucose acceleration rate,
fis2
sets the insulin dosage to very high instead of low.for a high precalculated dose and zero blood glucose acceleration rate,
fis2
sets the insulin dosage to very low instead of medium.
update the rules by modifying their consequent values.
% update fis1 rules. fistruleupdate = fistruletuned; fistruleupdate.fis(1).rules(1).description = ... "bg_level==l & bg_rate==n => precalculated_dose=vl"; fistruleupdate.fis(1).rules(2).description = ... "bg_level==m & bg_rate==n => precalculated_dose=m"; fistruleupdate.fis(1).rules(3).description = ... "bg_level==h & bg_rate==n => precalculated_dose=h"; fistruleupdate.fis(1).rules(9).description = ... "bg_level==h & bg_rate==p => precalculated_dose=vh"; % update fis2 rules. fistruleupdate.fis(2).rules(1).description = ... "precalculated_dose==l & bg_accel==n => insulin_dose=vl"; fistruleupdate.fis(2).rules(3).description = ... "precalculated_dose==h & bg_accel==n => insulin_dose=m"; fistruleupdate.fis(2).rules(6).description = ... "precalculated_dose==h & bg_accel==z => insulin_dose=m"; fistruleupdate.fis(2).rules(7).description = ... "precalculated_dose==l & bg_accel==p => insulin_dose=l";
view the modified rule bases.
showrulebase(fistruleupdate.fis(1))
rule base of fis1: bg_rate: n bg_rate: z bg_rate: p ______________________ _____________________ ______________________ bg_level: l precalculated_dose: vl precalculated_dose: m precalculated_dose: m bg_level: m precalculated_dose: m precalculated_dose: m precalculated_dose: h bg_level: h precalculated_dose: h precalculated_dose: h precalculated_dose: vh
showrulebase(fistruleupdate.fis(2))
rule base of fis2: bg_accel: n bg_accel: z bg_accel: p ________________ ________________ ________________ precalculated_dose: l insulin_dose: vl insulin_dose: vl insulin_dose: l precalculated_dose: m insulin_dose: vl insulin_dose: l insulin_dose: l precalculated_dose: h insulin_dose: m insulin_dose: m insulin_dose: vh
visualize the resulting fis control surfaces.
figure('position',[300 300 600 300]); subplot(1,2,1) gensurf(fistruleupdate.fis(1)) title('control surface - fis1') subplot(1,2,2) gensurf(fistruleupdate.fis(2)) title('control surface - fis2')
the control surfaces correspond to a more intuitive controller behavior.
to check if the updated rules improve the controller performance, simulate the model and plot the results. compare the results with those of the controller with tuned mf parameters.
fist = fistruleupdate; ruleupdatedoutput = sim(model); plotglucoseandinsulin([ruletunedoutput ruleupdatedoutput],... 'blood glucose and insulin dosage with updated rule base',... {'learned rules','updated rules'})
the controller with updated rules reduces the blood glucose levels compared to the tuned fis tree controller.
updating the rules reduces the value of the cost function.
mincost = costfcn(fistruleupdate,model,minlevel,reflevel,wsvars)
mincost = 23.2702
tune membership function parameters
to further improve the controller performance, you can tune the mf parameters of the fis tree.
to do so, use a local optimization method, such as pattern search. for this example, set the maximum number of optimization iterations to 10.
options.method = 'patternsearch';
options.methodoptions.maxiterations = 10;
by default, each input variable has three uniformly distributed triangular mfs. for example, view the mfs for the first input of fis1
.
figure
plotmf(fistruleupdate.fis(1),'input',1)
configure the tunable settings for the input variables such that the leftmost and rightmost peaks remain unchanged during tuning.
for i = 1:4 in(i).membershipfunctions(1).parameters.free = [0 0 1]; in(i).membershipfunctions(end).parameters.free = [1 0 0]; end
similarly, configure the tunable settings for the output variables. each output variable has five triangular membership functions.
for i = 1:2 out(i).membershipfunctions(1).parameters.free = [0 0 1]; out(i).membershipfunctions(end).parameters.free = [1 0 0]; end
tune the mf parameter values using the updated tunable settings.
if runtunefis figure reset(mindata) rng('default') fistmftuned = tunefis(fistruleupdate,[in;out],... @(fis)costfcn(fis,model,minlevel,reflevel,wsvars,mindata),options); else fistmftuned = data.fistmftuned; mincost = costfcn(fistmftuned,model,minlevel,reflevel,wsvars) end
mincost = 22.0627
the mf tuning process reduces the value of the cost function further.
simulate the model using the controller with tuned mf parameters.
fist = fistmftuned; mftunedoutput = sim(model);
plot the resulting regulated glucose levels and insulin infusion rate. compare the results with those for the controller with tuned rule bases.
plotglucoseandinsulin([ruleupdatedoutput mftunedoutput],... 'blood glucose and insulin dosage with tuned mfs',... {'updated rules','tuned mfs'})
the tuned mf parameters improve the performance and reduce the minimum cost value.
to further improve controller performance you can implement the following modifications.
incrementally add additional inputs to the fis tree controller, such as the patient weight and age, to provide a personalized insulin dosage.
use different numbers of mfs to balance performance and inference complexities.
use different tuning methods and iteration numbers to optimize the fuzzy system parameters.
use real world training data to tune the controller parameters.
% close model.
close_system(model)
references
[1] grant, paul. “a new approach to diabetic control: fuzzy logic and insulin pump technology.” medical engineering & physics 29, no. 7 (september 2007): 824–27. .
[2] wilinska, m.e., l.j. chassin, h.c. schaller, l. schaupp, t.r. pieber, and r. hovorka. “insulin kinetics in type-1 diabetes: continuous and bolus delivery of rapid acting insulin.” ieee transactions on biomedical engineering 52, no. 1 (january 2005): 3–12. .
[3] hovorka, roman, valentina canonico, ludovic j chassin, ulrich haueter, massimo massi-benedetti, marco orsini federici, thomas r pieber, et al. “nonlinear model predictive control of glucose concentration in subjects with type 1 diabetes.” physiological measurement 25, no. 4 (august 1, 2004): 905–20. .
[4] mauseth, richard, youqing wang, eyal dassau, robert kircher, donald matheson, howard zisser, lois jovanovič, and francis j. doyle. “proposed clinical application for tuning fuzzy logic controller of artificial pancreas utilizing a personalization factor.” journal of diabetes science and technology 4, no. 4 (july 2010): 913–22. .
helper functions
function fis = updateinput(fis,id,name,range,mfnames) % update fis input with the specified parameter values. fis.inputs(id).name = name; fis.inputs(id).range = range; for mfid = 1:length(mfnames) fis.inputs(id).membershipfunctions(mfid).name = mfnames(mfid); params = range(1) ... diff(range)*fis.inputs(id).membershipfunctions(mfid).parameters; fis.inputs(id).membershipfunctions(mfid).parameters = params; end end function fis = updateoutput(fis,id,name,range) % update fis output with the specified parameter values. rangediff = diff(range); fis.outputs(id).name = name; % mf names - very low, low, medium, high, very high mfnames = [... "vl","l","m","h","vh"]; for mfid = 1:length(mfnames) fis.outputs(id).membershipfunctions(mfid).name = mfnames(mfid); params = range(1) ... rangediff*fis.outputs(id).membershipfunctions(mfid).parameters; fis.outputs(id).membershipfunctions(mfid).parameters = params; end % extend output range values to fit the output mfs. left = fis.outputs(id).membershipfunctions(1).parameters(1); right = fis.outputs(id).membershipfunctions(end).parameters(end); fis.outputs(id).range = [left right]; end
see also
| |