No BSD License  

Highlights from
WDM Network Blocking Computation Toolbox

image thumbnail
from WDM Network Blocking Computation Toolbox by Przemyslaw Pawelczak
Blocking computation in WDM Networks for three different types of constraints.

varargout=create_load_manual(varargin)
function varargout=create_load_manual(varargin)
%CREATE_LOAD_MANUAL Create traffic matrix manually for specified network.
%   [TRAFFIC]=CREATE_LOAD_UNIFORM(NAME,NUMBER) is a script that
%   creates traffic matrix for a sprcified network saved in a *.MAT file in
%   MATLABROOT/networks/ directory. NAME is the 'NAME.mat' file stored in
%   string that holds definition of the network, NUMBER is the structure
%   number saved in the file according to STRUCTURE{:,:,NUMBER} [1] (for
%   precise fields descriptionsee i.e. help CREATE_NETWORK).
%
%   Script allows user to input various types of paths and loads on them.
%   By askig what user wants to do it is flexible to input as many and as
%   different paths as possible. Possible modes:
%   	1. Shortest path betwen desired nodes,
%   	2. Manually chosen path,
%   	3. End of script.
%
%   For example MAKE_LOAD_UNIFORM('network',2) makes uniform load for
%   STRUCTURE{:,:,2} definition saved in NETWORK.mat file in
%   MATLABROOT/networks/ directory.
%
%   Result is saved in the NAME.mat file in the TRAFFIC field. If such
%   field does not exists then script makes such one. If not then new
%   traffic matrix is saved in the TRAFFIC field in NAME.mat file. Result
%   can be also loaded to workspace.
%
%   See also CREATE_LOAD_UNIFORM, CREATE_RAND_NETWORK, CREATE_NETWORK.
%
%   References: 
%       [1]  T. E. Cormen et al., "Introduction to Algorithms", 
%       WNT, Warsaw 2000, page 527.

%   Copyright 2003-2004 Przemyslaw Pawelczak
%   E-mail: przemyslaw_pawelczak@idg.com.pl
%   Wroclaw University of Technology, Poland
%   $Revision: 1.0 $  $Date: 2004/02/20 12:40:16 $

%If no NUMBER specified then it will be first
if nargin==1
    varargin{2}=1;
end

%Exeption handling
message=nargchk(1,2,nargin);
if ~isempty(message)
    error('MATLAB:CREATE_LOAD_UNIFORM:NumberOfInputArguments',...
        message);
end
if find(isnan(varargin{1}))
    error('MATLAB:CREATE_LOAD_UNIFORM:ArgumentType',...
        'Argument must be numbers.');
end
%Check if VARARGIN are positive integers
if sum([varargin{1}<0,...
            fix(varargin{1})~=varargin{1}])~=0
    error('MATLAB:CREATE_LOAD_UNIFORM:ArgumentType',...
        'Wrong type of argument.');
end
if ~isstr(varargin{1})
    error('MATLAB:CREATE_LOAD_UNIFORM:ArgumentType',...
        'Argument must be string.');
end

%Change string into number (for all number arguments)
if isstr(varargin{2})==1
    varargin{2}=str2num(varargin{2});
end

%List all '*.mat' files in current working directory
%Change directory to MATLABROOT\networks - catalog must exist
cd([matlabroot,['\work\networks']]);
directory=what;
directory=struct2cell(directory);
directory=cellstr(directory{3});

