PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // HeapTest.cc 00004 // 00005 // Copyright (C) 2005 Nicolas Chapados 00006 // Copyright (C) 2005 Olivier Delalleau 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00038 ******************************************************* */ 00039 00040 // Authors: Nicolas Chapados, Olivier Delalleau 00041 00045 #include "HeapTest.h" 00046 #include <assert.h> 00047 #include <vector> 00048 #include <algorithm> 00049 #include <iostream> 00050 #include <plearn/misc/heap_utilities.h> 00051 #include <plearn/math/random.h> 00052 00053 namespace PLearn { 00054 using namespace std; 00055 00056 PLEARN_IMPLEMENT_OBJECT( 00057 HeapTest, 00058 "Test the STL-like functions update_heap and is_valid_heap", 00059 "" 00060 ); 00061 00063 // HeapTest // 00065 HeapTest::HeapTest(): 00066 n(1000), 00067 m(100000) 00068 /* ### Initialize all fields to their default value */ 00069 { 00070 // ... 00071 00072 // ### You may (or not) want to call build_() to finish building the object 00073 // ### (doing so assumes the parent classes' build_() have been called too 00074 // ### in the parent classes' constructors, something that you must ensure) 00075 } 00076 00078 // build // 00080 void HeapTest::build() 00081 { 00082 inherited::build(); 00083 build_(); 00084 } 00085 00087 // makeDeepCopyFromShallowCopy // 00089 void HeapTest::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00090 { 00091 inherited::makeDeepCopyFromShallowCopy(copies); 00092 00093 // ### Call deepCopyField on all "pointer-like" fields 00094 // ### that you wish to be deepCopied rather than 00095 // ### shallow-copied. 00096 // ### ex: 00097 // deepCopyField(trainvec, copies); 00098 00099 // ### Remove this line when you have fully implemented this method. 00100 PLERROR("HeapTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00101 } 00102 00104 // declareOptions // 00106 void HeapTest::declareOptions(OptionList& ol) 00107 { 00108 // ### Declare all of this object's options here 00109 // ### For the "flags" of each option, you should typically specify 00110 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00111 // ### OptionBase::tuningoption. Another possible flag to be combined with 00112 // ### is OptionBase::nosave 00113 00114 declareOption(ol, "m", &HeapTest::m, OptionBase::buildoption, 00115 "Number of random updates."); 00116 00117 declareOption(ol, "n", &HeapTest::n, OptionBase::buildoption, 00118 "Size of the data vector."); 00119 00120 // Now call the parent class' declareOptions 00121 inherited::declareOptions(ol); 00122 } 00123 00125 // build_ // 00127 void HeapTest::build_() 00128 { 00129 // ### This method should do the real building of the object, 00130 // ### according to set 'options', in *any* situation. 00131 // ### Typical situations include: 00132 // ### - Initial building of an object from a few user-specified options 00133 // ### - Building of a "reloaded" object: i.e. from the complete set of all serialised options. 00134 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00135 // ### You should assume that the parent class' build_() has already been called. 00136 } 00137 00139 // perform // 00141 void HeapTest::perform() 00142 { 00143 // ### The test code should go here. 00144 vector<real> data(n); 00145 for (int i=0; i<n; ++i) 00146 data[i] = gaussian_01(); 00147 make_heap(data.begin(), data.end()); 00148 00149 PLASSERT(is_valid_heap(data.begin(), data.end())); 00150 00151 // Destroy heap and reconstruct it 00152 for (int i=0; i<m; ++i) { 00153 int j = int(bounded_uniform(0,n)); 00154 PLASSERT(j>=0 && j<n); 00155 00156 data[j] = gaussian_01(); 00157 if (! is_valid_heap(data.begin(), data.end())) { 00158 update_heap(data.begin(), data.end(), data.begin()+j); 00159 PLASSERT(is_valid_heap(data.begin(), data.end())); 00160 } 00161 } 00162 00163 cout << "Heap condition successfully restored" << endl; 00164 00165 } 00166 00167 } // end of namespace PLearn 00168 00169 00170 /* 00171 Local Variables: 00172 mode:c++ 00173 c-basic-offset:4 00174 c-file-style:"stroustrup" 00175 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00176 indent-tabs-mode:nil 00177 fill-column:79 00178 End: 00179 */ 00180 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :