Home > src > randSampleCandidateSelection.m

# randSampleCandidateSelection

## PURPOSE

- random sample method for selecting swaps

## SYNOPSIS

This is a script file.

## DESCRIPTION

     - random sample method for selecting 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:

- genericSampleCandidateSelection - generic method for sample selection objects
- randSampleCandidateSelection - random sample method for selecting swaps
- verbosePrint - master print function that regulates cmd line output based on verbosity flags

This function is called by:

- randSampleCandidateSelection - random sample method for selecting swaps
- sos - SOS optimization object

## SUBFUNCTIONS

- function obj = randSampleCandidateSelection(sosObj)
- function init(obj,sosObj)
- function [targSample, targIndex] = getCandidateIndex(obj)

## SOURCE CODE

    0001 % - random sample method for selecting 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 randSampleCandidateSelection < genericSampleCandidateSelection
    0026     %% randomly select an item from one of the samples as a candidate for a swap.
    0027     %
    0028     % PROPERTIES:
    0029     %   candidateItemIndices % list of possible sample items to swap
    0030     %
    0031     %METHODS
    0032     %   randSampleCandidateSelection(sosObj) % Constructor
    0033     %   init(sosObj) initialize the object by generating a list of possible sample items to swap
    0034     %   [targSample, targIndex] = getCandidateIndex() % randomly select an item in a sample to swap
    0035
    0036     %% PROPERTIES
    0037     properties
    0038         candidateItemIndices % list of possible sample items to swap
    0039     end
    0040
    0041     methods
    0042         %% randSampleCandidateSelection(sosObj) Constructor
    0043         function obj = randSampleCandidateSelection(sosObj)
    0044             % Constructor
    0045             %
    0046             %PARAMETERS:
    0047             %   sosObj - the sos object this object is to be linked to
    0048
    0049             obj.init(sosObj);
    0050             verbosePrint('Random Sample Target Candidate Selection Ready', ...
    0051                 'randSampleCandidateSelection_const');
    0052         end
    0053
    0054         %% init(sosObj) METHOD
    0055         function init(obj,sosObj)
    0056             % initialize the object by generating a list of possible sample items to swap
    0057
    0058             candidateItemIndices = {}; %#ok<PROP>
    0059
    0060             for i =1:length(sosObj.samples)
    0061                 for j=1:sosObj.samples(i).n
    0062                    if sosObj.samples(i).locks(j) == 0
    0063                        candidateItemIndices = [candidateItemIndices ; {sosObj.samples(i) j}]; %#ok<AGROW,PROP>
    0064                    end
    0065                 end
    0066             end
    0067
    0068             obj.candidateItemIndices = candidateItemIndices;   %#ok<PROP>
    0069
    0070         end
    0071
    0072         %% getCandidateIndex() METHOD
    0073         function [targSample, targIndex] = getCandidateIndex(obj)
    0074             % randomly select an item in a sample to swap
    0075             %
    0076             %RETRURNS:
    0077             % Index of sample, and index of item in sample.
    0078
    0079             if isempty(obj.candidateItemIndices)
    0080                 error(['Cannot optimize - no unlocked items in samples.',...
    0081                     char(10), 'Did you initFillSamples()? Is there at least one sample whose items are not all locked?']);
    0082             end
    0083
    0084             index = obj.candidateItemIndices(floor((length(obj.candidateItemIndices)*rand)+1), 1:2);
    0085             targSample = index{1};
    0086             targIndex = index{2};
    0087         end
    0088
    0089     end
    0090 end