%Check if specified '*.mat' file already exist
if find(strcmp(directory,[varargin{1},'.mat'])==1)
    %Load *.MAT file
    load(varargin{1});
    fields_vector=[exist('description'),exist('structure')];
    if length(find(fields_vector))~=2
        error('MATLAB:CREATE_LOAD_UNIFORM:BadFieldNames',...
            'Bad field names.'); 
    end
    %Check if TRAFFIC definition already exists in this file
    %Initialize flag
    traffic_exists=0;
    varias=who;
    varias_dimension=cellfun('ndims',varias);
    varias_dimension=length(varias_dimension');
    %Check
    for k=1:varias_dimension
        if isequal(cell2mat(varias(k)),'traffic')
            traffic_exists=1;
        end
    end
    %If definition does exist - add another
    if traffic_exists==1
        %Find how many different LOAD definitions are in this '*.mat' file
        traffic_dimension=cellfun('ndims',traffic);
        traffic_dimension=length(traffic_dimension(1,1,:))+1;
        %Load MAKE_LOAD procedure
        traffic=make_load(varargin{1},traffic,structure,varargin{2},traffic_dimension);
        varargout{1}=save_network(description,structure,traffic,varargin{1});
        %If definition does not exist - make first one
    elseif traffic_exists==0;
        traffic{1,1,1}=[];
        traffic_dimension=1;
        %Load MAKE_LOAD procedure
        traffic=make_load(varargin{1},traffic,structure,varargin{2},traffic_dimension);
        varargout{1}=save_network(description,structure,traffic,varargin{1});
    end  
else
    error('MATLAB:CREATE_LOAD_UNIFORM:BadFiledName',...
        'Bad file name.'); 
end

function [traffic]=make_load(name,traffic,structure,structure_dimension,traffic_dimension)
%Data format: traffic{path number,path-1/load on path-2,number of dimensions (structures)};

%Check how many nodes are in the graph
struct_dim=cellfun('ndims',structure);
structure_length=length(struct_dim(:,1,1));
node_number=0;
for k=1:structure_length
    if ~isempty(structure{k,1,structure_dimension})
        node_number=node_number+1;
    end
end

%Initialization of temporary traffic structure
traffic_temp{1,1,1}=[];

%Computing paths and assignment to structure
%Counter of paths
w=1;
method=4;
while sum([fix(method)~=method,...
            method<0,...
            method>3,...
            method~=5,...
            isinf(method),...
            length(method)>1,...
            isempty(method)])
    disp(sprintf('Path number %d:',w));
    disp(sprintf('\t1. Shortest path betwen desired nodes,'));
    disp(sprintf('\t2. Manually chosen path,'));
    disp(sprintf('\t3. End of script.'));
    method=str2num(input(sprintf('Choose: '),'s'));
    if isempty(method) | length(method)>1
        method=4;
    end
    display_gap;
    switch method
        %Shortest path
        case 1
            nodes_pair=inf;
            %Check exeptions for inputting two nodes
            while sum([fix(nodes_pair)~=nodes_pair,...
                        nodes_pair<0,...
                        isinf(nodes_pair),...
                        length(nodes_pair)~=2,...
                        isempty(nodes_pair),...
                        find(nodes_pair>node_number)])~=0
                nodes_pair=str2num(input(sprintf('Give a pair of nodes: '),'s'));
            end
            traffic_temp{w,1,1}=shortest_path_dijkstra(nodes_pair(1),nodes_pair(2),name,structure_dimension);
            input_load=load_hand;
            traffic_temp{w,2,1}=input_load;
            display_gap;
            w=w+1;
            method=4;
            %User's path
        case 2
            traffic_temp{w,1,1}=[];
            traffic_temp{w,2,1}=[];
            input_path=path_hand;
            traffic_temp{w,1,1}=input_path;
            input_load=load_hand;
            traffic_temp{w,2,1}=input_load;
            display_gap;
            w=w+1;
            method=4;
            %No path
        otherwise
            break;
    end
end  

%Procedure for adding traffic_temp to traffic
if ~exist('traffic_temp')
    traffic=traffic;
else
    for k=1:w-1
        traffic{k,1,traffic_dimension}=traffic_temp{k,1,1};
        traffic{k,2,traffic_dimension}=traffic_temp{k,2,1};
    end
end

function [input_path]=path_hand
input_path=inf;
%Exeption handling
while sum([fix(input_path)~=input_path,...
            input_path<0,...
            isinf(input_path),...
            length(input_path)==1,...
            isempty(input_path)])~=0
    input_path=str2num(input(sprintf('Please give path manually: '),'s'));
end

function [input_load]=load_hand
input_load=inf;
%Exeption handling
while sum([input_load<0,...
            isinf(input_load),...
            length(input_load)>1,...
            isempty(input_load)])~=0
    input_load=str2num(input(sprintf('Please give load: '),'s'));
end

function display_gap
disp(sprintf('\n'));

function [net_out]=save_network(description,structure,traffic,name)
save(name,'description','structure','traffic');
%Cannot make structure with cells of different dimension (sic!) - it needs
%checking
net_out=struct('traffic',traffic);

Contact us at files@mathworks.com