感觉matlab的GUI学起来比较容易上手(Matlab把布局代码放入.fig文件中,而.m文件中只是记录函数代码,这样减轻了代码量,让人感觉思路更清晰;相对比VC++把布局代码和函数代码都放在一起,让人看着不方便)。下面是我做的单纯形法的GUI。
内容清单:
文件清单以及说明:
0.jpg,2.jpg,guidetu,jpg,GUItu.jpg,m.jpg,mgui.jpg,mm,jpg,r.jpg,sta.jpg,std.jpg
是论文中相关插入图片的源文件。
finalsimple.m,LPtostandard.m,originalsimple.m,originalsimpleM.m,twosteps.m
是论文中相关m文件的源代码
SimplicialMethod.m是单纯形法GUI的源代码
SimplicialMethod.fig是GUI的界面文件
SimplicialMethod_stdapp文件夹里装的是独立于Matlab的单纯形法的GUI应用程序,使用方法见该文件夹里的readme.txt
部分代码如下
SimplicialMethod.m 界面代码:
- function varargout = SimplicialMethod(varargin)
- % SIMPLICIALMETHOD M-file for SimplicialMethod.fig
- % SIMPLICIALMETHOD, by itself, creates a new SIMPLICIALMETHOD or raises the existing
- % singleton*.
- %
- % H = SIMPLICIALMETHOD returns the handle to a new SIMPLICIALMETHOD or the handle to
- % the existing singleton*.
- %
- % SIMPLICIALMETHOD('CALLBACK',hObject,eventData,handles,...) calls the local
- % function named CALLBACK in SIMPLICIALMETHOD.M with the given input arguments.
- %
- % SIMPLICIALMETHOD('Property','Value',...) creates a new SIMPLICIALMETHOD or raises the
- % existing singleton*. Starting from the left, property value pairs are
- % applied to the GUI before SimplicialMethod_OpeningFcn gets called. An
- % unrecognized property name or invalid value makes property application
- % stop. All inputs are passed to SimplicialMethod_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 text to modify the response to help SimplicialMethod
- % Last Modified by GUIDE v2.5 29-Apr-2008 21:31:56
- % Begin initialization code - DO NOT EDIT
- gui_Singleton = 1;
- gui_State = struct('gui_Name', mfilename, ...
- 'gui_Singleton', gui_Singleton, ...
- 'gui_OpeningFcn', @SimplicialMethod_OpeningFcn, ...
- 'gui_OutputFcn', @SimplicialMethod_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 SimplicialMethod is made visible.
- function SimplicialMethod_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 SimplicialMethod (see VARARGIN)
- % Choose default command line output for SimplicialMethod
- handles.output = hObject;
- % Update handles structure
- guidata(hObject, handles);
- % UIWAIT makes SimplicialMethod wait for user response (see UIRESUME)
- % uiwait(handles.figure1);
- % --- Outputs from this function are returned to the command line.
- function varargout = SimplicialMethod_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;
- function input_Callback(hObject, eventdata, handles)
- % hObject handle to input (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 input as text
- % str2double(get(hObject,'String')) returns contents of input as a double
- % --- Executes during object creation, after setting all properties.
- function input_CreateFcn(hObject, eventdata, handles)
- % hObject handle to input (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 clear.
- function clear_Callback(hObject, eventdata, handles)
- % hObject handle to clear (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- set(handles.input,'String','0');
- set(handles.shuchu,'String','0');
- guidata(hObject,handles);
- % --- Executes on button press in M.
- function M_Callback(hObject, eventdata, handles)
- % hObject handle to M (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- in = get(handles.input,'String');
- B = str2num(in);
- [x,fval,flag,iteration]=finalsimple(B)
- result = sprintf('解的状态:nflag = %dn(0 唯一解,1 无穷多,2 无界解,3 无解)n最优解为:nx = ( %s )n最优值为:nfval = %.2fn单纯形法迭代次数:niteration = %dn',flag,num2str(x),fval,iteration);
- set(handles.shuchu,'String',result);
- guidata(hObject,handles);
- % --- Executes on button press in twosteps.
- function twosteps_Callback(hObject, eventdata, handles)
- % hObject handle to twosteps (see GCBO)
- % eventdata reserved - to be defined in a future version of MATLAB
- % handles structure with handles and user data (see GUIDATA)
- in = get(handles.input,'String');
- B = str2num(in);
- [x,fval1,fval2,flag,iteration]=twosteps(B);
- result = sprintf('第一阶段:nfval1 = %.2fn第二阶段:n解的状态:nflag = %dn(0 唯一解,1 无穷多,2 无界解,3 无解)n最优解为:nx = ( %s )n最优值为:nfval = %.2fn单纯形法迭代次数:niteration = %dn',fval1,flag,num2str(x),fval2,iteration);
- set(handles.shuchu,'String',result);
- guidata(hObject,handles);
复制代码
originalsimple.m [原始单纯形法]
- function [x,fval,flag,iteration]=originalsimple(C,A,b,XB)
- %原始单纯形法(需直接给出初始的基变量)
- %Programmed by Liyang(faruto's Studio~!) BNU MATH
- %Email:liyangbnu@mail.bnu.edu.cn QQ:516667408
- %last modified 2008.4.27
- %求解标准型线性规划:max C*X;s.t. A*X=b (b>=0);X>=0;
- %输入:C是n维行向量,A是m*n的系数矩阵,b是m维列向量,XB承装初始基变量的下标
- %输出:x最优解(如果有的话),fval最优值,flag解的状态说明,interation求解时的循环次数
- %flag 最终解的状态说明:
- %flag = 0 LP converged to a solution x
- %flag = 1 Inf feasible solutions
- %flag = 2 LP is unbounded
- %flag = 3 No feasible point was found
- [m,n] = size(A);
- iteration = 0;
- pflag = 1;
- while pflag
- iteration = iteration +1;
- flag = 0;
-
- %%
- sigma = zeros(1,n);
- for col = 1:n
- temp = 0;
- for row = 1:m
- temp = temp+C(XB(row))*A(row,col);
- end
- sigma(col) = C(col)-temp;
- end
-
- %%
- if sigma<=0
- x = zeros(1,n);
- for row = 1:m
- x(XB(row)) = b(row);
- end
- fval = C*x';
-
- for row = 1:m
- for temp = n+1:n+m
- if XB(row)==temp && x(temp)~=0
- flag = 3;
- pflag = 0;
- break;
- end
- end
- if flag == 3
- break;
- end
- end
- if flag == 3
- break;
- end
-
- for col = 1:n
- tflag = 0;
- for row = 1:m
- if col == XB(row)
- tflag = 1;
- break;
- end
- end
- if tflag == 0
- if sigma(col) == 0
- flag = 1;
- pflag = 0;
- break;
- end
- end
- end
- if flag == 1
- break;
- end
-
- if flag == 0;
- pflag = 0;
- fval;
- break;
- end
- else
- for col = 1:n
- if sigma(col)>0 & A(:,col)<=0
- flag = 2;
- pflag = 0;
- break;
- end
- end
- if flag == 2
- break;
- end
-
- %%
- temp = 0;
- for col = 1:n
- if sigma(col)>temp
- temp = sigma(col);
- intobase = col;%入基变量的下标
- end
- end
-
- theta = zeros(m,1);
- for row = 1:m
- if A(row,intobase)>0
- theta(row) = b(row)/A(row,intobase);
- end
- end
- temp = Inf;
- for row = 1:m
- if theta(row)>0 & theta(row)
- temp = theta(row);
- outbase = XB(row);%出基变量的下标
- outrow = row;%出基变量在基变量中的位置
- end
- end
-
- %%
- b(outrow,1) = b(outrow,1)/A(outrow,intobase);
- A(outrow,:) = A(outrow,:)/A(outrow,intobase);
-
- for row = 1:m
- if row ~= outrow
- b(row) = b(row) - b(outrow)*A(row,intobase);
- A(row,:) = A(row,:) - A(outrow,:)*A(row,intobase);
-
- end
- end
- %%
-
- for row = 1:m
- if XB(row) == outbase;
- XB(row) = intobase;
- end
- end
- end
- end
- %%
- if flag == 0
- disp('LP converged to a solution x!');
- x;
- fval;
- end
- if flag == 1
- disp('Inf feasible solutions!');
- disp('one of the solutions is:');
- end
- if flag == 2
- disp('LP is unbounded!');
- end
- if flag == 3
- disp('No feasible point was found!');
- end
复制代码
0
评分
-
参与人数 1 | 威望 +10 |
+10 |
积分 +10 |
收起
理由
|
vvg
| + 10 |
+ 10 |
+ 10 |
多分享经验和资料 |
查看全部评分
|
|
|
|