You can use a hash, e.g. obtained by FEX: DataHash or FEX: GetMD5. The latter is faster, but the hashing will not be the bottleneck of the code. tabuList = {};
hash8 = GetMD5(candidateSolution, 'Binary', 'uint8');
hash = char(typecast(hash8, 'uint16'));
if any(strcmp(hash, tabuList))
else
tabuList{end+1} = hash;
end
This can be improved by pre-allocating the tabuList or perhaps by a binary search. But usually strcmp is fast, because it stops the comparison at the first not matching character.
[EDITED] The comparison is slightly faster with using all 16 bits of the CHAR type. Now this takes 37 seconds on my R2016b/64 system for 40'000 candidates with about 10'000 repeated keys. The main time is spent in any(strcmp), such that the drawback of the omitted pre-allocation is less important that the advantage of having a shorter tabuList. With a dedicated C-Mex function this can be accelerated:
tic;
tabuList = cell(1, 4e4);
iList = 0;
for k = 1:4e4
c = randi([1, 4], 1, 8);
hash8 = GetMD5(c, 'Binary', 'uint8');
hash = char(typecast(hash8, 'uint16'));
if anyStrcmp8(hash, tabuList))
else
iList = iList + 1;
tabuList{iList} = hash;
end
end
disp(iList)
toc
And the Mex function anyStrcmp8.c:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
uint16_T *S, *CS;
size_t iC, nC, nS;
const mxArray *C, *aC;
// Get inputs:
S = (uint16_T *) mxGetData(prhs[0]);
nS = mxGetNumberOfElements(prhs[0]);
C = prhs[1];
nC = mxGetNumberOfElements(C);
// Loop over cell:
for (iC = 0; iC < nC; iC++) {
aC = mxGetCell(C, iC);
if (aC == NULL) { // Undeclared element reached:
plhs[0] = mxCreateLogicalScalar(false);
return;
}
CS = (uint16_T *) mxGetData(aC);
if (memcmp(S, CS, 8 * sizeof(uint16_T)) == 0) {
// Matching element found:
plhs[0] = mxCreateLogicalScalar(true);
return;
}
}
// No element found
plhs[0] = mxCreateLogicalScalar(false);
return;
}
This needs 13.6 sec for 40'000 keys with 25% repetitions.
2 Comments
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/356924-fast-data-structure-for-tabu-list#comment_484809
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/356924-fast-data-structure-for-tabu-list#comment_484809
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/356924-fast-data-structure-for-tabu-list#comment_484896
Direct link to this comment
https://in.mathworks.com/matlabcentral/answers/356924-fast-data-structure-for-tabu-list#comment_484896
Sign in to comment.