00001 #include "common.hpp"
00002 #include "common/log.h"
00003 #include "io/input/transaction_reader/brBufferedTransactionReader.hpp"
00004 #include "io/input/transaction_reader/SortedTransactionReader.hpp"
00005
00006
00007 #include "io/codec/decoder/df/SimpleDFDecoder.hpp"
00008
00009 #include "io/codec/decoder/df/DFDecoderWithEEManagement.hpp"
00010
00011 #include "io/output/normal/SortOutput.hpp"
00012
00013
00014 #include "util/StreamParser.hpp"
00015 #include "util/FrequentFilter.cpp"
00016
00017 #include "datastructures/maxvector.hpp"
00018 #include "datastructures/trie/edgelist/OrderedEdgelist.hpp"
00019
00020 #include "datastructures/trie/edgelist/OffsetIndexVector.hpp"
00021 #include "common/allocators.hpp"
00022
00023 #include "apriori/bodon/Leaf.hpp"
00024 #include "apriori/bodon/Trie.hpp"
00025 #include "apriori/bodon/TrieNEE.hpp"
00026 #include "io/input/transaction_reader/SortedTransactionReader.hpp"
00027 #include "io/db_cache/BuildTreeDBCache.hpp"
00028 #include "io/input/transaction_reader/OrderReverser.hpp"
00029 #include "io/codec/coder/Coder.hpp"
00030 #include "util/Frequent2Filter.cpp"
00031 #include "util/Frequent2FilterOnline.cpp"
00032 #include "apriori/bodon/trie/trie_manipulators/FrequentItemInserter.hpp"
00033 #include "apriori/bodon/trie/trie_manipulators/FrequentPairInserter.hpp"
00034 #include "apriori/bodon/trie/trie_manipulators/FrequentPairInserterNoprune.hpp"
00035
00036 #include "apriori/bodon/inhomogeneous_trie/trie_manipulators/IntersectProPruner.hpp"
00037 #include "apriori/bodon/inhomogeneous_trie/trie_manipulators/CandidateGeneratorPrune.hpp"
00038 #include "apriori/bodon/inhomogeneous_trie/trie_manipulators/InfreqRemoverOrderPresAssumption.hpp"
00039
00040 #include "apriori/OneByOneSupportCounter.hpp"
00041 #include "apriori/bodon/trie/trie_manipulators/support_counter/SupportCounterMerge.hpp"
00042
00043 #include "apriori/Apriori.hpp"
00044
00045 #include <vector>
00046 #include <iostream>
00047 #include <string>
00048
00049
00050 std::string file_format;
00051
00052 void init()
00053 {
00054 file_format = "File format:";
00055 file_format += "\n\nThe transactionfile is a plan text file. Each row ";
00056 file_format += "represents a transaction. \n";
00057 file_format += "A transaction is a set of items seperated by a nonnumeric ";
00058 file_format += "character.\nIt can be for example a white space, comma, ";
00059 file_format += "colon, etc.\n";
00060 file_format += "Items are nonnegative integers.\n";
00061 }
00063 void usage()
00064 {
00065 log_err(0,"Usage: orderPresAssumptionRatio transactionfile min_supp outcomefile [options]");
00066 log_err(0," deadend-option\t option of dead-end pruning, i.e: on, off");
00067 log_err(0," transactionfile\t file, that contains the tranasctions of items");
00068 log_err(0," outcomefile\t file to write the outcome");
00069 log_err(0," min_supp\t absolute support threshold");
00070
00071 std::cerr << file_format;
00072 log_err(0,"\t\t\tHave a succesful mining ;-)\n\n");
00073 }
00074
00086 int process_arguments( int argc, char *argv[], counter_t& min_supp,
00087 bool &isrel, double &relminsupp, unsigned int& maxsize )
00088 {
00089 if ( argc < 4 )
00090 {
00091 log_err(0,"There are 3 mandatory arguments.");
00092 return 2;
00093 }
00094 std::string mins=argv[2];
00095 if (mins[mins.size()-1]=='%') {
00096 mins.erase(mins.size()-1);
00097 isrel=true;
00098 relminsupp=atof(mins.c_str());
00099 relminsupp/=100;
00100 log_info(0,"Using relative minimum support of %lg",relminsupp);
00101 return 0;
00102 }
00103 isrel=false; relminsupp=0;
00104 int min_supp_i;
00105 try
00106 {
00107 convert(argv[2], min_supp_i);
00108 if ( min_supp_i <= 0 )
00109 {
00110 log_err(0,"%s cannot be converted to a positive integer.",argv[3]);
00111 return 3;
00112 }
00113 }
00114 catch(BadConversion e)
00115 {
00116 log_err(0,"min_supp conversion problem.");
00117 return 3;
00118 }
00119 min_supp = static_cast<counter_t>(min_supp_i);
00120 log_info(0,"min_supp is set to %d", min_supp);
00121 if(argc == 5)
00122 {
00123 int maxsize_i;
00124 try
00125 {
00126 convert(argv[4], maxsize_i);
00127 if ( maxsize_i <= 0 )
00128 {
00129 log_err(0,"%s cannot be converted to a positive integer.",argv[4]);
00130 return 4;
00131 }
00132 }
00133 catch(BadConversion e)
00134 {
00135 log_err(0,"max_size conversion problem.");
00136 return 4;
00137 }
00138 maxsize = static_cast<unsigned int>(maxsize_i);
00139 log_status(0,"maxsize is set to %d", maxsize);
00140 }
00141 else
00142 maxsize = largest_itemsetsize;
00143 return 0;
00144 }
00145
00146 int main( int argc, char *argv[] )
00147 {
00148 init();
00149 counter_t min_supp;
00150 unsigned int maxsize;
00151 bool relative;
00152 double relminsupp;
00153
00154 {
00155 int return_val =
00156 process_arguments( argc, argv, min_supp,
00157 relative, relminsupp, maxsize );
00158 if(return_val)
00159 return return_val;
00160 }
00161
00162 char* input_file = argv[1];
00163 char* output_file = argv[3];
00164
00165 try
00166 {
00167
00168 typedef brBufferedTransactionReader< > T_R;
00169
00170
00171
00172 T_R::params_t par_i;
00173 par_i.file_name = input_file;
00174 par_i.mode=FileReprBase::READ;
00175 par_i.file_buffer_size = 16 * 1024;
00176 T_R tr_reader(&par_i);
00177 std::vector< std::pair<counter_t, item_t> > freq_items_with_counters;
00178 counter_t nr_of_transactions;
00179
00180 FrequentFilter<T_R>
00181 fr_filter(tr_reader);
00182 log_status(0,"Finding frequent items.");
00183 fr_filter.findFrequentItems( freq_items_with_counters,
00184 nr_of_transactions, min_supp);
00185
00186 if(!freq_items_with_counters.empty())
00187 {
00188 log_status(0,"Doing decoder.");
00189 typedef DFDecoderWithEEManagement< > DF_D;
00190
00191
00192
00193 DF_D::params_t par_d;
00194 par_d.file_name = output_file;
00195 par_d.mode=FileReprBase::WRITE;
00196
00197 DF_D df_decoder(&par_d);
00198
00199 log_status(0,"APRIORI is selected");
00200 typedef Bodon::LeafWithoutConstructor LEAF_WC;
00201 typedef Bodon::Leaf LEAF;
00202 typedef Bodon::Trie< LEAF, Bodon::OrderedEdgelist<std::vector<Edge> > > TRIE;
00203
00204
00205
00206 log_status(0,"NEE_FULL is disabled");
00207 log_status(0,"Low memory need option is on.");
00208 typedef SortedTransactionReader< Coder<T_R, DF_D>, false, false > S_C_T_R;
00209 const bool ENDONLY = true;
00210 typedef OrderReverser< bracz::BuildTreeDBCache<
00211 S_C_T_R, std::vector<item_t>, bracz::EndPatriciaBuildTree<ENDONLY>, ENDONLY > >S_C;
00212 typedef Bodon::SupportCounterMerge<TRIE> SUPP_C_BASE;
00213 typedef OneByOneSupportCounter<TRIE, S_C, SUPP_C_BASE> SUPP_C;
00214 typedef Frequent2FilterOnline<S_C> F2F;
00215
00216 S_C::params_t par_c;
00217 par_c.file_name = input_file;
00218 par_c.mode=FileReprBase::READ;
00219 par_c.largest_item = tr_reader.getLargestItem();
00220 par_c.decoder = &df_decoder;
00221 par_c.freq_items_with_counters = &freq_items_with_counters;
00222 par_c.codemode = ASC;
00223
00224 log_status(0,"Doing sorted codec.");
00225 S_C sorted_coder(&par_c);
00226
00227 std::vector< std::pair<counter_t, std::pair<item_t, item_t> > >
00228 freq_pairs_with_counters;
00229 F2F fr_2_filter(&sorted_coder );
00230 log_status(0,"Finding frequent pairs.")
00231 fr_2_filter.findFrequentPairs(freq_pairs_with_counters, min_supp);
00232
00233 TRIE main_trie;
00234 typedef Bodon::FrequentItemInserter<DF_D, TRIE, NEE_Off> FII;
00235 FII fii(main_trie, df_decoder);
00236 typedef bracz::singleualloc<LEAF_WC, 64 * 1024> LEAF_ALLOCATOR;
00237 LEAF_ALLOCATOR s_alloc;
00238
00239
00240 log_status(0,"Simple pruning is selected.");
00241 typedef Bodon::FrequentPairInserter<DF_D, TRIE, LEAF_WC,
00242 LEAF_ALLOCATOR, NEE_Off> FPI;
00243 log_status(0,"deadend option is off.");
00244 const bool deadend = false;
00245 typedef Bodon::inhomogeneous_trie::IntersectProPruner<
00246 DF_D, TRIE, LEAF_WC, LEAF_ALLOCATOR, NEE_Off, deadend> PRUNER;
00247
00248 typedef Bodon::inhomogeneous_trie::CandidateGeneratorPrune<PRUNER, DF_D,
00249 TRIE, LEAF_ALLOCATOR, NEE_Off, deadend> CG;
00250 typedef Bodon::inhomogeneous_trie::InfreqRemoverOrderPresAssumption<
00251 DF_D, TRIE, LEAF_WC, LEAF_ALLOCATOR> IR;
00252 IR infrequent_remover(main_trie, df_decoder, s_alloc);
00253 typedef Apriori<S_C, DF_D, TRIE, LEAF_ALLOCATOR, FII, FPI, CG, IR, SUPP_C> A;
00254 A apriori(main_trie, s_alloc, infrequent_remover, sorted_coder, df_decoder, fii);
00255 log_status(0,"Finding frequent itemsets.");
00256 apriori.findFrequentItemsets(
00257 nr_of_transactions, *par_c.freq_counters,
00258 freq_pairs_with_counters, min_supp, maxsize );
00259 }
00260 }
00261 catch (std::ios_base::failure e)
00262 {
00263 log_err(0,"Exiting the program due to IO exception");
00264 return 1;
00265 }
00266 }
00267
00268