Home > src > randPopulationAndSampleCandidateSelection.m

# randPopulationAndSampleCandidateSelection

## PURPOSE

- random population and sample selection method for swaps

## SYNOPSIS

This is a script file.

## DESCRIPTION

     - random population and sample selection method for swaps

     copyright 2009-2012 Blair Armstrong, Christine Watson, David Plaut

        This file is part of SOS

        SOS is free software: you can redistribute it and/or modify
        it for academic and non-commercial purposes
        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.  For commercial or for-profit
        uses, please contact the authors (sos@cnbc.cmu.edu).

        SOS 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.

## CROSS-REFERENCE INFORMATION

This function calls:

- genericFeederCandidateSelection - parent class for replacement selection objects
- randPopulationAndSampleCandidateSelection - random population and sample selection method for swaps
- sample - sample object
- verbosePrint - master print function that regulates cmd line output based on verbosity flags

This function is called by:

- randPopulationAndSampleCandidateSelection - random population and sample selection method for swaps
- sos - SOS optimization object

## SUBFUNCTIONS

- function obj = randPopulationAndSampleCandidateSelection(sosObj)
- function init(obj,sosObj)
- function [feederdf,feederdfIndex] = getCandidateIndex(obj,sample)

## SOURCE CODE

    0001 % - random population and sample selection method for swaps
    0002 %
    0003 % copyright 2009-2012 Blair Armstrong, Christine Watson, David Plaut
    0004 %
    0005 %    This file is part of SOS
    0006 %
    0007 %    SOS is free software: you can redistribute it and/or modify
    0008 %    it for academic and non-commercial purposes
    0009 %    under the terms of the GNU General Public License as published by
    0010 %    the Free Software Foundation, either version 3 of the License, or
    0011 %    (at your option) any later version.  For commercial or for-profit
    0012 %    uses, please contact the authors (sos@cnbc.cmu.edu).
    0013 %
    0014 %    SOS is distributed in the hope that it will be useful,
    0015 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
    0016 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0017 %    GNU General Public License for more details.
    0018
    0019 %    You should have received a copy of the GNU General Public License
    0020 %    along with SOS (see COPYING.txt).
    0021 %    If not, see <http://www.gnu.org/licenses/>.
    0022
    0023
    0024
    0025 classdef randPopulationAndSampleCandidateSelection < genericFeederCandidateSelection
    0026     % selects a neighbor item from a sample's population or other children samples of that pop.
    0027     % sample's population
    0028     %
    0029     %
    0030     % PROPERTIES:
    0031     %   samples - list of all samples swaps could occur in
    0032     %   sampleItems - list of samples and items that could be swapped into those samples
    0033     %
    0034     % METHODS:
    0035     %   randPopulationAndSampleCandidateSelection() % Constructor
    0036     %   init() %initializes the sample method by generating a list of swap candidates for this sample.
    0037     %   [feederdf,feederdfIndex] = getCandidateIndex(obj,sample)  % returns a dataframe and row index for a swap candidate
    0038
    0039
    0040
    0041     properties
    0042         samples
    0043         sampleItems
    0044     end
    0045
    0046     methods
    0047        %% randPopulationAndSampleCandidateSelection() CONSTRUCTOR
    0048         function obj = randPopulationAndSampleCandidateSelection(sosObj)
    0049             % Constructor
    0050
    0051             obj.init(sosObj);
    0052
    0053             verbosePrint('Random Population And Sample Feeder Candidate Selection Ready', ...
    0054                 'randPopulationAndSampleCandidateSelection_const');
    0055         end
    0056
    0057
    0058         %% init(sosObj) METHOD
    0059         function init(obj,sosObj)
    0060             %initializes the sample method by generating a list of swap candidates for this sample.
    0061             %
    0062             %PARAMETERS:
    0063             % sosObj - the SOS object the candidate selection method is linked to
    0064
    0065             obj.samples = [];
    0066             obj.sampleItems = {};
    0067
    0068             for i=1:length(sosObj.samples)
    0069                 curSample = sosObj.samples(i);
    0070                 obj.samples = [obj.samples curSample];
    0071
    0072                 alreadyMerged = {};
    0073
    0074                 candidateItemIndicies = {};
    0075                 %add it's own items to the list.
    0076
    0077                 for j=1:curSample.n
    0078                     if curSample.locks(j) == 0
    0079                         candidateItemIndicies = [candidateItemIndicies ; {curSample j}]; %#ok<AGROW>
    0080                     end
    0081                 end
    0082
    0083                 alreadyMerged = [alreadyMerged {curSample}]; %#ok<AGROW>
    0084
    0085                 %add it's population's item to the list
    0086
    0087                 if(isempty(curSample.population) == false && isempty(curSample.population.data) == false)
    0088                     tempIndicies = cell(length(curSample.population.data{1}),2);
    0089                     for j=1:length(curSample.population.data{1})
    0090                         tempIndicies{j,1} = curSample.population;
    0091                         tempIndicies{j,2} = j;
    0092                     end
    0093
    0094                     candidateItemIndicies = [candidateItemIndicies ; tempIndicies]; %#ok<AGROW>
    0095                     alreadyMerged = [alreadyMerged {curSample.population}]; %#ok<AGROW>
    0096
    0097                 end
    0098                 % add other samples which are part of this SOS obj and
    0099                 % which share the sample population to the current sample.
    0100
    0101                 %no siblings if there is no population linked with the
    0102                 %object
    0103                 if isempty(curSample.population) ~= 1
    0104                     numSiblings=length(curSample.population.samples);
    0105                 else
    0106                     numSiblings = 0;
    0107                 end
    0108
    0109                 for k=1:numSiblings
    0110                     siblingSample = curSample.population.samples(k);
    0111
    0112                     doneAlready = false;
    0113                     for l=1:length(alreadyMerged)
    0114                         if alreadyMerged{l} == siblingSample
    0115                             doneAlready = true;
    0116                         end
    0117                     end
    0118
    0119
    0120                     inSOSObj = max(ismember(siblingSample,sosObj.samples));
    0121                     %doneAlready = max(ismember(siblingSample,alreadyMerged));
    0122
    0123                     if inSOSObj == true && doneAlready == false
    0124                         for j=1:siblingSample.n
    0125                             %also exclude locked items from the sibling
    0126                             %sample from entering into the computation.
    0127                             if siblingSample.locks(j) == 0
    0128                              candidateItemIndicies = [candidateItemIndicies ; {siblingSample j}]; %#ok<AGROW>
    0129                             end
    0130                         end
    0131                     end
    0132
    0133                     alreadyMerged = [alreadyMerged {siblingSample}]; %#ok<AGROW>
    0134
    0135                 end
    0136
    0137                 obj.sampleItems{i} = candidateItemIndicies;
    0138             end
    0139
    0140         end %init()
    0141
    0142
    0143         %% [feederdf,feederdfIndex] = getCandidateIndex(obj,sample) METHOD
    0144         function [feederdf,feederdfIndex] = getCandidateIndex(obj,sample)
    0145             % returns a dataframe and row index for a swap candidate
    0146             %
    0147             % PARAMETERS:
    0148             %   sample - the target sample to swap into
    0149             %
    0150             % RETURNS:
    0151             % feederdf - the feeder dataframe
    0152             % feederdfIndex -row index of item in feederdf
    0153
    0154             if(strcmp(class(sample),'sample') == false)
    0155                 error('Input must be a sample');
    0156             end
    0157
    0158             sampleIndex = -1;
    0159             for i=1:length(obj.samples)
    0160                 if obj.samples(i) == sample
    0161                     sampleIndex = i;
    0162                 end
    0163             end
    0164
    0165             %find the sample
    0166             if sampleIndex == -1
    0167                 error('Unable to find sample');
    0168             end
    0169
    0170             %randomly select an item to swap with
    0171
    0172             lookupIndex = floor((length(obj.sampleItems{sampleIndex})*rand)+1);
    0173
    0174             feederdf = obj.sampleItems{1,sampleIndex}{lookupIndex,1};
    0175             feederdfIndex = obj.sampleItems{1,sampleIndex}{lookupIndex,2};
    0176
    0177         end
    0178
    0179     end
    0180
    0181 end
    0182