Muster
 All Classes Namespaces Files Functions Variables Typedefs Macros
trial.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2010, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory
4 // LLNL-CODE-433662
5 // All rights reserved.
6 //
7 // This file is part of Muster. For details, see http://github.com/tgamblin/muster.
8 // Please also read the LICENSE file for further information.
9 //
10 // Redistribution and use in source and binary forms, with or without modification, are
11 // permitted provided that the following conditions are met:
12 //
13 // * Redistributions of source code must retain the above copyright notice, this list of
14 // conditions and the disclaimer below.
15 // * Redistributions in binary form must reproduce the above copyright notice, this list of
16 // conditions and the disclaimer (as noted below) in the documentation and/or other materials
17 // provided with the distribution.
18 // * Neither the name of the LLNS/LLNL nor the names of its contributors may be used to endorse
19 // or promote products derived from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
22 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 // LAWRENCE LIVERMORE NATIONAL SECURITY, LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE
25 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 //////////////////////////////////////////////////////////////////////////////////////////////////
31 
32 ///
33 /// @file trial.cpp
34 /// @author Todd Gamblin tgamblin@llnl.gov
35 ///
36 #include "trial.h"
37 
38 #include <algorithm>
39 using namespace std;
40 
41 namespace cluster {
42 
43  size_t trial_generator::get_sample_size(size_t k) {
44  return min(init_size + 2 * k, num_objects);
45  }
46 
47  static size_t count_all(const trial_generator& tg) {
48  trial_generator dummy = tg;
49  while (dummy.has_next()) dummy.next();
50  return dummy.count();
51  }
52 
53 
54  trial_generator::trial_generator(size_t _max_k, size_t _max_reps, size_t _init_size, size_t _num_objects)
55  : max_k(_max_k),
56  max_reps(_max_reps),
57  init_size(_init_size),
58  num_objects(_num_objects),
59  cur_trial(1, 0, get_sample_size(1)),
60  iterations(0)
61  {
62  number_of_trials = count_all(*this);
63  }
64 
65 
66  trial_generator::trial_generator(size_t _min_k, size_t _max_k, size_t _max_reps, size_t _init_size,
67  size_t _num_objects)
68  : max_k(_max_k),
69  max_reps(_max_reps),
70  init_size(_init_size),
71  num_objects(_num_objects),
72  cur_trial(_min_k, 0, get_sample_size(_min_k)),
73  iterations(0)
74  {
75  number_of_trials = count_all(*this);
76  }
77 
78 
80  return cur_trial.k <= max_k;
81  }
82 
83 
85  trial result = cur_trial;
86 
87  // iterate first through trials, then through k's, and only do one trial if the sample size is
88  // equal to the maximum sample size.
89  cur_trial.rep++;
90  if (cur_trial.rep >= max_reps || cur_trial.sample_size == num_objects) {
91  cur_trial.rep = 0;
92  cur_trial.k++;
93  cur_trial.sample_size = get_sample_size(cur_trial.k);
94  }
95 
96  iterations++;
97  return result;
98  }
99 
100 
102  return number_of_trials;
103  }
104 
105  size_t trial_generator::count() const {
106  return iterations;
107  }
108 
109 
110 
111 } // namespace cluster
Data structure representing a trial run of a partitioned clustering algorithm.
bool has_next() const
whether there are trials remaining.
Definition: trial.cpp:79
const size_t max_k
maximum k to try
Definition: trial.h:89
size_t count() const
return iterations so far.
Definition: trial.cpp:105
trial next()
return parameters for next trial
Definition: trial.cpp:84
const size_t max_reps
max number of repetitions per k
Definition: trial.h:90
size_t k
Definition: trial.h:53
size_t num_trials()
Return total number of trials this will generate.
Definition: trial.cpp:101
trial_generator(size_t _max_k, size_t _max_reps, size_t _init_size, size_t _num_objects)
Constructor to generate trials from 1 to max_k.
Definition: trial.cpp:54
size_t rep
Definition: trial.h:54
const size_t num_objects
number of elements in the data set; determines maximum sample size.
Definition: trial.h:92
This struct represents parameters for a single trial run of kmedoids.
Definition: trial.h:52
size_t sample_size
Definition: trial.h:55
Muster. Copyright © 2010, Lawrence Livermore National Laboratory, LLNL-CODE-433662.
Distribution of Muster and its documentation is subject to terms of the Muster LICENSE.
Generated on Thu Sep 1 2016 using Doxygen 1.8.5