Skip to content

Commit

Permalink
Adding main files for the Hopfield Network Toolbox
Browse files Browse the repository at this point in the history
This GitHub commit corresponds to commit no. 267 from legacy code
(v7.1).
  • Loading branch information
mathinking committed Nov 2, 2015
1 parent 44ee77b commit a63751f
Show file tree
Hide file tree
Showing 295 changed files with 594,052 additions and 0 deletions.
85 changes: 85 additions & 0 deletions +Test/test_hopfieldnetwork.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
classdef test_hopfieldnetwork < matlab.unittest.TestCase
%TESTTSPHOPFIELDNET Summary of this class goes here
% Detailed explanation goes here

methods (Test)
% [the name of the tested method]_[expected input / tested state]_[expected behavior]

% Checking hopfieldnet method createOptions
function createOptions_Unkwown_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('unknown',2),'hopfieldNetwork:unvalidSetting');
end

function createOptions_u0WrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('u0',uint8(2)),'hopfieldnetwork:invalid_datatype');
end
function createOptions_hwResourcesWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('hwResources',0),'hopfieldnetwork:invalid_datatype');
end
function createOptions_maxIterWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('maxIter',true),'hopfieldnetwork:invalid_datatype');
end
function createOptions_epsWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('e',{7}),'hopfieldnetwork:invalid_datatype');
end
function createOptions_qWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('q',single(0.05)),'hopfieldnetwork:invalid_datatype');
end
function createOptions_R_ITERWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('R_ITER',uint32(20)),'hopfieldnetwork:invalid_datatype');
end
function createOptions_dtWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('dt',single(0.01)),'hopfieldnetwork:invalid_datatype');
end
function createOptions_showCommandLineWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('showCommandLine',1),'hopfieldnetwork:invalid_datatype');
end
function createOptions_transferFcnWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('transferFcn',{'exponential'}),'hopfieldnetwork:invalid_datatype');
end
function createOptions_loggingVWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('loggingV',1),'hopfieldnetwork:invalid_datatype');
end
function createOptions_viewConvergenceWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('viewConvergence',1),'hopfieldnetwork:invalid_datatype');
end
function createOptions_viewConvergenceSpeedWrongType_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('viewConvergenceSpeed',single(0.2)),'hopfieldnetwork:invalid_datatype');
end

function createOptions_u0WrongValue_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('u0',0),'hopfieldnetwork:invalid_value');
end
function createOptions_hwResourcesWrongValue_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('hwResources','FPGA'),'hopfieldnetwork:invalid_value');
end
function createOptions_maxIterWrongValue_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('maxIter',0),'hopfieldnetwork:invalid_value');
end
function createOptions_eWrongValue_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('e',50),'hopfieldnetwork:invalid_value');
end
function createOptions_qWrongValue_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('q',0),'hopfieldnetwork:invalid_value');
end
function createOptions_R_ITERWrongValue_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('R_ITER',-1),'hopfieldnetwork:invalid_value');
end
function createOptions_dtWrongValue_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('dt',0),'hopfieldnetwork:invalid_value');
end
function createOptions_transferFcnWrongValue_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('transferFcn','linear'),'hopfieldnetwork:invalid_value');
end
function createOptions_viewConvergenceSpeedWrongValue_Errors(testCase)
verifyError(testCase,@()hopfieldnetwork.createOptions('viewConvergenceSpeed',1),'hopfieldnetwork:invalid_value');
end

function createOptions_ValidProperty_Works(testCase)
u0 = 0.35;
options = hopfieldnetwork.createOptions('u0',u0);
verifyEqual(testCase,options.setting.u0,u0);
end
end

end
266 changes: 266 additions & 0 deletions +Test/test_tsphopfieldnet.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
classdef test_tsphopfieldnet < matlab.unittest.TestCase
%TEST_TSPHOPFIELDNET Summary of this class goes here
% Detailed explanation goes here

methods (Test)
% [the name of the tested method]_[expected input / tested state]_[expected behavior]

