First, Introduction

In the previous chapter, we used the genetic algorithm to calculate the maximum value of a unary function, but, some people would say, this is not a bit overqualified, obviously I can use less code to achieve the maximum value of the function. Indeed, the genetic algorithm used there is really overqualified, but the blogger's purpose is not to find the maximum, but to show you that the genetic algorithm is a feasible algorithm, and Bo master writing matlab code is feasible. Now, we need to use the code of the previous write as the basic code to solve this problem. Rest assured, the problem in this article is not to be solved in a common way. The spring of the genetic algorithm finally came. Let's get started.

ii. Problem description and modeling

**load Balancing scheduling problem** : Assuming that there are n tasks, the load balancer needs to be assigned to the M server node to handle. The task length of each task, the processing speed of each server node (hereinafter referred to as "node") is known, and a task assignment method is given to minimize the total processing time for all tasks.

OK, so now the topic has, but we have to find a way to model the problem. Obviously, we need 2 matrices, a storage task (including the number of tasks, the length of the task), a server node (including the number of nodes, processing speed).

(1) Task length matrix (short: Task matrix)

We represent the task length of all tasks in the matrix tasks, where I in Task (i) represents the **number of tasks** , and Tasks (i) represent the task **length** of task I. Like what

**tasks = [2, 4, 6, 8]**

Indicates that the length of task 1 is 2, the length of Task 2 is 4, and so on.

(2) Node processing speed matrix (abbreviation: node matrix)

We represent the processing speed of all server nodes in nodes, where I in nodes (i) represents **the node number** , and nodes (i) represents the **processing speed** of node J. Like what

**nodes = [2, 1]**

Indicates that the speed of node 1 is 2, the speed of Node 2 is 1, and so on.

(3) Task processing time matrix

After the task matrix task and node matrix nodes is determined, then all tasks assigned to all nodes of the task processing time can be determined, we use the matrix Timemat to save all nodes of the task processing time, then Timemat (i, j) for task I assigned to the node J processing time required , it is calculated by the following formula:

**Timemat (i, j) = Task (i)/nodes (j);**

With the above three matrices, we have succeeded in describing the problem mathematically.

We use MATLAB code to create a CREATEDATA.M file to model the problem and save the data. As follows:

Percent I empty the argument clear allclc%% Ii. Topic modeling% parameter Definition Tasknum = 100;% task Number Tasklengthrange = [10, 100];% task length range nodenum = 10;% node Number nodespeedrange = [10, 100];% node length range TA SKS = Initrandommat (Tasknum, tasklengthrange);% build task matrix (with task count and task length) nodes = Initrandommat (Nodenum, nodespeedrange);% Build node matrix (with number of nodes and node processing speed) Timemat = Zeros (Tasknum, nodenum);% build processing time matrix for i = 1:tasknum for j = 1:nodenum Timemat (i, J ) = Tasks (1, I)/nodes (1, j); EndEnd (' Data ', ' tasks ', ' nodes ', ' Timemat ');

which The Initrandommat () function code is as follows:

% [name]--initrandommat (initialize random matrix)% [function]--% [Input]--length (length of array)%--range (range of values for array elements ranges range (1) to Range (2))% [ Output]--randommatfunction Randommat = Initrandommat (length, range) Randommat = zeros (1, length); For i = 1:length randommat (1, i) = Round (rand () * (Range (2)-range (1)) + range (1)); End

Three, genetic algorithm to solve the problem

As usual, let's review the steps of the genetic algorithm as follows:

1. Population initialization

2. Calculate the fitness value for each population

3. Select (Selection)

4. Crossover (Crossover)

5. Mutation (Mutation)

6. Repeat 2-5 steps until the number of evolutions is reached

As we said in the previous article, in order to make our code more inclusive, we need to write a few function packages, and then main.m inside the reference, it is very convenient.

(1) Population initialization function popinit (), can produce the initial population according to the **population size** and **chromosome length** provided **. **the code is as follows:

% [Name] -- popinit (population initialization function)% [function]-- constructs a population matrix, where the number of rows is the number of populations, the number of columns is the chromosome length (i.e. the number of genes) and the pattern of the chromosomes is randomly assigned% [input] -- 1. Popsize (population size/number)% -- 2. Clength (chromosome length)% [Output] -- Popmat (population matrix) function Popmat = Popinit (popsize , clength) Popmat = Zeros (popsize, clength); % pre-allocated memory for i = 1:popsize for j = 1:clength Popmat (i, j) = round (rand); % Rand produces a random number between (0,1), round () is the rounding function end end Clear I;clear J;

(2) Calculate the fitness value function Getfitnessvalue () for each population. The code is as follows:

