simAnneal: Finds the minimum energy configuration using simulated anneal. INPUT : segs_list : list of ids of segments to be turned on and off data : seg data file cell_mask : mask of regions of cells to be optimized xx : xx from bounding box of cell_mask yy : yy from bounding box of cell_mask CONST : segmentation constants debug_flag : 1 to display 'diagnose' for simulated anneal OUTPUT : x : vector of segments to be on for minimum energy found. regEmin : energy of every region for the minimum state Copyright (C) 2016 Wiggins Lab Written by Stella Stylianidou, Paul Wiggins. University of Washington, 2016 This file is part of SuperSegger. SuperSegger is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. SuperSegger is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with SuperSegger. If not, see <http://www.gnu.org/licenses/>.
0001 function [x,regEmin]= simAnnealMap( segs_list, data, ... 0002 cell_mask, xx, yy, CONST, debug_flag) 0003 % simAnneal: Finds the minimum energy configuration using simulated anneal. 0004 % 0005 % INPUT : 0006 % segs_list : list of ids of segments to be turned on and off 0007 % data : seg data file 0008 % cell_mask : mask of regions of cells to be optimized 0009 % xx : xx from bounding box of cell_mask 0010 % yy : yy from bounding box of cell_mask 0011 % CONST : segmentation constants 0012 % debug_flag : 1 to display 'diagnose' for simulated anneal 0013 % 0014 % OUTPUT : 0015 % x : vector of segments to be on for minimum energy found. 0016 % regEmin : energy of every region for the minimum state 0017 % 0018 % Copyright (C) 2016 Wiggins Lab 0019 % Written by Stella Stylianidou, Paul Wiggins. 0020 % University of Washington, 2016 0021 % This file is part of SuperSegger. 0022 % 0023 % SuperSegger is free software: you can redistribute it and/or modify 0024 % it under the terms of the GNU General Public License as published by 0025 % the Free Software Foundation, either version 3 of the License, or 0026 % (at your option) any later version. 0027 % 0028 % SuperSegger is distributed in the hope that it will be useful, 0029 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0030 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0031 % GNU General Public License for more details. 0032 % 0033 % You should have received a copy of the GNU General Public License 0034 % along with SuperSegger. If not, see <http://www.gnu.org/licenses/>. 0035 0036 0037 num_segs = numel(segs_list); 0038 0039 if ~exist('debug_flag', 'var') || isempty( debug_flag ) 0040 debug_flag = 1; 0041 end 0042 0043 if debug_flag 0044 display = 'diagnose'; 0045 else 0046 display = 'off'; 0047 end 0048 0049 if isfield( CONST.regionOpti, 'ADJUST_FLAG' ) && CONST.regionOpti.ADJUST_FLAG 0050 maxiter = floor(CONST.regionOpti.Nt * num_segs/10); 0051 else 0052 maxiter = CONST.regionOpti.Nt; 0053 end 0054 0055 % interval at which it sets the temperature to T0 and starts annealing again 0056 reannealIter = round(maxiter / 2); 0057 ss = size(data.phase); 0058 0059 % initial state 0060 x0 = zeros(num_segs,1); 0061 0062 % a hashmap to find energies of states tried before faster 0063 stateEnergyMap = containers.Map(); 0064 0065 % runs simulated anneal 0066 options = saoptimset('Display',display,'TimeLimit',180,'ReannealInterval',reannealIter,'DataType','custom', 'AnnealingFcn',@newPoint,'StallIterLimit',num_segs*12,'MaxIter',maxiter); 0067 [x,Emin,exitflag,output] = simulannealbnd(@stateCostFunction,x0,[],[],options); 0068 0069 % compare to the state with the on/off segments by score and keep that one 0070 % if it is smaller 0071 xSegment = data.segs.score(segs_list); 0072 Esegment = stateCostFunction (xSegment); 0073 if Esegment < Emin 0074 x = xSegment; 0075 end 0076 0077 [Emin,minState] = calculateStateEnergy(cell_mask,vect0,segs_list,data,xx,yy,CONST); 0078 regEmin = minState.reg_E; % energy per region 0079 0080 if debug_flag 0081 disp (['minimum energy is ', num2str(Emin)]); 0082 displayState(x,segs_list,data) 0083 end 0084 0085 0086 function displayState(x,segs_list,data) 0087 % displayState : displays the modified mask given a vector x of 0088 % segments that are on and off. 0089 cell_mask_mod = cell_mask; 0090 num_segs = numel(segs_list); 0091 for kk = 1:num_segs 0092 cell_mask_mod = cell_mask_mod - x(kk)*(segs_list(kk)==data.segs.segs_label(yy,xx)); 0093 end 0094 imshow(cell_mask_mod); 0095 end 0096 0097 0098 function vect1 = newPoint ( optimvalues,~ ) 0099 % newPoint : modifies the previous state to a new state, by 0100 % switching one of the segments. 0101 vect0 = optimvalues.x; 0102 nn = floor(rand*num_segs)+1; 0103 vect1 = vect0; 0104 vect1(nn) = ~vect1(nn); 0105 end 0106 0107 0108 function E = stateCostFunction (vect0) 0109 % stateCostFunction : caclulates the cost function of a given state 0110 % vect0 0111 0112 key = makeKey(vect0); 0113 0114 if isKey(stateEnergyMap, key) 0115 % state tried before, get energy from the hashmap 0116 E = stateEnergyMap(key); 0117 else 0118 % calculate total score 0119 [E,~] = calculateStateEnergy(cell_mask,vect0,segs_list,data,xx,yy,CONST); 0120 stateEnergyMap(key) = E; 0121 end 0122 0123 end 0124 0125 function str = makeKey(vect) 0126 % HashMap function, creates a key from a vector of on/off segments 0127 % INPUT : vect : logical vector of 1 for segments that are on 0128 % OUTPUT : str : hashmap key 0129 str = char(double(vect)'+'a'); 0130 end 0131 0132 end 0133 0134