% Verify inputs
% Checking tsphopfieldnet input arguments
function tsphopfieldnet_IncorrecNumberInputs_Errors(testCase)
verifyError(testCase,@()tsphopfieldnet(),'tsphopfieldnet:IncorrectInputArguments');
end
function tsphopfieldnet_networkSizeNotDouble_Errors(testCase)
verifyError(testCase,@()tsphopfieldnet(uint8(10),0.1),'hopfieldnetwork:invalid_datatype');
end
function tsphopfieldnet_networkSizeTooSmall_Errors(testCase)
verifyError(testCase,@()tsphopfieldnet(1,0.1),'tsphopfieldnet:invalid_value');
end
function tsphopfieldnet_CNotDouble_Errors(testCase)
verifyError(testCase,@()tsphopfieldnet(10,single(0.1)),'tsphopfieldnet:invalid_datatype');
end
function tsphopfieldnet_CNotPositive_Errors(testCase)
verifyError(testCase,@()tsphopfieldnet(10,0),'tsphopfieldnet:invalid_value');
end
function tsphopfieldnet_optionsNotStruct_Errors(testCase)
verifyError(testCase,@()tsphopfieldnet(10,0.1,'options'),'hopfieldnetwork:invalid_datatype');
end

function createOptions_transferFcnIssatlin_invTransferFcnIsinvsatlin(testCase)
options = tsphopfieldnet.createOptions('u0',0.5,'transferFcn','satlin');
networkSize = 10;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
transferFcn = getSetting(net,'transferFcn');
invTransferFcn = getSetting(net,'invTransferFcn');
value = 0.1234;
verifyEqual(testCase, invTransferFcn(transferFcn(value)), value, 'AbsTol', power(10, -1 * getSetting(net,'e')));
end

function createOptions_invTransferFcnIsinvsatlin_transferFcnIssatlin(testCase)
options = tsphopfieldnet.createOptions('u0',0.5,'invTransferFcn','invsatlin');
networkSize = 10;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
invTransferFcn = getSetting(net,'invTransferFcn');
transferFcn = getSetting(net,'transferFcn');
value = 0.1234;
verifyEqual(testCase, transferFcn(invTransferFcn(value)), value, 'AbsTol', power(10, -1 * getSetting(net,'e')));
end

function createOptions_transferFcnIstanh_invTransferFcnIsatanh(testCase)
options = tsphopfieldnet.createOptions('u0',0.5,'transferFcn','tanh');
networkSize = 10;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
transferFcn = getSetting(net,'transferFcn');
invTransferFcn = getSetting(net,'invTransferFcn');
value = 0.1234;
verifyEqual(testCase, invTransferFcn(transferFcn(value)), value, 'AbsTol', power(10, -1 * getSetting(net,'e')));
end

function createOptions_invTransferFcnIsatanh_transferFcnIstanh(testCase)
options = tsphopfieldnet.createOptions('u0',0.5,'invTransferFcn','atanh');
networkSize = 10;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
invTransferFcn = getSetting(net,'invTransferFcn');
transferFcn = getSetting(net,'transferFcn');
value = 0.1234;
verifyEqual(testCase, transferFcn(invTransferFcn(value)), value, 'AbsTol', power(10, -1 * getSetting(net,'e')));
end

function createOptions_distanceMatrixNotDouble_Errors(testCase)
verifyError(testCase, @()tsphopfieldnet.createOptions('d',rand(4,'single')),'tsphopfieldnet:invalid_datatype');
end
function createOptions_distanceMatrixNotSquare_Errors(testCase)
verifyError(testCase, @()tsphopfieldnet.createOptions('d',rand(3,4)),'tsphopfieldnet:invalid_value');
end
function tsphopfieldnet_distanceMatrixNotSameSizeNetwork_Errors(testCase)
options = tsphopfieldnet.createOptions('d',rand(4));
networkSize = 10;
C = 0.1;
verifyError(testCase, @()tsphopfieldnet(networkSize, C, options),'tsphopfieldnet:invalid_value');
end