% [name]--getfitnessvalue (calculated population individual fitness value)% [function]--the algorithm of design fitness equation calculation according to different topic requirement %--in the actual load balancing schedule, the task processing of each node is calculated in parallel, so the completion time of all tasks should be the maximum of all node task completion time, not the sum of all task completion time. % [Input]--timemat (Time Processing matrix)--Timemat (I, J): The I task is assigned to the J node, the required processing time%--popmat (population matrix)--Popmat (I, J): On the first chromosome, The node designator assigned to the J Task%--nodenummax (maximum number of nodes)% [Output]--fitvalmat (fitness value per chromosome) function Fitnessvaluematrix = Getfitnessvalue ( Popmat, Timemat, Nodenummax) [Popsize, clength] = size (popmat);% gets the number of populations (rows) and chromosome lengths (columns) Fitnessvaluematrix = Zeros (Popsize, 1)% initialization fitness matrix% maximum node designator for i = 1:popsizemaxlength = eps;% minimum positive number greater than 0 for nodeindex = 1:nodenummaxsumlength = 0; % Clear 0 for j = 1:clengthif Popmat (i, j) = = Nodeindexsumlength = Sumlength + Timemat (J, Nodeindex); Endendif sumlength > maxlengthmaxlength = sumlength; End Endfitnessvaluematrix (i) = Maxlength;endclear i;clear j;clear nodeindex;

(3) Select function selection (), The population can be ** Select ** , the exact algorithm and code are as follows:

% [Name]--selection (select operation)% [function]--use a form of roulette to make choices, increase uncertainty, This makes the population less prone to local optimal% [input]--1. Fitnessvaluematrix (fitness value matrix)%--2. Popmat (not selected population matrix)%--3. Type--1: Preserving the optimal individual of the parent% 0: not preserving the optimal parental individual% [Output]--updatedpopmat (Selected population moment Array) function Updatedpopmat = Selection (Fitnessvaluematrix, Popmat, type) [Popsize, clength] = size (Popmat); Updatedpopmat = Zeros (popsize, clength); Fitnessvaluematrix =/fitnessvaluematrix;% reject a population with a negative adaptation value of 0for i = 1:popsizeif Fitnessvaluematrix (i, 1) < 0fit Nessvaluematrix (i, 1) = 0;endend% Roulette Algorithm p = fitnessvaluematrix/sum (Fitnessvaluematrix); PC = Cumsum (P); for i = 1:popsizeindex = Find (Pc >= rand) Updatedpopmat (i,:) = Popmat (index (1),:); end% whether to keep parental fitness value highest If, if, type = 1, otherwise 0if type[~, bestindex] = max (Fitnessvaluematrix), Updatedpopmat (popsize,:) = Popmat (bestindex,:);% will parental most The optimal chromosome is placed in the last individual of the Offspring endclear I;clear J;

(4) crossover function crossover (), The population can be ** Cross ** , and the intersection is divided into ** Single-point crossover ** and ** multipoint cross ** , depending on the input parameters to choose a different implementation, the exact algorithm and code are as follows:

% [name]--crossover (cross operation)% [function]--select intersection and swap% [input]--1. Popmat (non-intersecting population matrix)%--2. Type--1: Single point crossover%--2: Multi-point cross%--3. Crossrate (crossover rate)--the recommended value is 0.6% [Output]--updatedpopmat (cross-population matrix) function Updatedpopmat = Crossover (Popmat, type, crossrate) [Popsize, clength] = size (Popmat), if type = = 1% Single point crossover for i = 1:2: Popsizeif crossrate >= randcrossposition = Round (rand () * (cLength-2) + 2);% randomly acquires intersections, removes 0 and 1% pairs of crossposition and subsequent binary strings for j = Crossposition:clengthtemp = Popmat ( I, J);p Opmat (i, j) = Popmat (i + 1, j);p Opmat (i + 1, j) = Temp;endend End updatedpopmat = Popmat;elseif type = = 2% multipoint cross for i = 1:2: Popsizeif crossrate >= randcrossPosition1 = round (rand () * (cLength-2) + 2);% first intersection Crossposi Tion2 = Round (rand () * (cLength-2) + 2);% Second Intersection first = min (CrossPosition1, crossPosition2); last = max (CrossPosition1, C RossPosition2); for j = First:lasttemp = Popmat (i, J);p Opmat (i, j) = Popmat (i + 1, j);p Opmat (i + 1, j) = Temp;endend End updatedpopmat = Popmat;elseh = Errordlg (' type can only be 1 (single-point intersection) or 2 (multipoint crossing) ', ' error occurred when crossing '); End Clear I;clear J;

(5) The Mutation function mutation (), the population can be **mutated** , the specific algorithm and code are as follows:

