Skip to content

Commit

Permalink
Replaces the "callOutputAndPlotFcns" in the simplex with the new plot…
Browse files Browse the repository at this point in the history
… events (#173)

* Replaces the "callOutputAndPlotFcns" in the simplex with the new plot events

* Fixes drawMultiNest compile warning

* Changes the deafult value of updatePlotFreq and adds test
  • Loading branch information
StephenNneji authored Nov 28, 2023
1 parent 6f82749 commit 18db474
Show file tree
Hide file tree
Showing 33 changed files with 118 additions and 69 deletions.
12 changes: 10 additions & 2 deletions API/controlsClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
maxFunEvals = 10000
maxIter = 1000
updateFreq = -1
updatePlotFreq = -1
updatePlotFreq = 1

%(2) Differential Evolution
populationSize = 20
Expand Down Expand Up @@ -66,6 +66,14 @@
obj.display = validateOption(val, 'displayOptions', message).value;
end

function obj = set.updatePlotFreq(obj, val)
validateNumber(val, 'updatePlotFreq must be a number');
if val < 1
throw(exceptions.invalidValue('updatePlotFreq must be greater or equal to 1'));
end
obj.updatePlotFreq = val;
end

function obj = set.resamPars(obj,val)
if length(val) ~= 2
throw(exceptions.invalidValue('resamPars must have length of 2'));
Expand Down Expand Up @@ -428,7 +436,7 @@
defaultMaxFunEvals = 10000;
defaultMaxIter = 1000;
defaultUpdateFreq = -1;
defaultUpdatePlotFreq = -1;
defaultUpdatePlotFreq = 1;
defaultParallel = parallelOptions.Single.value;
defaultCalcSldDuringFit = false;
defaultResamPars = [0.9 50];
Expand Down
10 changes: 8 additions & 2 deletions API/events/triggerEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ function triggerEvent(eventType, data)
persistent notified;
persistent helper;

initialised = false;
hasPlotHandler = false;

if isempty(notified)
notified = false;
end
Expand All @@ -29,12 +32,15 @@ function triggerEvent(eventType, data)
coder.ceval('std::mem_fn(&eventHelper::init)', helper, path);
end

initialised = false;
initialised = coder.ceval('std::mem_fn(&eventHelper::isInitialised)', helper);
if initialised
if strcmpi(eventType, 'message')
coder.ceval('std::mem_fn(&eventHelper::sendMessage)', helper, [data,0]);
elseif strcmpi(eventType, 'plot')
hasPlotHandler = coder.ceval('std::mem_fn(&eventHelper::hasPlotHandler)', helper);
if ~hasPlotHandler
return;
end
result = data{1};
problemDef = data{3};
nContrast = length(result{1});
Expand Down Expand Up @@ -68,7 +74,7 @@ function triggerEvent(eventType, data)
else
% This avoids printing the error message multiple times during the optimization.
if ~notified
fprintf(2, "\neventManager library coult be loaded. Check that the dynamic library is present in the compile/events folder.\n");
fprintf(2, "\neventManager library could be loaded. Check that the dynamic library is present in the compile/events folder.\n");
notified = true;
end

Expand Down
10 changes: 9 additions & 1 deletion compile/events/eventHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class eventHelper
this->initialised = false;
}
};

void sendMessage(const char* msg)
{
auto sendMessage = library->get_function<void(const char*)>("sendMessage");
Expand All @@ -60,6 +60,14 @@ class eventHelper
return sendMessage(msg);

};

bool hasPlotHandler(void)
{
auto hasPlotHandler = library->get_function<bool(void)>("hasPlotHandler");

// pass the arguments to the function
return hasPlotHandler();
};

void updatePlot(int nContrast, double* reflect, double* nReflect, double* shiftedData, double* nShiftedData,
double* sldProfiles, double* nSldProfiles, double* layers, double* nLayers,
Expand Down
10 changes: 10 additions & 0 deletions compile/events/eventManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ LIB_EXPORT void clearListeners()
{
eventManager::get_instance()->clear();
}

LIB_EXPORT bool hasPlotHandler()
{
auto names = eventManager::get_instance()->getEventNames();
for(unsigned i=0; i < names.size(); i++) {
if (names[i] == PLOT)
return true;
}
return false;
}
2 changes: 2 additions & 0 deletions compile/events/eventManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ LIB_EXPORT void addListener(enum eventTypes type, const callback fn);

LIB_EXPORT void clearListeners();

LIB_EXPORT bool hasPlotHandler();

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions compile/events/eventManagerImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class eventManager
eventManager& operator=(eventManager const&) = delete;
~eventManager() {}

const std::vector<enum eventTypes>& getEventNames()const {return eventNames;}

void addListener(enum eventTypes type, const callback fn) {
std::lock_guard<std::mutex> lock(m_mutex);
eventNames.push_back(type);
Expand Down
3 changes: 2 additions & 1 deletion examples/eventDemo.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

% Make a controls block
controls = controlsClass();
controls.updatePlotFreq = 5;
controls.parallel = parallelOptions.Points;
controls.procedure = procedures.Simplex;
controls.maxIter = 1;
controls.maxIter = 10;

figure(2); clf
eventManager.register(eventTypes.Plot, 'plotRefSLDHelper')
Expand Down
5 changes: 3 additions & 2 deletions minimisers/NS/drawMultiNest.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
% find the ellipsoid from which to draw a new point
rval = rand;

for k=1:K
k0 = 1;
for k = 1:K
k0 = k;
if rval < fracvol(k)
continue
else
break
end
end
k0 = k;

% extract bounding matrix and centroid for that ellipsoid
B = Bs((k0-1)*ndims+1:k0*ndims,:);
Expand Down
Loading

0 comments on commit 18db474

Please sign in to comment.