function createOptions_coordsMatrixNotDouble_Errors(testCase)
verifyError(testCase, @()tsphopfieldnet.createOptions('coords',rand(4,2,'single')),'tsphopfieldnet:invalid_datatype');
end
function createOptions_coordsMatrixNotTwoColumns_Errors(testCase)
verifyError(testCase, @()tsphopfieldnet.createOptions('coords',rand(4,3)),'tsphopfieldnet:invalid_value');
end
function setOptions_coordsMatrixNotNetworkSize_Errors(testCase)
options = tsphopfieldnet.createOptions('coords',rand(4,2));
networkSize = 10;
C = 0.1;
verifyError(testCase, @()tsphopfieldnet(networkSize, C, options),'tsphopfieldnet:invalid_value');
end
function setOptions_cityNamesNotCell_Errors(testCase)
verifyError(testCase, @()tsphopfieldnet.createOptions('names','Madrid'),'tsphopfieldnet:invalid_datatype');
end
function setOptions_cityNamesNotCellArrayOfChars_Errors(testCase)
options = tsphopfieldnet.createOptions('names',{1,'Madrid','2'});
networkSize = 3;
C = 0.1;
verifyError(testCase, @()tsphopfieldnet(networkSize, C, options),'tsphopfieldnet:invalid_value');
end
function setOptions_cityNamesNotNetworkSize_Errors(testCase)
options = tsphopfieldnet.createOptions('names',{'London','Madrid','Berlin','Paris'});
networkSize = 3;
C = 0.1;
verifyError(testCase, @()tsphopfieldnet(networkSize, C, options),'tsphopfieldnet:invalid_value');
end

% TODO tests to ensure that fixedCities and startinPos have the same length

function createOptions_trainFcnNotChar_Errors(testCase)
verifyError(testCase, @()tsphopfieldnet.createOptions('trainFcn',{'trainty'}),'tsphopfieldnet:invalid_datatype');
end
function createOptions_trainFcnNotValid_Errors(testCase)
verifyError(testCase, @()tsphopfieldnet.createOptions('trainFcn','trainyt'),'tsphopfieldnet:invalid_value');
end
function createOptions_simFcnNotChar_Errors(testCase)
verifyError(testCase, @()tsphopfieldnet.createOptions('simFcn',{'talavan-yanez'}),'tsphopfieldnet:invalid_datatype');
end
function createOptions_simFcnNotValid_Errors(testCase)
verifyError(testCase, @()tsphopfieldnet.createOptions('simFcn','yanez-talavan'),'tsphopfieldnet:invalid_value');
end

function train_trainFcnIsTrainty_WorksFine(testCase)
import matlab.unittest.constraints.IsEqualTo;
import matlab.unittest.constraints.AbsoluteTolerance;

networkSize = 6;
C = 0.1;
options = tsphopfieldnet.createOptions('trainFcn','trainty');
net = tsphopfieldnet(networkSize, C, options);
trainParamExpected = getTrainParam(net);
trainParamExpected.A = 2.6;
trainParamExpected.B = 3.1;
trainParamExpected.D = 1;
trainParamExpected.Np = 36;
trainParamExpected.dL = 0.5;
trainParamExpected.dU = 1;
trainParamExpected.dUaux = 2;
trainParamExpected.K = 0;
trainParamExpected.rho = 0.5;
trainParamExpected = orderfields(trainParamExpected);
train(net);
trainParam = getTrainParam(net);
verifyThat(testCase, trainParam, IsEqualTo(trainParamExpected, 'Within', AbsoluteTolerance(power(10, -1 * getSetting(net,'e')))));
end
function train_trainFcnIsNotTrainty_Errors(testCase)
networkSize = 6;
C = 0.1;
options = tsphopfieldnet.createOptions('trainFcn','trainty');
options.trainFcn = 'nottrainty';
net = tsphopfieldnet(networkSize, C, options);
verifyError(testCase, @()train(net),'tsphopfieldnet:unvalidTrainFcn');
end

function sim_simTalavanYanezOutputWithPolygon_hasOne1perRow(testCase)
rng(2);
options = tsphopfieldnet.createOptions('simFcn','talavan-yanez');
networkSize = 6;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
train(net);
V = sim(net);
verifyTrue(testCase, all(sum(V,2) == 1));
end
function sim_simTalavanYanezOutputWithPolygon_hasOne1perCol(testCase)
rng(2);
options = tsphopfieldnet.createOptions('simFcn','talavan-yanez');
networkSize = 6;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
train(net);
V = sim(net);
verifyTrue(testCase, all(sum(V,1) == 1));
end
function sim_simTalavanYanezOutputWithPolygon_sumsN(testCase)
rng(2);
options = tsphopfieldnet.createOptions('simFcn','talavan-yanez');
networkSize = 6;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
train(net);
V = sim(net);
verifyEqual(testCase, sum(sum(V)), networkSize);
end

