main content

multiprocessor scheduling using simulated annealing with a custom data type -凯发k8网页登录

this example shows how to use simulated annealing to minimize a function using a custom data type. here simulated annealing is customized to solve the multiprocessor scheduling problem.

multiprocessor scheduling problem

the multiprocessor scheduling problem consists of finding an optimal distribution of tasks on a set of processors. the number of processors and number of tasks are given. time taken to complete a task by a processor is also provided as data. each processor runs independently, but each can only run one job at a time. we call an assignment of all jobs to available processors a "schedule". the goal of the problem is to determine the shortest schedule for the given set of tasks.

first we determine how to express this problem in terms of a custom data type optimization problem that simulannealbnd function can solve. we come up with the following scheme: first, let each task be represented by an integer between 1 and the total number of tasks. similarly, each processor is represented by an integer between 1 and the number of processors. now we can store the amount of time a given job will take on a given processor in a matrix called "lengths". the amount of time "t" that the processor "i" takes to complete the task "j" will be stored in lengths(i,j).

we can represent a schedule in a similar manner. in a given schedule, the rows (integer between 1 to number of processors) will represent the processors and the columns (integer between 1 to number of tasks) will represent the tasks. for example, the schedule [1 2 3;4 5 0;6 0 0] would be tasks 1, 2, and 3 performed on processor 1, tasks 4 and 5 performed on processor 2, and task 6 performed on processor 3.

here we define our number of tasks, number of processors, and lengths array. the different coefficients for the various rows represent the fact that different processors work with different speeds. we also define a "sampleschedule" which will be our starting point input to simulannealbnd.

rng default % for reproducibility
numberofprocessors = 11;
numberoftasks = 40;
lengths = [10*rand(1,numberoftasks);
           7*rand(1,numberoftasks);
           2*rand(1,numberoftasks);
           5*rand(1,numberoftasks);
           3*rand(1,numberoftasks);
           4*rand(1,numberoftasks);
           1*rand(1,numberoftasks);
           6*rand(1,numberoftasks);
           4*rand(1,numberoftasks);
           3*rand(1,numberoftasks);
           1*rand(1,numberoftasks)];
% random distribution of task on processors (starting point)
sampleschedule = zeros(numberofprocessors,numberoftasks);
for task = 1:numberoftasks
    processorid = 1   floor(rand*(numberofprocessors));
    index = find(sampleschedule(processorid,:)==0);
    sampleschedule(processorid,index(1)) = task;
end

simulated annealing for a custom data type

by default, the simulated annealing algorithm solves optimization problems assuming that the decision variables are double data types. therefore, the annealing function for generating subsequent points assumes that the current point is a vector of type double. however, if the datatype option is set to 'custom' the simulated annealing solver can also work on optimization problems involving arbitrary data types. you can use any valid matlab® data structure you like as decision variable. for example, if we want simulannealbnd to use "sampleschedule" as decision variable, a custom data type can be specified using a matrix of integers. in addition to setting the datatype option to 'custom' we also need to provide a custom annealing function via the annealingfcn option that can generate new points.

custom annealing functions

this section shows how to create and use the required custom annealing function. a trial point for the multiprocessor scheduling problem is a matrix of processor (rows) and tasks (columns) as discussed before. the custom annealing function for the multiprocessor scheduling problem will take a job schedule as input. the annealing function will then modify this schedule and return a new schedule that has been changed by an amount proportional to the temperature (as is customary with simulated annealing). here we display our custom annealing function.

type mulprocpermute.m
function schedule = mulprocpermute(optimvalues,problemdata)
% mulprocpermute moves one random task to a different processor.
% newx = mulprocpermute(optimvalues,problemdata) generate a point based
% on the current point and the current temperature
% 凯发官网入口首页 copyright 2006 the mathworks, inc.
schedule = optimvalues.x;
% this loop will generate a neighbor of "distance" equal to
% optimvalues.temperature.  it does this by generating a neighbor to the
% current schedule, and then generating a neighbor to that neighbor, and so
% on until it has generated enough neighbors.
for i = 1:floor(optimvalues.temperature) 1
    [nrows ncols] = size(schedule);
    schedule = neighbor(schedule, nrows, ncols);
end
%=====================================================%
function schedule = neighbor(schedule, nrows, ncols)
% neighbor generates a single neighbor to the given schedule.  it does so
% by moving one random task to a different processor.  the rest of the code
% is to ensure that the format of the schedule remains the same.
row1 = randinteger(1,1,nrows) 1;
col = randinteger(1,1,ncols) 1;
while schedule(row1, col)==0
    row1 = randinteger(1,1,nrows) 1;
    col = randinteger(1,1,ncols) 1;
