Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…annealing-iiith into testing
  • Loading branch information
balamma committed Oct 22, 2021
2 parents 3c3e185 + 41442fe commit e3b24c9
Show file tree
Hide file tree
Showing 1,393 changed files with 235,603 additions and 41 deletions.
55 changes: 55 additions & 0 deletions experiment-descriptor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"unit-type": "lu",
"label": "",
"basedir": ".",
"units": [
{
"unit-type": "aim"
},
{
"target": "theory.html",
"source": "theory.md",
"label": "Theory",
"unit-type": "task",
"content-type": "text"
},

{
"target": "procedure.html",
"source": "procedure.md",
"label": "Procedure",
"unit-type": "task",
"content-type": "text"
},
{
"target": "simulation.html",
"source": "simulation/index.html",
"label": "Simulation",
"unit-type": "task",
"content-type": "simulation"
},
{
"target": "observations.html",
"source": "observations.md",
"label": "Observations",
"unit-type": "task",
"content-type": "text"
},
{
"target": "assignment.html",
"source": "assignment.md",
"label": "Assignment",
"unit-type": "task",
"content-type": "text"
},

{
"target": "references.html",
"source": "references.md",
"label": "References",
"unit-type": "task",
"content-type": "text"
}
]
}

2 changes: 1 addition & 1 deletion experiment/experiment-name.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
## Deterministic, stochastic and mean-field annealing of Hopfield models
## Deterministic, Stochastic and Mean-field Annealing of Hopfield Models
19 changes: 0 additions & 19 deletions experiment/posttest.json

This file was deleted.

19 changes: 0 additions & 19 deletions experiment/pretest.json

This file was deleted.

12 changes: 12 additions & 0 deletions experiment/simulation/backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ubuntu:18.04

RUN apt update && \
apt install -y octave python3 python3-pip

RUN octave --eval 'pkg install -forge nnet'

COPY . /

RUN pip3 install -r requirements.txt

CMD python3 server.py
48 changes: 48 additions & 0 deletions experiment/simulation/backend/exp_weighted_matching/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from utils import *

EXP_NAME = "weighted_matching"


def init_graph(nodeloc, token):
clear_files(10, token)

res = run_octave(EXP_NAME, "init_wmp", nodeloc, token)

if res:
return res

images = read_all_pngs(EXP_NAME, token, 1)

return jsonify(images=images, result="")


def start_annealing(method, nodeloc, token):
res = run_octave(EXP_NAME, f"anneal_wmp_{method}", nodeloc, token)

if res:
return res

images = read_all_pngs(EXP_NAME, token)
result = read_result(EXP_NAME, token)

return jsonify(images=images, result=result)


num_map = {
"det": "det",
"mean": "mean",
"sto": "sto",
}


def runexp(data, part_num):
nodeloc = data.get("nodeloc")
token = data.get("token")

if data.get("annealflag") == "1":
return start_annealing(num_map[part_num], nodeloc, token)

if data.get("initflag") == "1":
return init_graph(nodeloc, token)

return jsonify(success=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
function finalstate = anneal_wmp_det(points, filename, initstate)
%%%%%%%%%
% Ex1: points=[1 1;3 3;0 4;2 6];
% The network yields solution only for certain initial states. For other
% init states, the network oscillates or gets stuck at some local minima.
% validinitstates=[5 6 7 9 10 11 13 14 15 21 25 29 37 41 45];
%%%%%%%%%

fid=fopen(strcat(filename, "-result.txt"),'w');

N=length(points);
M=nchoosek(N,2);
if(~exist('initstate'))
initstate=(rand(M,1)-0.5)>0;
end
initstate=initstate(:);

fprintf(fid,'Initial state:\n');
fprintf(fid,'%d ',initstate);
fprintf(fid,'\n');

d=zeros(N,N);
unit=[];
th=[];
k=1;
for i=1:N
for j=i+1:N
d(i,j)=norm(points(i,:)-points(j,:));
unit(k,:)=[i j];
th=[th; d(i,j)];
k=k+1;
end
end
d=d+d';


% The nodes are (i,j): 12, 13, 14, 23, 24, 34

gamma=max(max(d))/(M/2);
W = zeros(M,M);

for i=1:M
for j=i+1:M
if(any(~[unit(i,:)-unit(j,:) unit(i,[2 1])-unit(j,:)]))
W(i,j)=-gamma;
end
end
end

W = W + W';

th=th-gamma;

% Random initialization
%state = (rand(M,1)-0.5)>0;
%state=[0 1 1 0 0 0]'
state=initstate;
disp(state)
tempstate = zeros(M,1);
stateseq=[zeros(M,2) state];
k=3;
while(norm(tempstate-state) ~= 0 & norm(stateseq(:,k)-stateseq(:,k-2))~=0 | k<10)
tempstate=state;
state=W*state+th;
%disp(state')
state=state>0;
%disp(state')
stateseq=[stateseq state];
k=k+1;
%pause;
end
finalstate=state;

disp(stateseq);
fprintf(fid,'\nOutput state of the nodes after each update:\n');
for i=3:size(stateseq,2)
fprintf(fid,'%d ',stateseq(:,i));
fprintf(fid,'\n');
end


g=zeros(N,N);
ndx=unit(find(finalstate),:);
for i=1:size(ndx,1)
g([ndx(i,1)],[ndx(i,2)])=1;
end
%xyminmax = min_max(points');

drawgraph(g,points);

print(strcat(filename, "-2.png"))

fclose(fid);

return;
113 changes: 113 additions & 0 deletions experiment/simulation/backend/exp_weighted_matching/anneal_wmp_mean.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
function FinalState = anneal_wmp_mean(points, filename, InitState)
%%%%%%%%%
% Ex1: points=[1 1;3 3;0 4;2 6];
% The network yields solution only for certain initial states. For other
% init states, the network oscillates or gets stuck at some local minima.
% validinitstates=[5 6 7 9 10 11 13 14 15 21 25 29 37 41 45];
%%%%%%%%%

fid=fopen(strcat(filename, "-result.txt"),'w');

%points=[1 1;3 3;0 4;2 6];
N=length(points);
M=nchoosek(N,2);

if(~exist('InitState'))
InitState=rand(M,1);
end

fprintf(fid,'Initial state:\n');
fprintf(fid,'%d ',InitState);
fprintf(fid,'\n');
fprintf(fid,'\n');
fprintf(fid,'\nAvg. output state of the nodes after annealing for each temperature:\n');

d=zeros(N,N);
unit=[];
th=[];
k=1;
for i=1:N
for j=i+1:N
d(i,j)=norm(points(i,:)-points(j,:));
unit(k,:)=[i j];
th=[th; d(i,j)];
k=k+1;
end
end
d=d+d';


% The nodes are (i,j): 12, 13, 14, 23, 24, 34

gamma=max(max(d))/(M/2);
W = zeros(M,M);

for i=1:M
for j=i+1:M
if(any(~[unit(i,:)-unit(j,:) unit(i,[2 1])-unit(j,:)]))
W(i,j)=-gamma;
end
end
end

W = W + W';

th=th-gamma;
th=th(:);

% Random initialization
%State = (rand(M,1)-0.5)>0;
%state=[0 1 1 0 0 0]'

State=InitState(:);
TempState=zeros(M,1);
disp(State')

T=1;
dT=0.01;
i=1;
while(T>0)
%TempState=rand(M,1);
T=T-dT;
while(norm(State-TempState) >= 0.00001 & i<100)
TempState = State;
State = (W*State + th);
State = tanh(State/T);
i=i+1;
%disp(State')
%disp(norm(State-TempState))
%pause;
end
disp(State')

fprintf(fid,'%4.2f :',T);
fprintf(fid,'%d ',State>0);
fprintf(fid,'\n');

end

[sval,spos]=sort(State,'descend');
FinalState=zeros(M,1);
FinalState(spos(1:floor(N/2)))=1;

disp(FinalState)
fprintf(fid,'\n');
fprintf(fid,'\nFinal output state of the nodes:\n');
fprintf(fid,'%d ',FinalState);
fprintf(fid,'\n');


g=zeros(N,N);
ndx=unit(find(FinalState),:);
for i=1:size(ndx,1)
g([ndx(i,1)],[ndx(i,2)])=1;
end
%xyminmax = min_max(points');

drawgraph(g,points);

print(strcat(filename, "-2.png"))

fclose(fid);

return;
Loading

0 comments on commit e3b24c9

Please sign in to comment.