首页 文章 精选 留言 我的

精选列表

搜索[国密算法],共10000篇文章
优秀的个人博客,低调大师

【车间调度】基于遗传算法求解车间调度模型含GUI

2 --> 作业车间调度问题描述 作业车间调度问题(Job Shop Scheduling, JSP)是最经典的几个NP-hard问题之一。其应用领域极其广泛,涉及航母调度,机场飞机调度,港口码头货船调度,汽车加工流水线等。 JSP问题描述:一个加工系统有M台机器,要求加工N个作业,其中,作业i包含工序数为Li。令,则L为任务集的总工序数。其中,各工序的加工时间已确定,并且每个作业必须按照工序的先后顺序加工。调度的任务是安排所有作业的加工调度排序,约束条件被满足的同时,使性能指标得到优化。 作业车间调度需要考虑如下约束: Cons1:每道工序在指定的机器上加工,且必须在其前一道工序加工完成后才能开始加工; Cons2:某一时刻1台机器只能加工1个作业; Cons3:每个作业只能在1台机器上加工1次; Cons4:各作业的工序顺序和加工时间已知,不随加工排序的改变而改变。 问题实例 下面给出作业车间调度问题的一个实例,其中每个工序上标注有一对数值(m,p),其中,m表示当前工序必须在第m台机器上进行加工,p表示第m台机器加工当前工序所需要的加工时间。(注:机器和作业的编号从0开始)jop0=[(0,3),(1,2),(2,2)]jop1=[(0,2),(2,1),(1,4)]jop2=[(1,4),(2,3)]在这个例子中,作业jop0有3道工序:它的第1道工序上标注有(0,3),其表示第1道工序必须在第0台机器上进行加工,且需要3个单位的加工时间;它的第2道工序上标注有(1,2),其表示第2道工序必须在第1台机器上进行加工,且需要2个单位的加工时间;余下的同理。总的来说,这个实例中共有8道工序。该问题的一个可行解是L=8道工序开始时间的一个排列,且满足问题的约束。下图给出了一个可行解(注:该解不是最优解)的示例: function varargout = diaodu(varargin) % DIAODU MATLAB code for diaodu.fig % DIAODU, by itself, creates a new DIAODU or raises the existing % singleton*. % % H = DIAODU returns the handle to a new DIAODU or the handle to % the existing singleton*. % % DIAODU('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in DIAODU.M with the given input arguments. % % DIAODU('Property','Value',...) creates a new DIAODU or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before diaodu_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to diaodu_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 diaodu % Last Modified by GUIDE v2.5 26-May-2016 17:18:21 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @diaodu_OpeningFcn, ... 'gui_OutputFcn', @diaodu_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 diaodu is made visible. function diaodu_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 diaodu (see VARARGIN) % Choose default command line output for diaodu handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes diaodu wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = diaodu_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 edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (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 edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double % --- Executes during object creation, after setting all properties. function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (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 edit2_Callback(hObject, eventdata, handles) % hObject handle to edit9 (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 edit9 as text % str2double(get(hObject,'String')) returns contents of edit9 as a double % --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit9 (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 edit3_Callback(hObject, eventdata, handles) % hObject handle to edit3 (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 edit3 as text % str2double(get(hObject,'String')) returns contents of edit3 as a double % --- Executes during object creation, after setting all properties. function edit3_CreateFcn(hObject, eventdata, handles) % hObject handle to edit3 (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 edit4_Callback(hObject, eventdata, handles) % hObject handle to edit17 (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 edit17 as text % str2double(get(hObject,'String')) returns contents of edit17 as a double % --- Executes during object creation, after setting all properties. function edit4_CreateFcn(hObject, eventdata, handles) % hObject handle to edit17 (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 edit5_Callback(hObject, eventdata, handles) % hObject handle to edit2 (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 edit2 as text % str2double(get(hObject,'String')) returns contents of edit2 as a double % --- Executes during object creation, after setting all properties. function edit5_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (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 edit6_Callback(hObject, eventdata, handles) % hObject handle to edit5 (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 edit5 as text % str2double(get(hObject,'String')) returns contents of edit5 as a double % --- Executes during object creation, after setting all properties. function edit6_CreateFcn(hObject, eventdata, handles) % hObject handle to edit5 (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 edit7_Callback(hObject, eventdata, handles) % hObject handle to edit4 (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 edit4 as text % str2double(get(hObject,'String')) returns contents of edit4 as a double % --- Executes during object creation, after setting all properties. function edit7_CreateFcn(hObject, eventdata, handles) % hObject handle to edit4 (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 edit8_Callback(hObject, eventdata, handles) % hObject handle to edit7 (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 edit7 as text % str2double(get(hObject,'String')) returns contents of edit7 as a double % --- Executes during object creation, after setting all properties. function edit8_CreateFcn(hObject, eventdata, handles) % hObject handle to edit7 (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 edit9_Callback(hObject, eventdata, handles) % hObject handle to edit6 (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 edit6 as text % str2double(get(hObject,'String')) returns contents of edit6 as a double % --- Executes during object creation, after setting all properties. function edit9_CreateFcn(hObject, eventdata, handles) % hObject handle to edit6 (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 edit10_Callback(hObject, eventdata, handles) % hObject handle to edit9 (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 edit9 as text % str2double(get(hObject,'String')) returns contents of edit9 as a double % --- Executes during object creation, after setting all properties. function edit10_CreateFcn(hObject, eventdata, handles) % hObject handle to edit9 (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 edit11_Callback(hObject, eventdata, handles) % hObject handle to edit5 (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 edit5 as text % str2double(get(hObject,'String')) returns contents of edit5 as a double % --- Executes during object creation, after setting all properties. function edit11_CreateFcn(hObject, eventdata, handles) % hObject handle to edit5 (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 edit12_Callback(hObject, eventdata, handles) % hObject handle to edit17 (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 edit17 as text % str2double(get(hObject,'String')) returns contents of edit17 as a double % --- Executes during object creation, after setting all properties. function edit12_CreateFcn(hObject, eventdata, handles) % hObject handle to edit17 (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 edit13_Callback(hObject, eventdata, handles) % hObject handle to edit7 (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 edit7 as text % str2double(get(hObject,'String')) returns contents of edit7 as a double % --- Executes during object creation, after setting all properties. function edit13_CreateFcn(hObject, eventdata, handles) % hObject handle to edit7 (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 edit14_Callback(hObject, eventdata, handles) % hObject handle to edit17 (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 edit17 as text % str2double(get(hObject,'String')) returns contents of edit17 as a double % --- Executes during object creation, after setting all properties. function edit14_CreateFcn(hObject, eventdata, handles) % hObject handle to edit17 (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 edit15_Callback(hObject, eventdata, handles) % hObject handle to edit9 (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 edit9 as text % str2double(get(hObject,'String')) returns contents of edit9 as a double % --- Executes during object creation, after setting all properties. function edit15_CreateFcn(hObject, eventdata, handles) % hObject handle to edit9 (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 edit16_Callback(hObject, eventdata, handles) % hObject handle to edit17 (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 edit17 as text % str2double(get(hObject,'String')) returns contents of edit17 as a double % --- Executes during object creation, after setting all properties. function edit16_CreateFcn(hObject, eventdata, handles) % hObject handle to edit17 (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 edit17_Callback(hObject, eventdata, handles) % hObject handle to edit8 (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 edit8 as text % str2double(get(hObject,'String')) returns contents of edit8 as a double % --- Executes during object creation, after setting all properties. function edit17_CreateFcn(hObject, eventdata, handles) % hObject handle to edit8 (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 edit18_Callback(hObject, eventdata, handles) % hObject handle to edit17 (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 edit17 as text % str2double(get(hObject,'String')) returns contents of edit17 as a double % --- Executes during object creation, after setting all properties. function edit18_CreateFcn(hObject, eventdata, handles) % hObject handle to edit17 (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 edit19_Callback(hObject, eventdata, handles) % hObject handle to edit19 (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 edit19 as text % str2double(get(hObject,'String')) returns contents of edit19 as a double % --- Executes during object creation, after setting all properties. function edit19_CreateFcn(hObject, eventdata, handles) % hObject handle to edit19 (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 edit20_Callback(hObject, eventdata, handles) % hObject handle to edit20 (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 edit20 as text % str2double(get(hObject,'String')) returns contents of edit20 as a double % --- Executes during object creation, after setting all properties. function edit20_CreateFcn(hObject, eventdata, handles) % hObject handle to edit20 (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 edit21_Callback(hObject, eventdata, handles) % hObject handle to edit21 (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 edit21 as text % str2double(get(hObject,'String')) returns contents of edit21 as a double % --- Executes during object creation, after setting all properties. function edit21_CreateFcn(hObject, eventdata, handles) % hObject handle to edit21 (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 pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) gx11=str2num(get(handles.edit1,'String')); gx12=str2num(get(handles.edit2,'String')); gx13=str2num(get(handles.edit3,'String')); gx21=str2num(get(handles.edit4,'String')); gx22=str2num(get(handles.edit5,'String')); gx23=str2num(get(handles.edit6,'String')); gx31=str2num(get(handles.edit7,'String')); gx32=str2num(get(handles.edit8,'String')); gx33=str2num(get(handles.edit9,'String')); gxt11=str2num(get(handles.edit10,'String')); gxt12=str2num(get(handles.edit11,'String')); gxt13=str2num(get(handles.edit12,'String')); gxt21=str2num(get(handles.edit13,'String')); gxt22=str2num(get(handles.edit14,'String')); gxt23=str2num(get(handles.edit15,'String')); gxt31=str2num(get(handles.edit16,'String')); gxt32=str2num(get(handles.edit17,'String')); gxt33=str2num(get(handles.edit18,'String')); sl1=str2num(get(handles.edit19,'String')); sl2=str2num(get(handles.edit20,'String')); sl3=str2num(get(handles.edit21,'String')); % Instance=[2 10 1 45 0 0 % 2 10 1 45 0 0 % 2 34 1 20 0 10 % 2 34 1 20 0 10 % 2 34 1 20 0 10 % 0 28 1 12 2 10 % 0 28 1 12 2 10 % 0 28 1 12 2 10]; % gx11=str2num(gx11); % gx12=str2num(gx12); % gx13=str2num(gx13); % gx21=str2num(gx21); % gx22=str2num(gx22); % gx23=str2num(gx23); % gx31=str2num(gx31); % gx32=str2num(gx32); % gx33=str2num(gx33); % gxt11=str2num(gxt11); % gxt12=str2num(gxt12); % gxt13=str2num(gxt13); % gxt21=str2num(gxt21); % gxt22=str2num(gxt22); % gxt23=str2num(gxt23); % gxt31=str2num(gxt31); % gxt32=str2num(gxt32); % gxt33=str2num(gxt33); % sl1=str2num(sl1); % sl2=str2num(sl2); % sl3=str2num(sl3); Instance=zeros(sl1+sl2+sl3,6); % for i=1:sl1 % Instance(i,:)=[3-gx11 gxt13 3-gx12 gxt12 3-gx13 gxt11]; % end % for i=3:5 % Instance(i,:)=[3-gx21 gxt23 3-gx22 gxt22 3-gx23 gxt21]; % end % for i=6:9 % Instance(i,:)=[3-gx31 gxt33 3-gx32 gxt32 3-gx33 gxt31]; % end for i=1:sl1 Instance(i,:)=[3-gx11 gxt13 3-gx12 gxt12 3-gx13 gxt11]; end for i=sl1+1:sl1+sl2 Instance(i,:)=[3-gx21 gxt23 3-gx22 gxt22 3-gx23 gxt21]; end for i=sl1+sl2+1:sl1+sl2+sl3 Instance(i,:)=[3-gx31 gxt31 3-gx32 gxt32 3-gx33 gxt33]; end NumOfPopulation=20;%种群的个体数 population=cell(1,NumOfPopulation);%种群 MaxGen=500;%最大迭代次数 PCross=0.9;%交叉概率 PMutation=0.08;%变异概率 SchedulingBest=cell(1,MaxGen);%最佳顺序 TimeBest=zeros(MaxGen,1);%各代最佳时间 TimeAvg=zeros(MaxGen,1);%各代的平均时间 %生成初始种群 for i=1:NumOfPopulation population{i}=InitPopulation(Instance); end %开始种群迭代 SchedulingBest{1}= population{1}; SchedulingBestSofar=SchedulingBest{1}; for i=1:MaxGen disp(['当前进化代数为' num2str(i)]); MaxTime=zeros(1,NumOfPopulation);%种群中个体的最佳时间 %复制 population=Selection(population,Instance); %交叉 population=CrossOver(population,PCross,SchedulingBestSofar); %变异 population=Mutation(population,PMutation); %统计信息 TotalTime=0; for j=1:NumOfPopulation MaxTime(j)=MaxSpoon(Instance,population{j}); TotalTime=TotalTime+MaxTime(j); end [MinMaxTime,Index]=min(MaxTime); SchedulingBest{i}=population{Index};%各代最佳顺序 SchedulingBestSofar=SchedulingBest{i}; TimeBest(i)=MinMaxTime;%各代最佳时间 TimeAvg(i)=TotalTime/NumOfPopulation;%各代的平均时间 end %显示最佳加工顺序甘特图 [MinTime,Index]=min(TimeBest); MaxMaxTime=max(TimeBest); [MinMaxTime,Pstarts,Pends,PJobName]=MaxSpoon(Instance,SchedulingBest{Index}); starts=Pstarts; ends=Pends; JobName=PJobName; %最大完成时间为MinMaxTime disp(['最大加工周期是' num2str(MinMaxTime)]); %以下为图形显示%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% axes(handles.axes1); [m,n]=size(JobName); axis_size=[0 max(max(ends))+2 0 m]; yla=[1:m]; set(gca,'ytick',yla); ylabel('机器','FontSize',12,'color','b'); xlabel('加工时间(分钟)','FontSize',12,'color','b'); title(['最优加工时间(分钟): ' num2str(MinMaxTime)],'FontSize',16,'color','r'); ZO=m+1; for i=1:m for j=1:n x=[starts(i,j) ends(i,j)]; y=[ZO-i ZO-i]; line(x,y); x=[ends(i,j) ends(i,j)]; y=[ZO-i ZO-i-0.5]; line(x,y); x=[starts(i,j) ends(i,j)]; y=[ZO-i-0.5 ZO-i-0.5]; line(x,y); st=strcat(int2str(starts(i,j))); text(x(1),y(1)-0.1,st,'FontSize',8,'color','m'); st=strcat(int2str(ends(i,j))); text(x(2),y(2)-0.1,st,'FontSize',8,'color','m'); x=[starts(i,j) starts(i,j)]; y=[ZO-i ZO-i-0.5]; line(x,y); TrueJobName=strcat(int2str(JobName(i,j))); text((starts(i,j)+ends(i,j))/2-0.2,ZO-i-0.2,TrueJobName); end end %显示迭代过程中最佳时间和平均时间的变化情况 figure(2); axis_size=[0 MaxGen 0 MaxMaxTime+10]; axis(axis_size); ylabel('Processing Time','FontSize',12,'color','b'); xlabel('Processing Generation','FontSize',12,'color','b'); title('The Literator of Best Time and Average Time','FontSize',16,'color','r'); Gen=[1:MaxGen]; plot(Gen,TimeBest,'r',Gen,TimeAvg,'b') h = legend('最优时间','平均时间'); set(h,'Location','Best'); function edit22_Callback(hObject, eventdata, handles) % hObject handle to edit22 (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 edit22 as text % str2double(get(hObject,'String')) returns contents of edit22 as a double % --- Executes during object creation, after setting all properties. function edit22_CreateFcn(hObject, eventdata, handles) % hObject handle to edit22 (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 edit23_Callback(hObject, eventdata, handles) % hObject handle to edit20 (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 edit20 as text % str2double(get(hObject,'String')) returns contents of edit20 as a double % --- Executes during object creation, after setting all properties. function edit23_CreateFcn(hObject, eventdata, handles) % hObject handle to edit20 (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 edit24_Callback(hObject, eventdata, handles) % hObject handle to edit21 (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 edit21 as text % str2double(get(hObject,'String')) returns contents of edit21 as a double % --- Executes during object creation, after setting all properties. function edit24_CreateFcn(hObject, eventdata, handles) % hObject handle to edit21 (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

优秀的个人博客,低调大师

前端进阶算法4:链表原来如此简单(+leetcode刷题)

引言 链表相对于数组来说,要复杂的多,首先,链表不需要连续的内存空间,它是由一组零散的内存块透过指针连接而成,所以,每一个块中必须包含当前节点内容以及后继指针。最常见的链表类型有单链表、双链表以及循环链表。 学习链表最重要的是 多画图多练习 ,没有捷径可循,在遇到链表问题时,瓶子君总结了一下,可以按照以下五步骤: 确定解题的数据结构:单链表、双链表或循环链表等 确定解题思路:如何解决问题 画图实现:画图可以帮助我们发现思维中的漏洞(一些思路不周的情况) 确定边界条件:思考解题中是否有边界问题以及如何解决 代码实现:解题完成 本文会给常用链表(单链表、双链表以及循环链表)的基本操作已经代码实现,并给出实现思路,这些都是链表解题的基石,请务必掌握! 最后附赠一道 leetcode 题目! 下面开始本节的学习吧!!! 一、单链表 img 单链表结构: function List () { // 节点 let Node = function (element) { this.element = element this.next = null } // 初始头节点为 null let head = null // 链表长度 let length = 0 // 操作 this.getList = function() {return head} this.search = function(list, element) {} this.append = function(element) {} this.insert = function(position, element) {} this.remove = function(element){} this.isEmpty = function(){} this.size = function(){} } 1. 追加节点: 确定解题的数据结构:单链表 确定解题思路: 初始化一个节点(待追加节点),遍历到链尾,在尾节点后插入该节点 画图实现: 确定边界条件: 当链表为 null ,直接将 head 指向待插入节点,不需要遍历 代码实现: function append (element) { let node = new Node(element), p = head if (!head){ head = node } else { while (p.next) { p = p.next } p.next = node } length += 1 } // 测试 let list = new List() for(let i = 0; i < 5; i+=1) { list.append(i) } 解题完成 2. 查找: 确定解题的数据结构:单链表 确定解题思路: 遍历单链表,判断节点值是否等于待查找值,相等则返回 true ,否则继续遍历下一个节点,直到遍历完整个链表还未找到,则返回 false 画图实现: 很简单,读者可以尝试画一下 确定边界条件: 当链表为 null ,可直接返回 false 代码实现: // 判断链表中是否存在某节点 function search(element) { let p = head if (!p) return false while(p) { if (p.element === element) return true p = p.next } return false } // 测试 list.search(4) // true list.search(11) // false 解题完成 3. 在 position 位置插入: 确定解题的数据结构:单链表 确定解题思路: 初始化一个节点(待插入节点 node ),遍历到 position 前一个位置节点,在该节点后插入 node 画图实现:img 确定边界条件: 当 position 为 0 时,直接将插入节点 node.next 指向 head , head 指向 node 即可,不需要遍历 当待插入位置 position < 0 或超出链表长度 position > length ,都是有问题的,不可插入,此时直接返回 null ,插入失败 代码实现: // 插入 position 的后继节点 function insert (position, element) { // 创建插入节点 let node = new createNode(element) if (position >= 0 && position <= length) { let prev = head, curr = head, index = 0 if(position === 0) { node.next = head head = node } else { while(index < position) { prev = curr curr = curr.next index ++ } prev.next = node node.next = curr } length += 1 } else { return null } } // 测试 list.insert(10) 解题完成 4. 删除: 确定解题的数据结构:单链表 确定解题思路: 遍历单链表,找到待删除节点,删除 画图实现:img 确定边界条件: 当链表为 null ,直接返回 代码实现: // 删除值为 element 节点 function remove (element) { let p = head, prev = head if(!head) return while(p) { if(p.element === element) { p = p.next prev.next = p } else { prev = p p = p.next } } } 解题完成 5. 复杂度分析: 查找:从头节点开始查找,时间复杂度为 O(n) 插入或删除:在某一节点后插入或删除一个节点(后继节点)的时间复杂度为 O(1) 链表五步骤是不是很好用,下面看一下双链表 二、双链表 顾名思义,单链表只有一个方向,从头节点到尾节点,那么双链表就有两个方向,从尾节点到头节点: function DoublyLinkedList() { let Node = function(element) { this.element = element // 前驱指针 this.prev = null // 后继指针 this.next = null } // 初始头节点为 null let head = null // 新增尾节点 let tail = null // 链表长度 let length = 0 // 操作 this.search = function(element) {} this.insert = function(position, element) {} this.removeAt = function(position){} this.isEmpty = function(){ return length === 0 } this.size = function(){ return length } } 1. 在 position 位置插入节点: 确定解题的数据结构: 双链表 确定解题思路: 初始化一个节点(待插入节点 node ),遍历链表到 position 前一个位置节点,在该节点位置后插入 node 画图实现: 确定边界条件: 当待插入位置 position < 0 或超出链表长度 position > length ,都是有问题的,不可插入,此时直接返回 null ,插入失败 代码实现: // 插入 position 的后继节点 function insert (position, element) { // 创建插入节点 let node = new Node(element) if (position >= 0 && position < length) { let prev = head, curr = head, index = 0 if(position === 0) { // 在第一个位置添加 if(!head) { // 注意这里与单链表不同 head = node tail = node } else { // 双向 node.next = head head.prev = node // head 指向新的头节点 head = node } } else if(position === length) { // 插入到尾节点 curr = tial curr.next = node node.prev = curr // tail 指向新的尾节点 tail = node } else { while(index < position) { prev = curr curr = curr.next index ++ } // 插入到 prev 后,curr 前 prev.next = node node.next = curr curr.prev = node node.prev = prev } length += 1 return true } else { return false } } // 测试 list.insert(10) 解题完成 2. 删除: 确定解题的数据结构: 双链表 确定解题思路: 遍历双链表,找到待删除节点,删除 画图实现: 确定边界条件: 当链表为 null ,直接返回 代码实现: // 删除 position 位置的节点 function removeAt (position) { if (position >= 0 && position < length && length > 0) { let prev = head, curr = head, index = 0 if(position === 0) { // 移除头节点 if(length === 1) { // 仅有一个节点 head = null tail = null } else { head = head.next head.prev = null } } else if(position === length-1) { // 移除尾节点 curr = tial tail = curr.prev tail.next = null } else { while(index < position) { prev = curr curr = curr.next index ++ } // 移除curr prev.next = curr.next curr.next.prev = prev } length -= 1 return curr.element } else { return null } } 解题完成 3. 查找: 双链表的查找和单链表类似,都是遍历链表,找到返回 true,找不到返回 false 。 4. 复杂度分析: 查找:查找前驱节点或后继节点时间复杂度为 O(1),其它节点仍为 O(n) 插入或删除:插入或删除前驱节点或后继节点的时间复杂度都为 O(1) > 三、循环单链表 循环单链表是一种特殊的单链表,它和单链表的唯一区别是:单链表的尾节点指向的是 NULL,而循环单链表的尾节点指向的是头节点,这就形成了一个首尾相连的环:img 既然有循环单链表,当然也有循环双链表,循环双链表和双链表不同的是: 循环双链表的 tail.next( tail 的后继指针) 为 null ,循环双链表的 tail.next 为 head 循环双链表的 head.prev( head 的前驱指针) 为 null ,循环双链表的 head.prev 为 tail 这里以循环单列表为例 function CircularLinkedList() { let Node = function(element) { this.element = element // 后继指针 this.next = null } // 初始头节点为 null let head = null // 链表长度 let length = 0 // 操作 this.search = function(element) {} this.insert = function(positon, element) {} this.removeAt = function(position){} this.isEmpty = function(){ return length === 0 } this.size = function(){ return length } } 1. 在 positon 后插入: 确定解题的数据结构: 循环单链表 确定解题思路: 初始化一个节点(待插入节点 node ),遍历到 position 前一个位置节点,在该节点后插入 node 画图实现: 确定边界条件: 当 position 为 0 时,需要遍历到尾节点,然后在尾节点后插入节点 , 并将 head 指向 当待插入位置 position < 0 或超出链表长度 position > length ,都是有问题的,不可插入,此时直接返回 null ,插入失败 代码实现: // 插入 position 的后继节点 function insert (position, element) { // 创建插入节点 let node = new createNode(element) if (position >= 0 && position <= length) { let prev = head, curr = head, index = 0 if(position === 0) { // 与单链表插入不同的 while(index < length) { prev = curr curr = curr.next index ++ } prev.next = node node.next = curr head = node } else { while(index < position) { prev = curr curr = curr.next index ++ } prev.next = node node.next = curr } length += 1 } else { return null } } // 测试 list.insert(10) 解题完成 2. 查找: 和单链表类似,唯一不同的是:循环单链表的循环结束条件为 p !== head // 判断链表中是否存在某节点 function search(element) { let p = head if (!p) return false // 和单链表的不同所在 while(p !== head) { if (p.element === element) return true p = p.next } return false } // 测试 list.search(4) // true list.search(11) // false 解题完成 3. 删除: 和单链表类似,唯一不同的是:循环单链表的循环结束条件为 p !== head // 删除值为 element 节点 function remove (element) { let p = head, prev = head if(!head) return while(p !== head) { if(p.element === element) { p = p.next prev.next = p } else { prev = p p = p.next } } } 解题完成 4. 复杂度分析 查找:循环链表从任一节点开始查找目标节点,时间复杂度为 O(n) 插入或删除:它和单链表一样,后继节点插入及删除的时间复杂度为 O(1) > 四、leetcode21:合并两个有序链表 将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 欢迎将答案提交到 https://github.com/sisterAn/JavaScript-Algorithms/issues/11,让更多人看到,瓶子君也会在明日放上自己的解答。

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册