end
row2 = randinteger(1,1,nrows) 1;
while row1==row2
    row2 = randinteger(1,1,nrows) 1;
end
for j = 1:ncols
    if schedule(row2,j)==0
        schedule(row2,j) = schedule(row1,col);
        break
    end
end
schedule(row1, col) = 0;
for j = col:ncols-1
    schedule(row1,j) = schedule(row1,j 1);
end
schedule(row1,ncols) = 0;
%=====================================================%
function out = randinteger(m,n,range)
%randinteger generate integer random numbers (m-by-n) in range
len_range = size(range,1) * size(range,2);
% if the irange is specified as a scalar.
if len_range < 2
    if range < 0
        range = [range 1, 0];
    elseif range > 0
        range = [0, range-1];
    else
        range = [0, 0];    % special case of zero range.
    end
end
% make sure range is ordered properly.
range = sort(range);
% calculate the range the distance for the random number generator.
distance = range(2) - range(1);
% generate the random numbers.
r = floor(rand(m, n) * (distance 1));
% offset the numbers to the specified value.
out = ones(m,n)*range(1);
out = out   r;

objective function

we need an objective function for the multiprocessor scheduling problem. the objective function returns the total time required for a given schedule (which is the maximum of the times that each processor is spending on its tasks). as such, the objective function also needs the lengths matrix to be able to calculate the total times. we are going to attempt to minimize this total time. here we display our objective function

type mulprocfitness.m
function timetocomplete = mulprocfitness(schedule, lengths)
%mulprocfitness determines the "fitness" of the given schedule.
%  in other words, it tells us how long the given schedule will take using the
%  knowledge given by "lengths"
%   凯发官网入口首页 copyright 2006 the mathworks, inc.
[nrows ncols] = size(schedule);
timetocomplete = zeros(1,nrows);
for i = 1:nrows
    timetocomplete(i) = 0;
    for j = 1:ncols
        if schedule(i,j)~=0
            timetocomplete(i) = timetocomplete(i) lengths(i,schedule(i,j));
        else
            break
        end
    end
end
timetocomplete = max(timetocomplete);

simulannealbnd will call our objective function with just one argument x, but our fitness function has two arguments: x and "lengths". we can use an anonymous function to capture the values of the additional argument, the lengths matrix. we create a function handle 'objectivefcn' to an anonymous function that takes one input x, but calls 'mulprocfitness' with x and "lengths". the variable "lengths" has a value when the function handle 'fitnessfcn' is created so these values are captured by the anonymous function.

% lengths was defined earlier
fitnessfcn = @(x) mulprocfitness(x,lengths);

we can add a custom plot function to plot the length of time that the tasks are taking on each processor. each bar represents a processor, and the different colored chunks of each bar are the different tasks.

type mulprocplot.m
function stop = mulprocplot(~,optimvalues,flag,lengths)
%mulprocplot plotfcn used for samultiprocessordemo
%   stop = mulprocplot(options,optimvalues,flag) where optimvalues is a
%   structure with the following fields:
%              x: current point
%           fval: function value at x
%          bestx: best point found so far
%       bestfval: function value at bestx
%    temperature: current temperature
%      iteration: current iteration
%      funccount: number of function evaluations
%             t0: start time
%              k: annealing parameter 'k'
%
%   flag: current state in which plotfcn is called. possible values are:
%           init: initialization state
%           iter: iteration state
%           done: final state
%
%   stop: a boolean to stop the algorithm.
%
%   凯发官网入口首页 copyright 2006-2015 the mathworks, inc.
persistent thistitle %#ok
stop = false;
switch flag
    case 'init'
        set(gca,'xlimmode','manual','zlimmode','manual', ...
            'alimmode','manual')
        titlestr = sprintf('current point - iteration %d', optimvalues.iteration);
        thistitle = title(titlestr,'interp','none');
        toplot = i_generateplotdata(optimvalues, lengths);
        ylabel('time','interp','none');
        bar(toplot, 'stacked','edgecolor','none');
        xlength = size(toplot,1);        
        set(gca,'xlim',[0,1   xlength])
    case 'iter'
        if ~rem(optimvalues.iteration, 100)
            toplot = i_generateplotdata(optimvalues, lengths);
            bar(toplot, 'stacked','edgecolor','none');
            titlestr = sprintf('current point - iteration %d', optimvalues.iteration);
            thistitle = title(titlestr,'interp','none');            
        end
end
function toplot = i_generateplotdata(optimvalues, lengths)
schedule = optimvalues.x;
nrows = size(schedule,1);
% remove zero columns (all processes are idle)
maxlen = 0;
for i = 1:nrows
    if max(nnz(schedule(i,:))) > maxlen
        maxlen = max(nnz(schedule(i,:)));
    end
