⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 algorithmmanager.java

📁 数据挖掘的工具代码(包含fp-tree,appriory
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		  {		    String item_name = (String)(inConsequent.get(i));		    int index = ((Integer)(name_to_index.get(item_name))).intValue();		    is_in_consequent.addItem(index);		  }	      }	    if (ignored.size() > 0)	      {		// create an itemset corresponding to ignored		is_ignored = new Itemset(ignored.size());		for (int i = 0; i < ignored.size(); i++)		  {		    String item_name = (String)(ignored.get(i));		    int index = ((Integer)(name_to_index.get(item_name))).intValue();		    is_ignored.addItem(index);		  }	      }	  }	catch (Exception e)	  {	    throw new AlgorithmManagerException("PANIC! " + e);	  }      }    // at this point we should have a cache_reader initialized    if (cache_reader == null)      throw new AlgorithmManagerException("Internal application error: cache_reader not initialized!");          AssociationsFinder algorithm = new AprioriRules();    Vector results;        try      {	if (advanced)	  {	    results = algorithm.findAssociations(cache_reader, 						 minSupport, 						 minConfidence,						 is_in_antecedent, 						 is_in_consequent,						 is_ignored, 						 maxAntecedent, 						 minConsequent);	  }	else	  {	    results = algorithm.findAssociations(cache_reader, 						 minSupport, 						 minConfidence);	  }      }    catch (Throwable e)      {	throw new AlgorithmManagerException("An error occurred: " + e);      }    finally      {	try	  {	    dbconf.unmarkDatabaseInUse(dbName);	    cache_reader.close();	  }	catch (Exception e)	  {	    throw new AlgorithmManagerException("PANIC! " + e);	  }      }    return results;  }  /**   * Find association rules with minimum support <code>minSupport</code>   * and minimum confidence <code>minConfidence</code>   * in database <code>dbName</code> using   * algorithm <code>algName</code>. The user that requests this operation   * is <code>user</code>.   *   * @param user   the user that requests this operation   * @param dbName   the database to mine   * @param algName   the algorithm to use   * @param minSupport   the minimum support of the rules   * @param minConfidence   the minimum confidence of the rules   * @exception AlgorithmManagerException   a problem happened   * @return   a Vector of AssociationRule objects   */  public Vector mine(String user, String dbName, String algName, 		     float minSupport, float minConfidence)    throws AlgorithmManagerException  {    if (user == null	|| dbName == null	|| algName == null	|| minSupport <= 0	|| minSupport > 1	|| minConfidence <= 0	|| minConfidence > 1)      throw new AlgorithmManagerException("Invalid mining parameters!");    return mine(user, dbName, algName, false,		minSupport, minConfidence,		null, null, null, 		0, 0);  }  /**   * Find association rules with minimum support <code>minSupport</code>   * and minimum confidence <code>minConfidence</code>   * in database <code>dbName</code> using   * algorithm <code>algName</code>. The user that requests this operation   * is <code>user</code>. Additional constraints are specified:   * the items that must appear in the antecedent of the rules are   * listed in <code>inAntecedent</code>, the items that must appear   * in the consequent of the rules are listed in <code>inConsequent</code>,   * the items that should be ignored are listed in <code>ignored</code>.   * Also the rules must have at most <code>maxAntecedent</code> items   * in the antecedent and at least <code>minConsequent</code> items   * in the consequent. An empty Vector or a value of 0 will indicate that   * a constraint is not required.   *   * @param user   the user that requests this operation   * @param dbName   the database to mine   * @param algName   the algorithm to use   * @param minSupport   the minimum support of the rules   * @param minConfidence   the minimum confidence of the rules   * @param inAntecedent   items to appear in antecedent, if empty then   * this constraint is ignored   * @param inConsequent   items to appear in consequent, if empty then   * this constraint is ignored   * @param ignored   items to ignore, if empty then   * this constraint is ignored   * @param maxAntecedent   the maximum number of items in antecedent,   * if 0 this constraint is ignored   * @param minConsequent   the minimum number of items in consequent,   * if 0 this constraint is ignored   * @exception AlgorithmManagerException   a problem happened   * @return   a Vector of AssociationRule objects   */  public Vector advMine(String user, String dbName, String algName,			float minSupport, float minConfidence,			Vector inAntecedent, Vector inConsequent,			Vector ignored, 			int maxAntecedent, int minConsequent)    throws AlgorithmManagerException  {    if (user == null	|| dbName == null	|| algName == null	|| minSupport <= 0	|| minSupport > 1	|| minConfidence <= 0	|| minConfidence > 1	|| inAntecedent == null	|| inConsequent == null	|| ignored == null	|| maxAntecedent < 0	|| minConsequent < 0)      throw new AlgorithmManagerException("Invalid mining parameters!");    return mine(user, dbName, algName, true,		minSupport, minConfidence,		inAntecedent, inConsequent, ignored, 		maxAntecedent, minConsequent);  }  /**   * Run a test to see how algorithms run on different databases for   * different supports. This tests only the algorithms for finding   * large itemsets.   *   * Each algorithm will be run on each database for all the supports   * listed. If tests fail, results of 0 will be returned. This method   * deals silently with errors since it tries to test as much as it can.   *   * The returned Vector will contain:   *    - at index 0 the Vector <code>supports</code>   *    - from index 1 on, we will have for each database/algorithm pair   *      a Vector structured as follows:   *      - at index 0 a String representing the name of the database   *      - at index 1 a String representing the name of the algorithm   *      - at index 2 a Vector containing the results of running the   *        algorithm on the database for the full range of supports.   *        This Vector corresponds to the <code>supports</code>   *        Vector as follows:   *           For each support at index <code>i</code> in the Vector    *           <code>supports</code>, we will find the time as a Long at   *           index <code>2*i</code> and the number of passes as an    *           Integer at index <code>2*i+1</code> in this Vector of   *           results.   *   * @param user   the user that requests this operation   * @param databases   the databases to use in the test   * @param algorithms   the algorithms to use in the test   * @param supports   the supports to use in the test   * @return   a Vector containing the results of the test   */  public Vector benchmark(String user, Vector databases, Vector algorithms,			  Vector supports)  {    if (user == null	|| databases == null	|| algorithms == null	|| supports == null)      return new Vector(); // qui pro quo    // the following will let me know if I marked the     // databases and algorithms    boolean[] db_ready = new boolean[databases.size()];    boolean[] alg_ready = new boolean[algorithms.size()];    // mark all databases in use    for (int i = 0; i < databases.size(); i++)      {	String dbName = (String)databases.get(i);	try	  {	    dbconf.markDatabaseInUse(user, dbName);	    db_ready[i] = true;	  }	catch (DBConfigException e)	  {	    db_ready[i] = false;	  }      }    // mark all algorithms in use    for (int i = 0; i < algorithms.size(); i++)      {	String algName = (String)algorithms.get(i);	try	  {	    dbconf.markAlgorithmInUse(user, algName);	    alg_ready[i] = true;	  }	catch (DBConfigException e)	  {	    alg_ready[i] = false;	  }      }    // at this point we made sure that the algorithms and databases    // that we just marked won't be deleted in the near future so    // we can use them safely. Those algorithms and databases that    // we couldn't mark were probably either deleted or scheduled    // to be deleted so we'll just skip them.    Timer timer = new Timer();    timer.start(); // keep it running, we'll use reset() and time() to    // read the time spent on something    Vector results = new Vector();    results.add(supports);    for (int d = 0; d < databases.size(); d++)      {	if (db_ready[d] == false)	  continue; // go to next one	String dbName = (String)databases.get(d);	// run all algorithms on this database so that we can	// unmark this database	for (int a = 0; a < algorithms.size(); a++)	  {	    if (alg_ready[a] == false)	      continue; // go to next one	    String algName = (String)algorithms.get(a);	    Vector testrun = new Vector();	    testrun.add(dbName);	    testrun.add(algName);	    // here we'll add for each test a Long indicating the time	    Vector time_results = new Vector();	    // here we'll add for each test an Integer indicating 	    // the database passes	    Vector num_passes_results = new Vector();	    testrun.add(time_results);	    testrun.add(num_passes_results);	    results.add(testrun);	    for (int s = 0; s < supports.size(); s++)	      {		float minSupport = ((Float)supports.get(s)).floatValue();		// run algorithm algName on database dbName for minimum		// support minSupport		try		  {		    // get hold of the algorithm object		    LargeItemsetsFinder algorithm = loadAlgorithm(algName);		    String db_path = dbconf.getPathDatabase(dbName);		    DBReader db_reader = new DBReader(db_path);		    timer.reset();		    // we try to run the algorithm		    int passes = algorithm.findLargeItemsets(db_reader, 							     null,							     minSupport);		    long time = timer.time();		    db_reader.close();		    time_results.add(new Long(time));		    num_passes_results.add(new Integer(passes));		  }		catch (Throwable e) // catches both Error and Exception !		  {		    // we indicate that something went wrong by returning		    // 0 values for time and passes.		    time_results.add(new Long(0));		    num_passes_results.add(new Integer(0));		    System.err.println("Error trying to run algorithm "				       + algName + " on database "				       + dbName + " : " + e);		  }	      }	  }	// we've finished our work with this database so we can unmark it	try	  {	    dbconf.unmarkDatabaseInUse(dbName);	  }	catch (DBConfigException e)	  {	    System.err.println("Error trying to unmark database "			       + dbName);	  }      }    // here we can finally unmark the algorithms    for (int i = 0; i < algorithms.size(); i++)      {	if (alg_ready[i] == false)	  continue; // go to next one	String algName = (String)algorithms.get(i);	try	  {	    dbconf.unmarkAlgorithmInUse(algName);	  }	catch (DBConfigException e)	  {	    System.err.println("Error trying to unmark algorithm "			       + algName);	  }      }    return results;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -