Skip to content

Commit

Permalink
Interface rename and code cleanup
Browse files Browse the repository at this point in the history
Renaming 'generichopfieldnet' to 'hopfieldnet' and cleaning unused code
in tsphopfieldnet.
  • Loading branch information
mathinking committed Sep 3, 2016
1 parent 6432b95 commit 189bec3
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 114 deletions.
22 changes: 11 additions & 11 deletions @hopfieldnet/generichopfieldnet.m → @hopfieldnet/hopfieldnet.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
classdef generichopfieldnet < hopfieldnetwork
%GENERICHOFIELDNETWORK Continuous Hopfield Network (CHN)
classdef hopfieldnet < hopfieldnetwork
%HOFIELDNETWORK Continuous Hopfield Network (CHN)

properties (GetAccess = public, SetAccess = public)
name = 'Continuous Hopfield Network (CHN)';
Expand All @@ -8,7 +8,7 @@
end

methods
function net = generichopfieldnet(P, q, A, b, Aeq, beq, options)
function net = hopfieldnet(P, q, A, b, Aeq, beq, options)
% Solving the GQKP
% min{1/2 * v' * P * v + q' * v}
% subject to
Expand All @@ -18,19 +18,19 @@
% v_{n+k} belongs to [0,1], k = 1,..., m1

if nargin < 6 || nargin > 7
error('generichopfieldnet:IncorrectInputArguments','Please provide proper input arguments to generichopfieldnet: P, q, R, b (and options)');
error('hopfieldnet:IncorrectInputArguments','Please provide proper input arguments to hopfieldnet: P, q, R, b (and options)');
else
% asserting inputs
assert(isa(P,'double'), 'generichopfieldnet:invalid_datatype', 'P must be double.');
assert(isa(q,'double'), 'generichopfieldnet:invalid_datatype', 'q must be double.');
assert(isa(A,'double'), 'generichopfieldnet:invalid_datatype', 'A must be double.');
assert(isa(b,'double'), 'generichopfieldnet:invalid_datatype', 'b must be double.');
assert(isa(Aeq,'double'), 'generichopfieldnet:invalid_datatype', 'Aeq must be double.');
assert(isa(beq,'double'), 'generichopfieldnet:invalid_datatype', 'beq must be double.');
assert(isa(P,'double'), 'hopfieldnet:invalid_datatype', 'P must be double.');
assert(isa(q,'double'), 'hopfieldnet:invalid_datatype', 'q must be double.');
assert(isa(A,'double'), 'hopfieldnet:invalid_datatype', 'A must be double.');
assert(isa(b,'double'), 'hopfieldnet:invalid_datatype', 'b must be double.');
assert(isa(Aeq,'double'), 'hopfieldnet:invalid_datatype', 'Aeq must be double.');
assert(isa(beq,'double'), 'hopfieldnet:invalid_datatype', 'beq must be double.');
end

if nargin < 7
options = generichopfieldnet.createOptions();
options = hopfieldnet.createOptions();
end

% Creating net object from parent class
Expand Down
6 changes: 3 additions & 3 deletions @hopfieldnet/isValidSetting.m
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
function isValidSetting(property,value)

invalidDataTypeID = 'generichopfieldnet:invalid_datatype';
invalidDataTypeID = 'hopfieldnet:invalid_datatype';

switch property
case {'trainFcn', 'simFcn'}
assert(isa(value,'char'), invalidDataTypeID, [property, ' must be char.']);
end

invalidValueID = 'generichopfieldnet:invalid_value';
invalidValueID = 'hopfieldnet:invalid_value';

switch property

case 'trainFcn'
assert(strcmp('traingty',value), invalidValueID, 'Training function (parametrization) must be ''traingty''');
assert(strcmp('traingty',value), invalidValueID, 'Training function (parametrization) must be ''trainty''');

case 'simFcn'
assert(strcmp('euler',value) || strcmp('talavan-yanez',value), invalidValueID, 'Simulation function must be ''euler'' or ''talavan-yanez''');
Expand Down
15 changes: 3 additions & 12 deletions @tsphopfieldnet/sim.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,14 @@
[net,V,~,iter] = simDivideConquer(net,V,U);
end

elseif strcmp(net.simFcn,'talavan-yanez-n')
if nargin < 2
[net,V,~,iter] = simTalavanYanezVarN(net);
else
[net,V,~,iter] = simTalavanYanezVarN(net,V,U);
end

else
error('tsphopfieldnet:UnknownsimFcn', 'Unknown training algorithm');
end

if ~isempty(net.cities.fixedCities{1})
net.cities.d = aux_d;
end

if ~any(strcmp({ST.name},'saddle'))
net = computeTour(net,V,iter);
net.results.compTime = toc(timeID);
Expand Down Expand Up @@ -460,7 +454,7 @@
iter = net.results.itersReached + netPhase2.results.itersReached - 1;

if plotPhases
if isempty(net.results.tourLength);
if isempty(net.results.tourLength); %#ok<UNRCH>
init(net);
net = computeTour(net,V,iter); % Needed for plot phase1 + phase2 to output correctly
else
Expand Down Expand Up @@ -699,10 +693,7 @@
(net.trainParam.A + net.trainParam.B) * V - ...
net.trainParam.C * sumV - ...
net.trainParam.D * dVpVn;
if isfield(net.trainParam,'F')
TV = TV + net.trainParam.F * (net.cities.d * bsxfun(@minus, sumVrow, V) - dVpVn);
% net.trainParam.F * net.cities.d * (repmat(sumVrow,1,net.trainParam.N) - V - V(:,deltaPrev) - V(:,deltaNext));
end

else % HAY QUE TRABAJAR ESTO
reOrder = reshape(flipud(reshape(1:2*net.trainParam.K,2,net.trainParam.K)),2*net.trainParam.K,1);
termA = repmat(sumVrow,1,size(V,2)) - V;
Expand Down
24 changes: 8 additions & 16 deletions @tsphopfieldnet/train.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,21 @@
net.trainParam.D = 1/net.trainParam.dU;
net.trainParam.rho = net.trainParam.dL/net.trainParam.dU;

if ~isfield(net.trainParam, 'F')
net.trainParam.Np = net.trainParam.N - net.trainParam.K + 3 / net.trainParam.C;
net.trainParam.Np = net.trainParam.N - net.trainParam.K + 3 / net.trainParam.C;

if net.trainParam.K == 0
net.trainParam.B = 3 + net.trainParam.C;
net.trainParam.A = net.trainParam.B - net.trainParam.rho;
else
net.trainParam.A = 3 + net.trainParam.C;
net.trainParam.B = net.trainParam.A + net.trainParam.rho;
end
if net.trainParam.K == 0
net.trainParam.B = 3 + net.trainParam.C;
net.trainParam.A = net.trainParam.B - net.trainParam.rho;
else
net.trainParam.Np = net.trainParam.N - 1 + 3 / net.trainParam.C - (net.trainParam.N - 4)*(net.trainParam.F/net.trainParam.C)*net.trainParam.dL + 1;
net.trainParam.A = 3 + net.trainParam.C;
net.trainParam.B = net.trainParam.A + net.trainParam.rho;
end

net.trainParam.B = 3 + net.trainParam.F*(net.trainParam.dU*(net.trainParam.N - 2) - net.trainParam.dL*(net.trainParam.N - 4)) - net.trainParam.C + 2*net.trainParam.C;
net.trainParam.A = net.trainParam.B - net.trainParam.D*net.trainParam.dL - net.trainParam.F*net.trainParam.dU;

end

net.trainParam = orderfields(net.trainParam);
else
net.trainFcn = 'trainty';
error('tsphopfieldnet:unvalidTrainFcn', ['Not Valid Training Function. Setting to ''trainty''. ',...
'Please, train the network using train(net)']);
end

end
end
25 changes: 8 additions & 17 deletions @tsphopfieldnet/tsphopfieldnet.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,21 @@
end

methods (Sealed = true)
function net = tsphopfieldnet(networkSize, networkParameters, options)
function net = tsphopfieldnet(networkSize, networkParameter, options)