function sim_simTalavanYanezOutputWithBerlin52_hasOne1perRow(testCase)
rng(2);
problem = tsplib({'berlin52'});
options = tsphopfieldnet.createOptions('coords',problem.coords,'type',problem.type,'simFcn','talavan-yanez');
networkSize = problem.nCities;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
train(net);
V = sim(net);
verifyTrue(testCase, all(sum(V,2) == 1));
end
function sim_simTalavanYanezOutputWithBerlin52_hasOne1perCol(testCase)
rng(2);
problem = tsplib({'berlin52'});
options = tsphopfieldnet.createOptions('coords',problem.coords,'type',problem.type,'simFcn','talavan-yanez');
networkSize = problem.nCities;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
train(net);
V = sim(net);
verifyTrue(testCase, all(sum(V,1) == 1));
end
function sim_simTalavanYanezOutputWithBerlin52_sumsN(testCase)
rng(2);
problem = tsplib({'berlin52'});
options = tsphopfieldnet.createOptions('coords',problem.coords,'type',problem.type,'simFcn','talavan-yanez');
networkSize = problem.nCities;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
train(net);
V = sim(net);
verifyEqual(testCase, sum(sum(V)), networkSize);
end

function reinit_netObjectAlreadySimulated_WorksFine(testCase)
import matlab.unittest.constraints.IsEqualTo;

networkSize = 6;
C = 0.1;
net = tsphopfieldnet(networkSize, C);
expectedResults = getResults(net);
train(net);
sim(net);
reinit(net);
results = getResults(net);
verifyThat(testCase, results, IsEqualTo(expectedResults));
end

function saddle_simFcnNotTalavanYanez_saddleGetsComputed(testCase)
networkSize = 10;
C = 0.0001;
options = tsphopfieldnet.createOptions('simFcn', 'divide-conquer');
net = tsphopfieldnet(networkSize, C, options);
simFcnExpected = getSimFcn(net);
saddle(net);
verifyEqual(testCase, getSimFcn(net), simFcnExpected);
end
function saddle_computingSaddle_allColumnsEqual(testCase)
import matlab.unittest.constraints.IsEqualTo;
import matlab.unittest.constraints.AbsoluteTolerance;

problem = tsplib({'berlin52'});
options = tsphopfieldnet.createOptions('coords',problem.coords,'type',problem.type,'simFcn','talavan-yanez');
networkSize = problem.nCities;
C = 0.1;
net = tsphopfieldnet(networkSize, C, options);
train(net);
V = saddle(net);
for i = 2:networkSize
verifyThat(testCase, V(:,1), IsEqualTo(V(:,i), 'Within', AbsoluteTolerance(power(10, -1 * getSetting(net,'e')))));
end
end

end
end
32 changes: 32 additions & 0 deletions @generichopfieldnet/addDefaultOptionValues.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function net = addDefaultOptionValues(net, options)

% if isempty(options.cities.names)
% net.cities.names = tsphopfieldnet.cityTextGeneration(net.trainParam.N);
% end
%
% if isempty(options.cities.type)
% net.cities.type = 'EUC';
% end
%
% if isempty(options.cities.coords) && isempty(options.cities.d)
% net.cities.coords = tsphopfieldnet.polygonCoords(1, net.trainParam.N);
% end
%
% if isempty(options.cities.d)
% net.cities.d = [];
% end
%
% if isempty(options.cities.fixedCities)
% net.cities.fixedCities = {''};
% net.cities.startFixedCitiesIn = NaN;
% end

if isempty(options.trainFcn)
net.trainFcn = 'traingty';
end

if isempty(options.simFcn)
net.simFcn = 'talavan-yanez';
end

end
Loading

0 comments on commit a63751f

Please sign in to comment.