% [name]--mutation (mutation action)% [function]--single point variation: Randomly select the mutation point to change it to 0 or 1% [input]--1. Popmat (non-intersecting population matrix)%--2. Mutaterate (crossover rate)--the recommended value is 0.1%--3. Randvalur (variable random number range 0-randvalue)% [Output]--updatedpopmat (after cross-population matrix) function Updatedpopmat = mutation (Popmat, Mutaterate, Randvalue) [Popsize, clength] = size (Popmat); for i = 1:popsizeif mutaterate >= randmutateposition = ceil (ra nd () * clength);% randomly acquires intersections, removes 0% to mutateposition points for mutation Popmat (i, mutateposition) = Ceil (rand () * randvalue); Endendupdatedpopmat = Popmat;clear I;clear J;

(6) Drawing function Plotgraph: Used to visualize the change in the average total processing time of the population during evolution. The code is as follows:

% [name]--plotgraph (paint)% [function]--visually shows the trend change in average fitness value in the evolutionary process% [input]--1. Generationtime (evolutionary times)%2. Fitnessaveragevaluematrix (average fitness value matrix)% [Output]--nonefunction plotgraph (Fitnessaveragevaluematrix, GenerationTime) x = 1:1: generationtime;y = Fitnessaveragevaluematrix;plot (x, y), Xlabel (' Iteration count '), Ylabel (' Average total processing time '), title (' The evolutionary curve of the average total processing time of the population);

Well, so far, we have completed the function blocks needed to solve this topic. Next, we only need to write the main function main.m, for this chapter of the topic, to solve it. The code is as follows:

Percent I empty variable clear allclc%% ii. Import data and parameter initialization% import parameter data tasks = Load (' Data ', ' tasks '); % Import Task matrix (with task count and task length) tasks = Tasks.tasks; The% import is in the struct format, so one more step nodes = Load (' data ', ' nodes '); % Import node matrix (with number of nodes and node processing speed) nodes = Nodes.nodes;timemat = Load (' data ', ' Timemat '); % Import processing time Matrix Timemat = Timemat.timemat;tasknum = Length (tasks); % task Number Nodenum = length (nodes); % Number of nodes iteratornum = 1000; % iteration count popsize = 300; % population size (number of chromosomes) Crossrate = 0.6; % crossover Probability MutaTerate = 0.01; % mutation probability Fitnessaveragevaluematrix = zeros (iteratornum, 1); % per generation Average Fitness value Fitnessbestvaluematrix = Zeros (iteratornum, 1); % of optimal fitness value per generation bestindividual = Zeros (Iteratornum, tasknum); % of best independent variables per generation Initialize population Popmat = popinit (Popsize, Tasknum, nodenum); Iterative multiplication Gets better individuals (selection, crossover, mutation) for currentnum = 1:iteratornum% The total processing time and fitness matrix of a single chromosome Sumtimemat = Getfitnessvalue (Popmat, Timemat, Nodenum);% total chromosome processing time% record current best data if currentnum = = 1 [~, Bestindex] = min (Sumtimemat); Fitnessbestvaluematrix (currentnum) = min (Sumtimemat); Fitnessaveragevaluematrix (Currentnum) = mean (Sumtimemat); Bestindividual (currentnum,:) = Popmat (Bestindex,:); the chromosome corresponding to the optimal fitness value (allocation scheme) ELSE [~, Bestindex] = min (Sumtimemat); Fitnessbestvaluematrix (currentnum) = min (Fitnessbestvaluematrix (currentnum-1), Min (Sumtimemat)); Fitnessaveragevaluematrix (Currentnum) = mean (Sumtimemat); If Fitnessbestvaluematrix (currentnum) = = Fitnessbestvaluematrix (currentNum-1) bestindividual (currentnum,:) = Bestindividual (currentNum-1,:); ElseIf Fitnessbestvaluematrix (currentnum) = = min (sumtimemat) bestindividual (currentnum,:) = Popmat (Bestindex, :); else H = errordlg (' Please check code ', ' unexpected error '); End End If Currentnum ~= iteratornum% Select Popmat = Selection (Sumtimemat, Popmat, 1);% keep parental best individuals % Cross Popmat = Crossover (Popmat, 1, crossrate);% single point crossover% variation Popmat = mutation (Popmat, Mutaterate, Nodenum); endend%% v. Drawing and showing results% drawing plotgraph (Fitnessaveragevaluematrix, iteratornum);% display Data disp shortest processing time Fitnessbestvaluematrix ( Currentnum, 1) plan = bestindividual (Currentnum,:);

Running the above program, you can get:

Ans = 9.5278

It can be learned that through our genetic algorithm, we can design a set of solutions, so that the total processing time greatly reduced (not guaranteed to be minimal, but close to the smallest). As you can see, as the number of iterations increases, the genetic algorithm optimizes the scheme and then the average total processing time changes.

As you can see, the average total processing time decreases from 30 to the last 9 points as the number of iterations increases.