if nargin < 2 || nargin > 3
error('tsphopfieldnet:IncorrectInputArguments','Please provide proper input arguments to tsphopfieldnet: networkSize, C (and options)');
else
% networkSize assertion done in hopfieldnetwork constructor
assert(networkSize > 1, 'tsphopfieldnet:invalid_value', '''networkSize'' must be greater than 2.');

assert(numel(networkParameters) > 0 & numel(networkParameters) <= 2, 'networkParameters must be 1 or 2: ''C'' or ''C and F''.')
assert(numel(networkParameter) == 1, 'networkParameters must be 1: ''C''.')

C = networkParameters(1);
C = networkParameter(1);
assert(isa(C,'double'), 'tsphopfieldnet:invalid_datatype', '''C'' must be double.');
assert(C > 0, 'tsphopfieldnet:invalid_value', '''C'' must be greater than 0.');

% If using parameter F in the model
if numel(networkParameters) > 1
F = networkParameters(2);
assert(isa(F,'double'), 'tsphopfieldnet:invalid_datatype', [F, ' must be double.']);
assert(F >= 0, 'tsphopfieldnet:invalid_value', '''F'' must be greater than or equal to 0.');
end
end
end

if nargin < 3
options = tsphopfieldnet.createOptions();
Expand All @@ -53,13 +47,10 @@
net.cities = orderfields(net.cities);

net.trainParam.C = C;
if exist('F','var')
net.trainParam.F = F;
end

% if size(net.cities.coords,1) ~= net.trainParam.N
% error('Mismatch between number of coordinates, distance matrix and neurons');
% end
if size(net.cities.coords,1) ~= net.trainParam.N
error('Mismatch between number of coordinates, distance matrix and neurons');
end

% Initialize results;
net = init(net);
Expand All @@ -75,6 +66,7 @@
net = verifyIfValidSubtours(net);
[net,V,U] = fixedCities(net);
thisCity = city(net,cityPosition);
[modifiedDistance,Ng] = neighbourDistance(net, tau_or_p);
end

methods (Hidden = true, Access = protected)
Expand All @@ -89,7 +81,6 @@
p = modulo(m,n);
loggingV(iter,V,dU);
chain = createChain(cities);
[modifiedDistance,Ng] = neighbourDistance(net, tau_or_p);
end

methods (Static = true, Hidden = true, Access = protected)
Expand Down
34 changes: 17 additions & 17 deletions Hopfield Net TSP solver.prj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<deployment-project plugin="plugin.apptool" plugin-version="1.0">
<configuration build-checksum="1730626426" file="E:\Users\Lucas\Documents\GitHub\HopfieldNetworkToolbox\Hopfield Net TSP solver.prj" location="E:\Users\Lucas\Documents\GitHub\HopfieldNetworkToolbox" name="Hopfield Net TSP solver" target="target.mlapps" target-name="Package App">
<configuration build-checksum="1730626426" file="E:\Users\Lucas\Documents\Math\PhD\sandbox\hopfield_network\trunk\code\Hopfield Net TSP solver.prj" location="E:\Users\Lucas\Documents\Math\PhD\sandbox\hopfield_network\trunk\code" name="Hopfield Net TSP solver" target="target.mlapps" target-name="Package App">
<param.appname>Hopfield Net TSP solver</param.appname>
<param.authnamewatermark>Lucas García Rodríguez</param.authnamewatermark>
<param.email>[email protected]</param.email>
Expand All @@ -26,11 +26,11 @@ Find more information in the documentation of the toolbox or in the &lt;a href="
<item>19</item>
</param.products.id>
<param.products.version>
<item>8.6</item>
<item>10.1</item>
<item>9.0</item>
<item>10.2</item>
</param.products.version>
<param.platforms />
<param.output>E:\Users\Lucas\Documents\GitHub\HopfieldNetworkToolbox</param.output>
<param.output>E:\Users\Lucas\Documents\Math\PhD\sandbox\hopfield_network\trunk\code</param.output>
<param.guid>d605def3-3010-4890-ac40-cf704be9aed1</param.guid>
<unset>
<param.platforms />
Expand Down Expand Up @@ -338,36 +338,36 @@ Find more information in the documentation of the toolbox or in the &lt;a href="
</fileset.resources>
<fileset.package />
<build-deliverables>
<file location="E:\Users\Lucas\Documents\GitHub" name="HopfieldNetworkToolbox" optional="false">E:\Users\Lucas\Documents\GitHub\HopfieldNetworkToolbox</file>
<file location="E:\Users\Lucas\Documents\Math\PhD\sandbox\hopfield_network\trunk" name="code" optional="false">E:\Users\Lucas\Documents\Math\PhD\sandbox\hopfield_network\trunk\code</file>
</build-deliverables>
<workflow />
<matlab>
<root>C:\Program Files\MATLAB\R2015b</root>
<root>C:\Program Files\MATLAB\R2016a</root>
<toolboxes>
<toolbox name="matlabhdlcoder" />
<toolbox name="fixedpoint" />
<toolbox name="embeddedcoder" />
<toolbox name="matlabcoder" />
<toolbox name="matlabhdlcoder" />
<toolbox name="embeddedcoder" />
</toolboxes>
<toolbox>
<matlabhdlcoder>
<fixedpoint>
<enabled>true</enabled>
</matlabhdlcoder>
</fixedpoint>
</toolbox>
<toolbox>
<fixedpoint>
<matlabcoder>
<enabled>true</enabled>
</fixedpoint>
</matlabcoder>
</toolbox>
<toolbox>
<embeddedcoder>
<matlabhdlcoder>
<enabled>true</enabled>
</embeddedcoder>
</matlabhdlcoder>
</toolbox>
<toolbox>
<matlabcoder>
<embeddedcoder>
<enabled>true</enabled>
</matlabcoder>
</embeddedcoder>
</toolbox>
</matlab>
<platform>
Expand All @@ -379,7 +379,7 @@ Find more information in the documentation of the toolbox or in the &lt;a href="
<vista>false</vista>
<linux>false</linux>
<solaris>false</solaris>
<osver>6.1</osver>
<osver>6.2</osver>
<os32>false</os32>
<os64>true</os64>
<arch>win64</arch>
Expand Down
Loading

0 comments on commit 189bec3

Please sign in to comment.