Code covered by the BSD License  

Highlights from
System Identification using GA with a GUI interface

image thumbnail
from System Identification using GA with a GUI interface by Wesam Elshamy
Idnetifying ARX model of a system using Genetic Algorithms with a GUI interface and compare it to Le

identga(varargin)
function varargout = identga(varargin)
%
% Author: Wesam ELSHAMY
% wesamelshamy@ieee.org
% Electrical Engineering Dept., Cairo University, Egypt


% IDENTGA M-file for identga.fig
%      IDENTGA, by itself, creates a new IDENTGA or raises the existing
%      singleton*.
%
%      H = IDENTGA returns the handle to a new IDENTGA or the handle to
%      the existing singleton*.
%
%      IDENTGA('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in IDENTGA.M with the given input arguments.
%
%      IDENTGA('Property','Value',...) creates a new IDENTGA or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before identga_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to identga_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text27 to modify the response to help identga

% Last Modified by GUIDE v2.5 16-Apr-2006 13:51:47

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @identga_OpeningFcn, ...
                   'gui_OutputFcn',  @identga_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before identga is made visible.
function identga_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to identga (see VARARGIN)

% Choose default command line output for identga
handles.output = hObject;
handles.error_tolerance = 0.4;
handles.population_size = 100;
handles.mutation_rate = 0.9;
handles.no_of_generations = 100;
sisodata;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes identga wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = identga_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in estimate.
function estimate_Callback(hObject, eventdata, handles)
% hObject    handle to estimate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% ----- Get parameters from GUI -----
if get(handles.estimate, 'value') == get(handles.estimate, 'max');
% try
o = evalin('base', get(handles.out, 'string'));
i = evalin('base', get(handles.in, 'string'));
na = str2double(get(handles.Na,'string'));
nb = str2double(get(handles.Nb,'string'));
nk = str2double(get(handles.Nk,'string'));
handles.real_output = o;
guidata(hObject, handles);

% ----- Estimate the model using GA and view parameters in GUI -----
[A_parameters_GA B_parameters_GA] = arx2(o, i, na, nb, nk, handles.error_tolerance, handles.population_size, handles.mutation_rate, handles.no_of_generations); % model evaluation using GA
delay_zeros = zeros(1, nk);
set(handles.Aga, 'string', num2str([1 A_parameters_GA])); 
set(handles.Bga, 'string', num2str([delay_zeros B_parameters_GA])); 
GA_model = idpoly([1 A_parameters_GA], [delay_zeros B_parameters_GA]);
handles.GA_model_output = sim(GA_model, i); % simulating the GA model output
guidata(hObject, handles);

% ----- Estimate the model using Least Square and vew parameter in GUI -----
least_model = arx(iddata(o, i), [na nb nk]); % model evaluatin using Least Square
set(handles.Aleast, 'string', num2str(least_model.a));
set(handles.Bleast, 'string', num2str(least_model.b));
handles.least_model_output = sim(least_model, i); % simulating the Least Square model output
guidata(hObject, handles);

% ----- Evaluate the models errors -----
set(handles.GA_error, 'string', num2str(modelerror([1 A_parameters_GA], [delay_zeros B_parameters_GA])));
set(handles.Least_error, 'string', num2str(modelerror(least_model.a, least_model.b)));

plot_output(handles);
set(handles.estimate, 'value', get(handles.estimate, 'min'));
% catch
%     errordlg('Not enough data: Enter model orders and I/O data then hit Estimate','Data missing','on');
% end
end

function Nb_Callback(hObject, eventdata, handles)
% hObject    handle to Nb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Nb as text27
%        str2double(get(hObject,'String')) returns contents of Nb as a double


% --- Executes during object creation, after setting all properties.
function Nb_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Nb (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Nk_Callback(hObject, eventdata, handles)
% hObject    handle to Nk (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Nk as text27
%        str2double(get(hObject,'String')) returns contents of Nk as a double


% --- Executes during object creation, after setting all properties.
function Nk_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Nk (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function out_Callback(hObject, eventdata, handles)
% hObject    handle to out (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of out as text27
%        str2double(get(hObject,'String')) returns contents of out as a double


% --- Executes during object creation, after setting all properties.
function out_CreateFcn(hObject, eventdata, handles)
% hObject    handle to out (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Na_Callback(hObject, eventdata, handles)
% hObject    handle to Na (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Na as text27
%        str2double(get(hObject,'String')) returns contents of Na as a double


% --- Executes during object creation, after setting all properties.
function Na_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Na (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Aga_Callback(hObject, eventdata, handles)
% hObject    handle to Aga (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Aga as text27
%        str2double(get(hObject,'String')) returns contents of Aga as a double


% --- Executes during object creation, after setting all properties.
function Aga_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Aga (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Bga_Callback(hObject, eventdata, handles)
% hObject    handle to Bga (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Bga as text27
%        str2double(get(hObject,'String')) returns contents of Bga as a double


% --- Executes during object creation, after setting all properties.
function Bga_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Bga (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function in_Callback(hObject, eventdata, handles)
% hObject    handle to in (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of in as text27
%        str2double(get(hObject,'String')) returns contents of in as a double


% --- Executes during object creation, after setting all properties.
function in_CreateFcn(hObject, eventdata, handles)
% hObject    handle to in (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in error.
function error_Callback(hObject, eventdata, handles)
% hObject    handle to error (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of error





function Aleast_Callback(hObject, eventdata, handles)
% hObject    handle to Aleast (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Aleast as text27
%        str2double(get(hObject,'String')) returns contents of Aleast as a double


% --- Executes during object creation, after setting all properties.
function Aleast_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Aleast (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Bleast_Callback(hObject, eventdata, handles)
% hObject    handle to Bleast (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Bleast as text27
%        str2double(get(hObject,'String')) returns contents of Bleast as a double


% --- Executes during object creation, after setting all properties.
function Bleast_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Bleast (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in show_real_output.
function show_real_output_Callback(hObject, eventdata, handles)
% hObject    handle to show_real_output (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of show_real_output
try
    real_plot = get(handles.show_real_output, 'value');                 % to draw
    GA_plot = get(handles.show_GA_output, 'value');                     % the
    least_plot = get(handles.show_least_output, 'value');               % output
    % without
    if (real_plot && ~GA_plot && ~least_plot)                           % doing
        hold off;                                                       % the
        plot(evalin('base', get(handles.out, 'string')), 'k');          % estimation
        title('Output of Real, GA and Least Square estimations models');%
        xlabel('Time');                                                 %
        ylabel('Output');                                               %
    else
        plot_output(handles);                                   % while this is for the other cases
    end
catch
    errordlg('Enter Input and Output vectors as they appear on Workspace', 'No I/O Data Availlable', 'on');
end

% --- Executes on button press in show_GA_output.
function show_GA_output_Callback(hObject, eventdata, handles)
% hObject    handle to show_GA_output (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of show_GA_output
plot_output(handles);

% --- Executes on button press in show_least_output.
function show_least_output_Callback(hObject, eventdata, handles)
% hObject    handle to show_least_output (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of show_least_output
plot_output(handles);

% ----- Plot Real, GA, Least Square estimation outputs -----
function plot_output(handles)
real_plot = get(handles.show_real_output, 'value');
GA_plot = get(handles.show_GA_output, 'value');
least_plot = get(handles.show_least_output, 'value');
try
    if (~real_plot && ~GA_plot && ~least_plot) ,cla(handles.axes1);end
    if (~real_plot && ~GA_plot && least_plot) ,hold off; plot(handles.least_model_output,'b');end
    if (~real_plot && GA_plot && ~least_plot) ,hold off; plot(handles.GA_model_output,'r');end
    if (~real_plot && GA_plot && least_plot) ,hold off; plot(handles.GA_model_output, 'r'); hold on; plot(handles.least_model_output, 'b');end
    if (real_plot && ~GA_plot && ~least_plot) ,hold off; plot(handles.real_output, 'k');end
    if (real_plot && ~GA_plot && least_plot) ,hold off; plot(handles.real_output, 'k'); hold on; plot(handles.least_model_output, 'b');end
    if (real_plot && GA_plot && ~least_plot) ,hold off; plot(handles.real_output,'k'); hold on; plot(handles.GA_model_output, 'r');end
    if (real_plot && GA_plot && least_plot) ,hold off; plot(handles.real_output, 'k'); hold on; plot(handles.GA_model_output, 'r'); plot(handles.least_model_output, 'b');end
catch
    errordlg('No model to plot its output: Enter model orders and I/O data then hit Estimate','No Model','on');
end
title('Output of Real, GA and Least Square estimations models');
xlabel('Time');
ylabel('Output');

% --- Executes on button press in GA.
function GA_Callback(hObject, eventdata, handles)
% hObject    handle to GA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
GA_parameters = inputdlg({'Error Tolerance', 'Population Size', 'Mutation Rate' , 'No. of iterations'}, 'GA Parameters' , 1,{'0.4', '30', '0.2', '50'});
if ~(strcmp(GA_parameters,''))
    handles.error_tolerance = str2double(GA_parameters{1});
    handles.population_size = str2double(GA_parameters{2});
    handles.mutation_rate = str2double(GA_parameters{3});
    handles.no_of_generations = str2double(GA_parameters{4});
    guidata(hObject, handles);
end


% --------------------------------------------------------------------
function help_menu_Callback(hObject, eventdata, handles)
% hObject    handle to help_menu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function Untitled_3_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function about_Callback(hObject, eventdata, handles)
% hObject    handle to about (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
msgbox('Genetics Identification Toolbox All rights reserved to Wesam ELSHAMY', 'About');

% --------------------------------------------------------------------
function how_to_use_Callback(hObject, eventdata, handles)
% hObject    handle to how_to_use (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
helpdlg('This toolbox identifies an ARX SISO model for a system given its input and output:          First enter the input and output vectors defined in MATLAB Workspace in their respective boxes          Then enter the model orders (Na Nb Nk) in their respective boxes            When you hit GA parameters a new window opens where you can modify the default parameters (error tolerance = 0.4, population size = 100 and muation rate = 0.9)         Then hit Estimate button to estimate A and B parameters             You can toggle the outputs (real, GA generated or the Lease Squares estimate) by toggling the three buttons under the plot', 'How to use');


% --- Executes during object creation, after setting all properties.
function no_of_generations_CreateFcn(hObject, eventdata, handles)
% hObject    handle to no_of_generations (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function status_Callback(hObject, eventdata, handles)
% hObject    handle to status (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of status as text27
%        str2double(get(hObject,'String')) returns contents of status as a double


% --- Executes during object creation, after setting all properties.
function status_CreateFcn(hObject, eventdata, handles)
% hObject    handle to status (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function GA_error_Callback(hObject, eventdata, handles)
% hObject    handle to GA_error (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of GA_error as text27
%        str2double(get(hObject,'String')) returns contents of GA_error as a double


% --- Executes during object creation, after setting all properties.
function GA_error_CreateFcn(hObject, eventdata, handles)
% hObject    handle to GA_error (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Least_error_Callback(hObject, eventdata, handles)
% hObject    handle to Least_error (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Least_error as text
%        str2double(get(hObject,'String')) returns contents of Least_error as a double


% --- Executes during object creation, after setting all properties.
function Least_error_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Least_error (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


Contact us at files@mathworks.com