end
schedule = schedule(:,1:maxlen);
toplot = zeros(size(schedule));
[nrows, ncols] = size(schedule);
for i = 1:nrows
    for j = 1:ncols
        if schedule(i,j)==0 % idle process
            toplot(i,j) = 0;
        else
            toplot(i,j) = lengths(i,schedule(i,j));
        end
    end
end

but remember, in simulated annealing the current schedule is not necessarily the best schedule found so far. we create a second custom plot function that will display to us the best schedule that has been discovered so far.

type mulprocplotbest.m
function stop = mulprocplotbest(~,optimvalues,flag,lengths)
%mulprocplotbest plotfcn used for samultiprocessordemo
%   stop = mulprocplotbest(options,optimvalues,flag) where optimvalues is a
%   structure with the following fields:
%              x: current point
%           fval: function value at x
%          bestx: best point found so far
%       bestfval: function value at bestx
%    temperature: current temperature
%      iteration: current iteration
%      funccount: number of function evaluations
%             t0: start time
%              k: annealing parameter 'k'
%
%   flag: current state in which plotfcn is called. possible values are:
%           init: initialization state
%           iter: iteration state
%           done: final state
%
%   stop: a boolean to stop the algorithm.
%
%   凯发官网入口首页 copyright 2006-2015 the mathworks, inc.
persistent thistitle %#ok
stop = false;
switch flag
    case 'init'
        set(gca,'xlimmode','manual','zlimmode','manual', ...
            'alimmode','manual')
        titlestr = sprintf('current point - iteration %d', optimvalues.iteration);
        thistitle = title(titlestr,'interp','none');
        toplot = i_generateplotdata(optimvalues, lengths);
        xlength = size(toplot,1);
        ylabel('time','interp','none');
        bar(toplot, 'stacked','edgecolor','none');
        set(gca,'xlim',[0,1   xlength])
    case 'iter'
        if ~rem(optimvalues.iteration, 100)
            toplot = i_generateplotdata(optimvalues, lengths);
            bar(toplot, 'stacked','edgecolor','none');
            titlestr = sprintf('best point - iteration %d', optimvalues.iteration);
            thistitle = title(titlestr,'interp','none');            
        end
         
end
function toplot = i_generateplotdata(optimvalues, lengths)
schedule = optimvalues.bestx;
nrows = size(schedule,1);
% remove zero columns (all processes are idle)
maxlen = 0;
for i = 1:nrows
    if max(nnz(schedule(i,:))) > maxlen
        maxlen = max(nnz(schedule(i,:)));
    end
end
schedule = schedule(:,1:maxlen);
toplot = zeros(size(schedule));
[nrows, ncols] = size(schedule);
for i = 1:nrows
    for j = 1:ncols
        if schedule(i,j)==0
            toplot(i,j) = 0;
        else
            toplot(i,j) = lengths(i,schedule(i,j));
        end
    end
end

simulated annealing options setup

we choose the custom annealing and plot functions that we have created, as well as change some of the default options. reannealinterval is set to 800 because lower values for reannealinterval seem to raise the temperature when the solver was beginning to make a lot of local progress. we also decrease the stalliterlimit to 800 because the default value makes the solver too slow. finally, we must set the datatype to 'custom'.

options = optimoptions(@simulannealbnd,'datatype', 'custom', ...
    'annealingfcn', @mulprocpermute, 'maxstalliterations',800, 'reannealinterval', 800, ...
    'plotfcn', {{@mulprocplot, lengths},{@mulprocplotbest, lengths},@saplotf,@saplotbestf});

finally, we call simulated annealing with our problem information.

schedule = simulannealbnd(fitnessfcn,sampleschedule,[],[],options);
% remove zero columns (all processes are idle)
maxlen = 0;
for i = 1:size(schedule,1)
    if max(nnz(schedule(i,:)))>maxlen
        maxlen = max(nnz(schedule(i,:)));
    end
end
% display the schedule
schedule = schedule(:,1:maxlen)
optimization terminated: change in best function value less than options.functiontolerance.
schedule =
    22    34    32     0     0     0     0     0
     5     0     0     0     0     0     0     0
    19     6    12    11    39    35     0     0
     7    20     0     0     0     0     0     0
    30    15    10     3     0     0     0     0
    18    28     0     0     0     0     0     0
    31    33    29     4    21     9    25    40
    24    26    14     0     0     0     0     0
    13    16    23     0     0     0     0     0
    38    36     1     0     0     0     0     0
     8    27    37    17     2     0     0     0

see also

related topics

    网站地图