BÀI TẬP LỚN CÔNG NGHỆ TÍNH TOÁN MỀM SOFT COMPUTING

20 11 0
BÀI TẬP LỚN  CÔNG NGHỆ TÍNH TOÁN MỀM  SOFT COMPUTING

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

ĐẠI HỌC QUỐC GIA TP HỒ CHÍ MINH TRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA ĐIỆN – ĐIỆN TỬ BÁO CÁO BÀI TẬP MÔN HỌC PHƯƠNG PHÁP TÍNH TRONG KỸ THUẬT ĐIỆN (CÔNG NGHỆ TÍNH TOÁN MỀM – SOFT COMPUTING)GVHD: DƯƠNG HOÀI NGHĨA

ĐẠI HỌC QUỐC GIA TP HỒ CHÍ MINH TRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA ĐIỆN – ĐIỆN TỬ -o0o - BÁO CÁO BÀI TẬP MÔN HỌC: PHƯƠNG PHÁP TÍNH TRONG KỸ THUẬT ĐIỆN (CƠNG NGHỆ TÍNH TỐN MỀM – SOFT COMPUTING) HVTH MSHV GV : VÕ VĂN TIÊN : 2070352 : TS DƯƠNG HOÀI NGHĨA TP HỒ CHÍ MINH NGÀY 04 THÁNG 04 NĂM 2021 Bài tập 1.1 Dùng phương pháp phân nhóm mờ, viết m file phân chia tập liệu X  R2 thành nhóm (Tự tạo tập liệu gồm 1000 phần tử R 2) So sánh với K-means clustering (dùng hàm kmeans) Bài làm a Chương trình phân nhóm mờ + Tạo liệu mơ hình Simulink gồm 1000 phần tử: + Phân nhóm: Mã chương trình file m X = n = c = m = % U = for for for for % for XX'; size(X,2); % number of data 4; % number of partition 2; initialization zeros(c,n); kk = 1: floor(n/4), U(1,kk) = 1; kk = floor(n/4)+1:2*floor(n/4), U(2,kk) = 1; kk = 2*floor(n/4)+1:3*floor(n/4), U(3,kk) = 1; kk = 3*floor(n/4)+1: n, U(4,kk) = 1; compute V k1 = : c XX1 = 0; XX2 = [0; 0]; for k2 = : n, XX1 = XX1 + U(k1,k2)^m; XX2(1) = XX2(1) + X(1,k2)*(U(k1,k2)^m); XX2(2) = XX2(2) + X(2,k2)*(U(k1,k2)^m); end V(1,k1) = XX2(1)/XX1; V(2,k1) = XX2(2)/XX1; end for kk = 1:50 % compute U for k1 = : c, for k2 = : n, XX = 0; end end end end for k = : c, XXt(1) = X(1,k2)-V(1,k1); XXt(2) = X(2,k2)-V(2,k1); XXm(1) = X(1,k2)-V(1,k); XXm(2) = X(2,k2)-V(2,k); XX = XX + (XXt(1)*XXt(1) + XXt(2)*XXt(2))/ (XXm(1)*XXm(1) + XXm(2)*XXm(2)); end U(k1,k2) = 1/XX; end end % compute V for k1 = : c XX1 = 0; XX2 = [0; 0]; for k2 = : n, XX1 = XX1 + U(k1,k2)^m; XX2(1) = XX2(1) + X(1,k2)*(U(k1,k2)^m); XX2(2) = XX2(2) + X(2,k2)*(U(k1,k2)^m); end V(1,k1) = XX2(1)/XX1; V(2,k1) = XX2(2)/XX1; end V end clf for kk = 1:n k2 = 1; Um = U(1,kk); for k1 = 2:c if Um < U(k1,kk), Um = U(k1,kk); k2 = k1; end end if k2 == 1, plot(X(1,kk), X(2,kk), 'rx','LineWidth',1), if k2 == 2, plot(X(1,kk), X(2,kk), 'g*','LineWidth',1), if k2 == 3, plot(X(1,kk), X(2,kk), 'k+','LineWidth',1), if k2 == 4, plot(X(1,kk), X(2,kk), 'bo','LineWidth',1), hold on end plot(V(1,1), V(2,1), 'ms','LineWidth',2) plot(V(1,2), V(2,2), 'ms','LineWidth',2) plot(V(1,3), V(2,3), 'ms','LineWidth',2) plot(V(1,4), V(2,4), 'ms','LineWidth',2) end end end end + Kết hiển thị figure b Chương trình phân nhóm dùng thuật tốn Kmean + Mã chương trình file m [MyK,V] = kmeans(XX, NN = size(MyK,1) figure(2) for kk = 1:NN if MyK(kk) == 1, if MyK(kk) == 2, if MyK(kk) == 3, if MyK(kk) == 4, hold on end plot(V(1,1), V(1,2), plot(V(2,1), V(2,2), plot(V(3,1), V(3,2), plot(V(4,1), V(4,2), + Kết hiển thị figure 4); plot(XX(kk,1), plot(XX(kk,1), plot(XX(kk,1), plot(XX(kk,1), XX(kk,2), XX(kk,2), XX(kk,2), XX(kk,2), 'ms','LineWidth',2) 'ms','LineWidth',2) 'ms','LineWidth',2) 'ms','LineWidth',2) 'rx','LineWidth',1), 'g*','LineWidth',1), 'k+','LineWidth',1), 'bo','LineWidth',1), end end end end c Kết luận: + Các tiêu chuẩn phân nhóm khác cho kết khác phân nhóm đối tượng Tuy nhiên, kết tương tự Bài tập 1.2 Dùng Simulink mô hệ thống điều khiển mờ Mamdani cho đối tượng có hàm truyền đạt: G(s) = , r = 0-100 STT = -> a1 = 2.9; a2 = 3.9; b = 1.9, ta có hàm truyền đạt tương ứng với STT: G(s) = , r = 0-100 Ta thực điều khiển hàm truyền đạt điều khiển PID theo phương pháp Mandani, khối mơ phịng dung Simulink sau : Bên khối PID: Luật điều khiển Mamdani khối FuzzyD, FuzzyP, FuzzyI sau : + FuzzyD: + FuzzyI: + FuzzyP: + Kết hiển thị figure Nhận xét: - Thời gian độ thấp >4s Độ vọt lố thấp Sau 16s, hệ thống bị ổn định Bài tập 2.1 Huấn luyện mạng truyền thẳng để phân nhóm phần tử tập X  R2 thành hai nhóm xác định đường thẳng x2 = ax1 (Tự tạo tập liệu gồm 1000 phần tử R2): STT = -> a = 0.7; b = 0.84 ta có mạng truyền thẳng phân nhóm xác định đường thẳng x2=0.7x1 + Tạo liệu mô hình Simulink gồm 1000 phần tử: + Phân nhóm: Mã chương trình file m XX = XYd(:,1:2)'; Yd = XYd(:,3)'; % khai bao mang truyen thang voi tin hieu vao % [-4 4; -4 4] min, max of inputs % lop an co 10 neurons voi ham tac dong tansig % lop co neuron voi ham tac dong purelin myNet = newff([-4 4; -4 4],[10 1],{'tansig' 'purelin'},'trainlm'); myNet.trainParam.epochs = 50; % so epoch max myNet.trainParam.goal = 0.01; % nguong dung figure(1) myNet = train(myNet,XX,Yd); % huan luyen YY = sim(myNet,XX); figure(2) clf plot(YY,'k'), hold on, plot(Yd,'r') gensim(myNet,0.1) + Kết hiển thị figure 1.5 0.5 ­0.5 ­1 ­1.5 200 400 600 800 1000 1200 Nhận xét: Kết đạt gần mong đợi Bài tập 2.2 Dùng Simulink mô hệ thống rời rạc H(z) = (dùng khối discrete transfer function với sample time = 0.1) với tín hiệu vào ồn trắng (dùng khối band limited white noise với sample time = 1) Lưu tín hiệu vào hệ thống vào tập tin (dùng khối to file với sample time = 0.1) Viết m file để đọc liệu trên tập tin huấn luyện mạng truyền thẳng So sánh tín hiệu hệ thống với tín hiệu mơ dự báo Nhận xét STT = -> a = 0.7; b = 0.84 ta có hàm truyền rời rạc H(z) = == Cấu hình tạo liệu, mơ phỏng, dự báo simulink sau: Đoạn chương trình m-file: N = size(XX,1) % train Adaline eta = 0.0001; W = [0; 0; 0]; Nepoch = 10; for Kepoch = 1:Nepoch for k = 1:N X = [XX(k,2:4)']; Yd = XX(k,1); Y = X'*W; % adaline W = W + eta*(Yd-Y)*X; end % epoch end W % simulate adaline for k = 1:N X = [XX(k,2:4)']; YY(k,1) = X'*W; % adaline end clf subplot(211), plot(XX(:,4),'k'), xlabel('t'), ylabel('x'), axis([0 1000 -2 2]) subplot(212), plot(XX(:,1),'r'), hold on, plot(YY,'k'), xlabel('t'), ylabel('y'), axis([0 1000 -2 2]) Kết Figure: x ­1 ­2 100 200 300 400 500 t 600 700 800 900 1000 100 200 300 400 500 t 600 700 800 900 1000 y ­1 ­2 Kết huấn luyện mạng Kết dự báo mô Kết luận: - Kết ngõ training mạng giống với kết ngõ hệ thống Kết dự đoán dự đoán xu hướng gần kết ngõ Kết mơ có tiếp nhận tín hiệu hồi tiếp nên mơ với ngõ hệ thống Bài tập 2.3 Dùng Simulink mô hệ thống rời rạc H(z) = (dùng khối discrete transfer function với sample time = 0.1) với tín hiệu vào ồn trắng (dùng khối band limited white noise với sample time = 1) Lưu tín hiệu vào hệ thống vào tập tin (dùng khối to file với sample time = 0.1) Viết m file để đọc liệu trên tập tin huấn luyện mạng ANFIS So sánh tín hiệu hệ thống với tín hiệu mơ dự báo Nhận xét Tương tự 2.2 ta có hàm truyền tương ứng với STT H(z)= N = size(XX,1) myNet = newff([-60 60; -60 60; -2 2],[10 1],{'tansig' 'purelin'},'trainlm'); myNet.trainParam.epochs = 100; myNet.trainParam.goal = 0.0001; myNet = train(myNet, XX(:,2:4)', XX(:,1)'); YY = sim(myNet, XX(:,2:4)'); plot(XX(:,1),'r'), hold on, plot(XX(:,1),'k') gensim(myNet,0.1) Kết Figure: Kết huấn luyện mạng Kết dự báo mô - Kết luận: Kết ngõ training mạng giống với kết ngõ hệ thống Kết dự đoán dự đoán xu hướng gần kết ngõ Kết mơ có tiếp nhận tín hiệu hồi tiếp nên mô với ngõ hệ thống Bài tập 3.1 Dùng giải thuật di truyền, tìm đường ngắn nối điểm ABCDEFG Theo STT ta có: a = 0.7; b = 0.84; d = 0.9 Ta đoạn chương trình m: % my_ga.m clear all a = 0.7; b = 0.84; d = 0.9; n = 7; locations(1,:) = [1 , ]; %A locations(2,:) = [1+a , ]; %B locations(3,:) = [1+a+b , ]; %C locations(4,:) = [1+a+b+a, ]; %D locations(5,:) = [1+a , 1-a ]; %E locations(6,:) = [1+a+b , 1-a ]; %F locations(7,:) = [1+a , 1+d]; %G AA = meshgrid(1:n); distances = reshape(sqrt(sum((locations(AA,:)locations(AA',:)).^2,2)),n,n) % fitness function FitnessFcn = @(x)open_traveling_salesman_fitness(x,distances); % ga option options = gaoptimset('PopulationType','custom','PopInitRange',[1;n]); options = gaoptimset(options,'CreationFcn',@create_permutations, 'CrossoverFcn', @crossover_permutation,'MutationFcn', @mutate_permutation, 'PlotFcn', [],'Generations',20000, 'PopulationSize',60,'StallGenLimit',200,'Vectorized','on'); numberOfVariables = n; [x,fval,reason,output] = ga(FitnessFcn, numberOfVariables, options); x{1,1} axis([0 -2 7]); hold on plot(locations(:,1),locations(:,2),'bo'); plot(locations(x{1,1},1),locations(x{1,1},2),'r.-'); fval function pop = create_permutations(NVARS,FitnessFcn,options) % This function creates a population of permutations % POP = CREATE_PERMUTATION(NVARS,FITNESSFCN,OPTIONS) creates a population % of permutations POP each with a length of NVARS % % The arguments to the function are % NVARS: Number of variables % FITNESSFCN: Fitness function % OPTIONS: Options structure used by the GA % % Copyright 2004 The MathWorks, Inc $Revision: 1.1.4.1 $ $Date: 2004/03/26 13:26:06 $ totalPopulationSize = sum(options.PopulationSize); n = NVARS; pop = cell(totalPopulationSize,1); for i = 1:totalPopulationSize pop{i} = randperm(n); end end function xoverKids = crossover_permutation(parents,options,NVARS, FitnessFcn,thisScore,thisPopulation) % CROSSOVER_PERMUTATION Custom crossover function for traveling salesman % XOVERKIDS = CROSSOVER_PERMUTATION(PARENTS,OPTIONS,NVARS, % FITNESSFCN,THISSCORE,THISPOPULATION) crossovers PARENTS to produce % the children XOVERKIDS % % The arguments to the function are % PARENTS: Parents chosen by the selection function % OPTIONS: Options structure created from GAOPTIMSET % NVARS: Number of variables % FITNESSFCN: Fitness function % STATE: State structure used by the GA solver % THISSCORE: Vector of scores of the current population % THISPOPULATION: Matrix of individuals in the current population % % Copyright 2004 The MathWorks, Inc $Revision: 1.1.4.1 $ $Date: 2004/03/26 13:26:06 $ nKids = length(parents)/2; xoverKids = cell(nKids,1); % Normally zeros(nKids,NVARS); index = 1; for i=1:nKids % here is where the special knowledge that the population is a cell % array is used Normally, this would be thisPopulation(parents(index),:); parent = thisPopulation{parents(index)}; index = index + 2; % Flip a section of parent1 p1 = ceil((length(parent) -1) * rand); p2 = p1 + ceil((length(parent) - p1- 1) * rand); child = parent; child(p1:p2) = fliplr(child(p1:p2)); xoverKids{i} = child; % Normally, xoverKids(i,:); end end function mutationChildren = mutate_permutation(parents ,options,NVARS, FitnessFcn, state, thisScore,thisPopulation,mutationRate) % MUTATE_PERMUTATION Custom mutation function for traveling salesman % MUTATIONCHILDREN = MUTATE_PERMUTATION(PARENTS,OPTIONS,NVARS, % FITNESSFCN,STATE,THISSCORE,THISPOPULATION,MUTATIONRATE) mutate the % PARENTS to produce mutated children MUTATIONCHILDREN % % The arguments to the function are % PARENTS: Parents chosen by the selection function % OPTIONS: Options structure created from GAOPTIMSET % NVARS: Number of variables % FITNESSFCN: Fitness function % STATE: State structure used by the GA solver % THISSCORE: Vector of scores of the current population % THISPOPULATION: Matrix of individuals in the current population % MUTATIONRATE: Rate of mutation % % Copyright 2004 The MathWorks, Inc $Revision: 1.1.4.1 $ $Date: 2004/03/26 13:26:06 $ % Here we swap two elements of the permutation mutationChildren = cell(length(parents),1);% Normally zeros(length(parents),NVARS); for i=1:length(parents) parent = thisPopulation{parents(i)}; % Normally thisPopulation(parents(i),:) p = ceil(length(parent) * rand(1,2)); child = parent; child(p(1)) = parent(p(2)); child(p(2)) = parent(p(1)); mutationChildren{i} = child; % Normally mutationChildren(i,:) end end function scores = traveling_salesman_fitness(x,distances) % A custom fitness function % SCORES = TRAVELING_SALESMAN_FITNESS(X,DISTANCES) Calculate the fitness % of an individual The fitness is the total distance traveled for an % ordered set of cities in X DISTANCE(A,B) is the distance from the city % A to the city B % % Copyright 2004 The MathWorks, Inc $Revision: 1.1.4.1 $ $Date: 2004/03/26 13:26:06 $ scores = zeros(size(x,1),1); for j = 1:size(x,1) % here is where the special knowledge that the population is a cell % array is used Normally, this would be pop(j,:); p = x{j}; f = distances(p(end),p(1)); for i = 2:length(p) f = f + distances(p(i-1),p(i)); end scores(j) = f; end end Ta có kết đường ngắn sau: Bài tập 3.2 Dùng giải thuật di truyền, tìm thơng số điều khiển PI cho đối tượng G(s) = cho J = Theo STT ta có: a = 0.7 ; b = 0.84 từ ta có hàm truyền G(s) = Ta có chương trình m: clear all % Stop condition max_generation = 200; max_stall_generation = 50; epsilon = 0.001; % Parameter pop_size = 20; % population size npar = 2; % parameters Kp, Ki range = [ 0 % value 100 100]; % max value dec = [2 2]; % posiition of decimal point sig = [5 5]; % gen cross_prob = 0.9; % probability of cross mutate_prob = 0.1; % mutation elitism = 1; % keep the best individual % par = range*rand(pop_size, npar); par = InitPop(pop_size, npar, range); % stop_condition = 0; generation = 0; stall_generation = 0; for pop_index = 1:pop_size, Kp = par(pop_index,1); Ki = par(pop_index,2); sim('myGA_PID.mdl'); J = e'*e; fitness(pop_index) = 1/(J+eps); % end [bestfit0, bestchrom] = max(fitness); while ~stop_condition, generation = generation + 1; pop = EncodeDecimal(par, sig, dec); parent = SelectLinearRanking(pop, fitness, 0.5, elitism, bestchrom); % child = CrossTwoPoint(parent, cross_prob, elitism, bestchrom); % pop = MutateUniform(child, mutate_prob, elitism, bestchrom); % par = DecodeDecimal(pop, sig, dec); % for pop_index = 1:pop_size, Kp = par(pop_index,1); Ki = par(pop_index,2); sim('myGA_PID.mdl'); J = e'*e; fitness(pop_index) = 1/(J+eps); end [bestfit(generation), bestchrom] = max(fitness); if generation == max_generation stop_condition = 1; elseif generation > if abs(bestfit(generation) - bestfit(generation - 1)) < epsilon stall_generation = stall_generation + 1; if stall_generation == max_stall_generation, stop_condition = 1; end else stall_generation = 0; end end end plot(1./bestfit); Kp = par(bestchrom,1); Ki = par(bestchrom,2); sim('myGA_PID.mdl'); Mơ hình Simulink: Kết đầu Scope: Nhận xét: Tín hiệu ngõ đáp ứng theo tín hiệu đặt, sai số xác lập thấp, đồng thời thời gian xác lập ngắn (sau 1s) xảy tình trạng vọt lố .. .Bài tập 1.1 Dùng phương pháp phân nhóm mờ, viết m file phân chia tập liệu X  R2 thành nhóm (Tự tạo tập liệu gồm 1000 phần tử R 2) So sánh với K-means clustering (dùng hàm kmeans) Bài làm... thấp Sau 16s, hệ thống bị ổn định Bài tập 2.1 Huấn luyện mạng truyền thẳng để phân nhóm phần tử tập X  R2 thành hai nhóm xác định đường thẳng x2 = ax1 (Tự tạo tập liệu gồm 1000 phần tử R2): STT... luận: + Các tiêu chuẩn phân nhóm khác cho kết khác phân nhóm đối tượng Tuy nhiên, kết tương tự Bài tập 1.2 Dùng Simulink mô hệ thống điều khiển mờ Mamdani cho đối tượng có hàm truyền đạt: G(s)

Ngày đăng: 12/08/2022, 17:38

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan