function varargout = gensds(varargin)
% GENSDS M-file for gensds.fig
%      GENSDS, by itself, creates a new GENSDS or raises the existing
%      singleton*.
%
%      H = GENSDS returns the handle to a new GENSDS or the handle to
%      the existing singleton*.
%
%      GENSDS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GENSDS.M with the given input arguments.
%
%      GENSDS('Property','Value',...) creates a new GENSDS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gensds_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gensds_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gensds

% Last Modified by GUIDE v2.5 19-Dec-2003 02:45:59

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gensds_OpeningFcn, ...
                   'gui_OutputFcn',  @gensds_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before gensds is made visible.
function gensds_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to gensds (see VARARGIN)

% Choose default command line output for gensds
handles.output = hObject;

n = str2double(get(handles.nval,'String'));
Nprime = str2double(get(handles.Nprimeval,'String'));
r = str2double(get(handles.rvalue,'String'));
%t = str2double(get(handles.t,'String'));
buildTestSet(n,floor(n/10));

[aSpace,storage,C_SetSizes] = createMemory(n,Nprime,r,'testSet');
set(handles.fitnessN,'String', num2str(fitness(aSpace)/n));

handles.addressSpace = aSpace;
handles.storageCells = storage;
handles.C_Sets = C_SetSizes;
handles.n = n;
handles.r = r;
handles.Nprime = Nprime;
%handles.t = t;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes gensds wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = gensds_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes during object creation, after setting all properties.
function nval_CreateFcn(hObject, eventdata, handles)
% hObject    handle to nval (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function nval_Callback(hObject, eventdata, handles)
% hObject    handle to nval (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of nval as text
%        str2double(get(hObject,'String')) returns contents of nval as a double


% --- Executes during object creation, after setting all properties.
function Nprimeval_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Nprimeval (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function Nprimeval_Callback(hObject, eventdata, handles)
% hObject    handle to Nprimeval (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of Nprimeval as text
%        str2double(get(hObject,'String')) returns contents of Nprimeval as a double


% --- Executes on button press in ResetSysParam.
function ResetSysParam_Callback(hObject, eventdata, handles)
% hObject    handle to ResetSysParam (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.output = hObject;

n = str2double(get(handles.nval,'String'));
Nprime = str2double(get(handles.Nprimeval,'String'));
r = str2double(get(handles.rvalue,'String'));
%t = str2double(get(handles.t,'String'));
buildTestSet(n,floor(n/10));

[aSpace,storage,C_SetSizes] = createMemory(n,Nprime,r,'testSet');
set(handles.fitnessN,'String', num2str(fitness(aSpace)/n));
handles.addressSpace = aSpace;
handles.storageCells = storage;
handles.C_Sets = C_SetSizes;
handles.n = n;
handles.r = r;
handles.Nprime = Nprime;

% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function rvalue_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rvalue (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function rvalue_Callback(hObject, eventdata, handles)
% hObject    handle to rvalue (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of rvalue as text
%        str2double(get(hObject,'String')) returns contents of rvalue as a double


% --- Executes on button press in bw.
function bw_Callback(hObject, eventdata, handles)
% hObject    handle to bw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of bw


% --- Executes during object creation, after setting all properties.
function fitnessN_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fitnessN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function fitnessN_Callback(hObject, eventdata, handles)
% hObject    handle to fitnessN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of fitnessN as text
%        str2double(get(hObject,'String')) returns contents of fitnessN as a double



% --- Executes during object creation, after setting all properties.
function testSet_CreateFcn(hObject, eventdata, handles)
% hObject    handle to testSet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function testSet_Callback(hObject, eventdata, handles)
% hObject    handle to testSet (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of testSet as text
%        str2double(get(hObject,'String')) returns contents of testSet as a double


% --- Executes on button press in pcol.
function pcol_Callback(hObject, eventdata, handles)
% hObject    handle to pcol (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fidinput = fopen('testSet');
if get(handles.randomV,'Value') == 0
   
   z = fscanf(fidinput,'%g',[1,handles.n]);
   
else
    z = floor(2*rand(1,handles.n));
end
%z = reshape(flipud(rot90(A)),1,100);  



handles.orig = z;

for i=1:6

     zprime = zeros(1,handles.n);
     DS = 0;
     distN = zeros(1,handles.Nprime);
     for j = 1:handles.Nprime
        dist(j) = hamdist(handles.addressSpace(j,1:handles.n),z);
     end

     S = find(dist - handles.r<0);
     for k=1:length(S)
       zprime = zprime + handles.storageCells(S(k),1:handles.n);
       DS = DS + handles.C_Sets(S(k));
    end
       
       zprime = floor((1.999999999/DS)*zprime);
       z = zprime;
  end
handles.zprime = z;

%refresh(handles.figure1);
%set(handles.('checkbox101'),'Value',1)

guidata(hObject,handles);

picture(handles.orig,handles.zprime,handles.n);

function picture(X,Y,n)

n = floor(sqrt(n));
%x = reshape(X,n,n);
%Y = reshape(Y,n,n);
%reshape(flipud(rot90(A)),1,100)
A = zeros(n+1);
B = zeros(n+1);

for i = 0:n-1;
        A(i+2,1:n) = X(i*n+1:i*n+n);
        B(i+2,1:n) = Y(i*n+1:i*n+n);
end

axis('ij')
axis('equal')

subplot(2,2,3), pcolor(flipud(A))
subplot(2,2,4), pcolor(flipud(B))

%colormap(gray(2))


% --- Executes during object creation, after setting all properties.
function tSize_CreateFcn(hObject, eventdata, handles)
% hObject    handle to tSize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function tSize_Callback(hObject, eventdata, handles)
% hObject    handle to tSize (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of tSize as text
%        str2double(get(hObject,'String')) returns contents of tSize as a double


% --- Executes during object creation, after setting all properties.
function t_CreateFcn(hObject, eventdata, handles)
% hObject    handle to t (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function t_Callback(hObject, eventdata, handles)
% hObject    handle to t (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of t as text
%        str2double(get(hObject,'String')) returns contents of t as a double


% --- Executes on button press in randomV.
function randomV_Callback(hObject, eventdata, handles)
% hObject    handle to randomV (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of randomV


