Skip to content

Commit

Permalink
Major interface redesign
Browse files Browse the repository at this point in the history
Committing missing changes from issue #34.
  • Loading branch information
mathinking committed Apr 23, 2017
1 parent 24083ea commit e171753
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 67 deletions.
69 changes: 42 additions & 27 deletions chn/+network/@HopfieldNetwork/hopfieldnetwork.m
Original file line number Diff line number Diff line change
@@ -1,43 +1,59 @@
classdef hopfieldnetwork < handle
%HOFIELDNETWORK Continuous Hopfield Network (CHN)
classdef HopfieldNetwork < handle
%HopfieldNetwork Continuous Hopfield Network (CHN)
%
% This class defines abstract methods and properties for Hopfield
% Networks. Therefore it can only be instantiated from within a child
% class.
%
% Input arguments properties:
% Setting - Settings used in the Simulation of
% the Hopfield Network.
% SimFcn - Simulation Function.
% TrainFcn - Training Function.
% TrainingParam - Training Parameters.
% Results - Simulation Results.
%
% See also options.HopfieldNetworkOptions, hopfieldnet, tsphopfieldnet.

properties (GetAccess = protected, SetAccess = protected)
trainFcn;
trainParam;
setting;
simFcn;
results;
Setting;
SimFcn;
TrainFcn;
TrainParam;
Results;
end

methods (Sealed = true)
function net = hopfieldnetwork(networkSize, options)
function net = HopfieldNetwork(networkSize, opts)

% Input checking
if nargin < 1 || nargin > 2
error('hopfieldnetwork:IncorrectInputArguments','Please provide proper input arguments to hopfieldnetwork: networkSize (and options)');
error('HopfieldNetwork:IncorrectInputArguments','Please provide proper input arguments to HopfieldNetwork: networkSize (and options)');
else
assert(isa(networkSize,'double'), 'hopfieldnetwork:invalid_datatype', [networkSize, ' must be double.']);
assert(all(networkSize >= 1), 'hopfieldnetwork:invalid_value', '''networkSize'' must be greater or equal to 1.');
assert(isreal(networkSize) && isnumeric(networkSize) && isscalar(networkSize) && all(mod(networkSize,1)==0) && networkSize >= 1,...
'HopfieldNetwork:invalid_value', [num2str(networkSize), ' must be a scalar integer greater or equal than 1.']);
if nargin == 2
assert(isstruct(options), 'hopfieldnetwork:invalid_datatype', 'options must be a structure created using createOptions.');
assert(isa(opts,'options.HopfieldNetworkOptions') || isa(opts,'options.HopfieldNetworkGQKPOptions') || isa(opts,'options.HopfieldNetworkTSPOptions'),...
'HopfieldNetwork:invalid_datatype', 'options must be an object from the class HopfieldNetworkOptions, HopfieldNetworkGQKPOptions or HopfieldNetworkTSPOptions.');
end
end


if ~isa(networkSize,'float')
networkSize = double(networkSize);
end

% Setting network attributes
net.trainParam.N = networkSize;
net.TrainParam.N = networkSize;

% Setting default options if not provided
if nargin < 2
options = hopfieldnetwork.createOptions();
net = addDefaultOptionValues(net, options);
opts = options.HopfieldNetworkOptions();
end

% Setting desired options available in options structure
else
net = setOptions(net, options);
net = addDefaultOptionValues(net, options);
end
net = setOptions(net, opts);

net.setting = orderfields(net.setting);
net.Setting = orderfields(net.Setting);

end
end
Expand All @@ -52,23 +68,23 @@
% --- Set methods --- %
setTrainFcn(net,trainFcn);
setSimFcn(net,simFcn);
setTrainParam(net,property,value);
end

methods (Hidden = true, Access = private)
net = addDefaultOptionValues(net, options);
%net = addDefaultOptionValues(net, options);
net = setOptions(net, options);
end

methods (Hidden = false, Access = protected)
net = init(net);
end

methods (Static = true, Access = public)
options = createOptions(varargin);
end
% methods (Static = true, Access = public)
% options = createOptions(varargin);
% end

methods (Static = true, Access = protected)
isValidSetting(property,value);
y = satlin(x,u0);
y = invsatlin(x,u0);
end
Expand All @@ -80,7 +96,6 @@
simFcn = getSimFcn(net);
results = getResults(net,field);
setSetting(net,property,value);
setTrainParam(net,property,value);
setResults(net,property,value);
end
end
94 changes: 54 additions & 40 deletions chn/+utils/@TSPLIB/tsplib.m
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
classdef tsplib < handle
classdef TSPLIB < handle
%tsplib Define a TSPLIB problem
%
% This class allows to define a TSPLIB problem.
%
% tsplib properties:
% Name - Name of the TSPLIB problem.
% NumberOfCities - Number of cities.
% Coordinates - TSP problem coordinates
% DistanceType - Type of distance defined for the
% TSP problem.
% DistanceMatrix - Distance matrix for the TSP
% problem.
%
% See also tsphopfieldnetOptions, tsphopfieldnet.

properties (GetAccess = public, SetAccess = private)
name;
nCities
coords;
type;
d;
Name;
NumberOfCities
Coordinates;
DistanceType;
DistanceMatrix;
end

methods %(Sealed = true)
function problem = tsplib(name)
function problem = TSPLIB(name)
if nargin ~= 0
problem(length(name),1) = tsplib;
problem(length(name),1) = utils.TSPLIB;
for i = 1:length(name)

problem(i).name = name{i}; %#ok<*AGROW>
problem(i).Name = name{i}; %#ok<*AGROW>

fid = fopen([problem(i).name,'.tsp'],'rt');
fid = fopen([problem(i).Name,'.tsp'],'rt');

positioned = false;

while ~positioned
str = fgetl(fid);
if strncmp(str,'DIMENSION', 9)
problem(i).nCities = str2double(regexprep(...
problem(i).NumberOfCities = str2double(regexprep(...
str(length('DIMENSION')+1:end),...
{':',' '},''));
end
if strncmp(str, 'EDGE_WEIGHT_TYPE',16)
problem(i).type = regexprep(...
problem(i).DistanceType = regexprep(...
str(length('EDGE_WEIGHT_TYPE')+1:end),...
{':',' '},'');
end
if strcmp(problem(i).type,'EXPLICIT')
if strcmp(problem(i).DistanceType,'EXPLICIT')
if strncmp(str,'EDGE_WEIGHT_FORMAT', 18)
matrixType = regexprep(...
str(length('EDGE_WEIGHT_FORMAT')+1:end),...
Expand All @@ -49,46 +63,46 @@
end
end

if ~strcmp(problem(i).type,'EXPLICIT')
problem(i).coords = textscan(fid, '%*u%f%f%*[^\n]');
problem(i).coords = [problem(i).coords{:}];
if ~strcmp(problem(i).DistanceType,'EXPLICIT')
problem(i).Coordinates = textscan(fid, '%*u%f%f%*[^\n]');
problem(i).Coordinates = [problem(i).Coordinates{:}];
else
dataCoords = textscan(fid, '%f');
dataCoords = [dataCoords{:}];

if strcmp(matrixType, 'LOWER_DIAG_ROW')
problem(i).d = ones(problem(i).nCities);
problem(i).d(logical(tril(problem(i).d))') = dataCoords;
problem(i).d = problem(i).d-tril(problem(i).d)+triu(problem(i).d)';
problem(i).DistanceMatrix = ones(problem(i).NumberOfCities);
problem(i).DistanceMatrix(logical(tril(problem(i).DistanceType))') = dataCoords;
problem(i).DistanceMatrix = problem(i).DistanceMatrix-tril(problem(i).DistanceMatrix)+triu(problem(i).DistanceMatrix)';
elseif strcmp(matrixType, 'UPPER_ROW')
problem(i).d = ones(problem(i).nCities);
problem(i).d(logical(triu(problem(i).d,1))') = dataCoords;
problem(i).d = problem(i).d + triu(problem(i).d',1) - ...
triu(ones(problem(i).nCities));
problem(i).DistanceMatrix = ones(problem(i).NumberOfCities);
problem(i).DistanceMatrix(logical(triu(problem(i).DistanceMatrix,1))') = dataCoords;
problem(i).DistanceMatrix = problem(i).DistanceMatrix + triu(problem(i).DistanceMatrix',1) - ...
triu(ones(problem(i).NumberOfCities));
elseif strcmp(matrixType, 'FULL_MATRIX')
problem(i).d = reshape(dataCoords,...
problem(i).nCities,problem(i).nCities);
problem(i).DistanceMatrix = reshape(dataCoords,...
problem(i).NumberOfCities,problem(i).NumberOfCities);
elseif strcmp(matrixType, 'UPPER_DIAG_ROW')
problem(i).d = ones(problem(i).nCities);
problem(i).d(logical(tril(problem(i).d))) = dataCoords;
problem(i).d = problem(i).d - triu(problem(i).d);
problem(i).d = problem(i).d + problem(i).d';
problem(i).DistanceMatrix = ones(problem(i).NumberOfCities);
problem(i).DistanceMatrix(logical(tril(problem(i).DistanceMatrix))) = dataCoords;
problem(i).DistanceMatrix = problem(i).DistanceMatrix - triu(problem(i).DistanceMatrix);
problem(i).DistanceMatrix = problem(i).DistanceMatrix + problem(i).DistanceMatrix';
end
end

fid = fclose(fid); %#ok<NASGU>

if strcmp(problem(i).type,'GEO')
problem(i).coords = tsplib.convert2LatLon(problem(i).coords); %#TODO Should it be in the object instantation?
problem(i).d = tsphopfieldnet.computeDistance(problem(i).coords,problem(i).type);
elseif strcmp(problem(i).type,'EUC_2D')
problem(i).d = tsphopfieldnet.computeDistance(problem(i).coords,problem(i).type);
elseif strcmp(problem(i).type,'CEIL_2D')
problem(i).d = tsphopfieldnet.computeDistance(problem(i).coords,problem(i).type);
elseif strcmp(problem(i).type,'ATT')
problem(i).d = tsphopfieldnet.computeDistance(problem(i).coords,problem(i).type);
elseif strcmp(problem(i).type,'EUC')
problem(i).d = tsphopfieldnet.computeDistance(problem(i).coords,problem(i).type);
if strcmp(problem(i).DistanceType,'GEO')
problem(i).Coordinates = tsplib.convert2LatLon(problem(i).Coordinates); %#TODO Should it be in the object instantation?
problem(i).DistanceMatrix = network.HopfieldNetworkTSP.computeDistance(problem(i).Coordinates,problem(i).DistanceType);
elseif strcmp(problem(i).DistanceType,'EUC_2D')
problem(i).DistanceMatrix = network.HopfieldNetworkTSP.computeDistance(problem(i).Coordinates,problem(i).DistanceType);
elseif strcmp(problem(i).DistanceType,'CEIL_2D')
problem(i).DistanceMatrix = network.HopfieldNetworkTSP.computeDistance(problem(i).Coordinates,problem(i).DistanceType);
elseif strcmp(problem(i).DistanceType,'ATT')
problem(i).DistanceMatrix = network.HopfieldNetworkTSP.computeDistance(problem(i).Coordinates,problem(i).DistanceType);
elseif strcmp(problem(i).DistanceType,'EUC')
problem(i).DistanceMatrix = network.HopfieldNetworkTSP.computeDistance(problem(i).Coordinates,problem(i).DistanceType);
end
end
end
Expand Down

0 comments on commit e171753

Please sign in to comment.