public Item[] getAllConclusionItems() {
   SortedSet<Item> conclusions = new TreeSet<Item>();
   for (AssociationRule rule : this) {
     Iterator<Item> i = rule.getConclusionItems();
     while (i.hasNext()) {
       conclusions.add(i.next());
     }
   }
   Item[] itemArray = new Item[conclusions.size()];
   conclusions.toArray(itemArray);
   return itemArray;
 }
 public String toString(int maxNumber) {
   Collections.sort(associationRules);
   StringBuffer buffer = new StringBuffer("Association Rules" + Tools.getLineSeparator());
   int counter = 0;
   for (AssociationRule rule : associationRules) {
     if ((maxNumber >= 0) && (counter > maxNumber)) {
       buffer.append("... " + (associationRules.size() - maxNumber) + " other rules ...");
       break;
     }
     buffer.append(rule.toString());
     buffer.append(Tools.getLineSeparator());
     counter++;
   }
   return buffer.toString();
 }
  @Override
  public void doWork() throws OperatorException {
    double minValue = getParameterAsDouble(PARAMETER_MIN_CONFIDENCE);
    if (getParameterAsInt(PARAMETER_CRITERION) != CONFIDENCE) {
      minValue = getParameterAsDouble(PARAMETER_MIN_CRITERION_VALUE);
    }
    double theta = getParameterAsDouble(PARAMETER_GAIN_THETA);
    double laplaceK = getParameterAsDouble(PARAMETER_LAPLACE_K);
    FrequentItemSets sets = itemSetsInput.getData(FrequentItemSets.class);
    AssociationRules rules = new AssociationRules();
    HashMap<Collection<Item>, Integer> setFrequencyMap = new HashMap<Collection<Item>, Integer>();
    int numberOfTransactions = sets.getNumberOfTransactions();

    // iterating sorted over every frequent Set, generating every possible rule and building
    // frequency map
    sets.sortSets();
    for (FrequentItemSet set : sets) {
      setFrequencyMap.put(set.getItems(), set.getFrequency());
      // generating rule by splitting set in every two parts for head and body of rule
      if (set.getItems().size() > 1) {
        PowerSet<Item> powerSet = new PowerSet<Item>(set.getItems());
        for (Collection<Item> premises : powerSet) {
          if (premises.size() > 0 && premises.size() < set.getItems().size()) {
            Collection<Item> conclusion = powerSet.getComplement(premises);
            int totalFrequency = set.getFrequency();
            int preconditionFrequency = setFrequencyMap.get(premises);
            int conclusionFrequency = setFrequencyMap.get(conclusion);

            double value =
                getCriterionValue(
                    totalFrequency,
                    preconditionFrequency,
                    conclusionFrequency,
                    numberOfTransactions,
                    theta,
                    laplaceK);
            if (value >= minValue) {
              AssociationRule rule =
                  new AssociationRule(
                      premises, conclusion, getSupport(totalFrequency, numberOfTransactions));
              rule.setConfidence(getConfidence(totalFrequency, preconditionFrequency));
              rule.setLift(
                  getLift(
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rule.setConviction(
                  getConviction(
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rule.setPs(
                  getPs(
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rule.setGain(
                  getGain(
                      theta,
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rule.setLaplace(
                  getLaPlace(
                      laplaceK,
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rules.addItemRule(rule);
            }
          }
        }
      }
    }
    rulesOutput.deliver(rules);
    itemSetsOutput.deliver(sets);
  }
Example #4
0
  /** It launches the algorithm */
  public void execute() {
    if (somethingWrong) { // We do not execute the program
      System.err.println("An error was found");
      System.err.println("Aborting the program");
      // We should not use the statement: System.exit(-1);
    } else {
      this.proc =
          new AlcalaetalProcess(
              this.trans,
              this.nEvaluations,
              this.popSize,
              this.nBitsGene,
              this.phi,
              this.d,
              this.nFuzzyRegionsForNumericAttributes,
              this.useMaxForOneFrequentItemsets,
              this.minSupport,
              this.minConfidence);
      this.proc.run();
      this.associationRulesSet = this.proc.getRulesSet();
      this.proc.printReport(this.associationRulesSet);

      /*for (int i=0; i < this.associationRulesSet.size(); i++) {
      	System.out.println(this.associationRulesSet.get(i));
      }*/

      try {
        int r, i;
        AssociationRule ar;
        Itemset itemset;

        this.saveFuzzyAttributes(
            this.uniformFuzzyAttributesFilename, this.proc.getUniformFuzzyAttributes());
        this.saveFuzzyAttributes(
            this.adjustedFuzzyAttributesFilename, this.proc.getAdjustedFuzzyAttributes());
        this.saveGeneticLearningLog(
            this.geneticLearningLogFilename, this.proc.getGeneticLearningLog());

        PrintWriter rules_writer = new PrintWriter(this.rulesFilename);
        PrintWriter values_writer = new PrintWriter(this.valuesFilename);

        rules_writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        rules_writer.println("<rules>");

        values_writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        values_writer.print("<values ");
        values_writer.println(
            "n_one_frequent_itemsets=\""
                + this.proc.getNumberOfOneFrequentItemsets()
                + "\" n_rules=\""
                + this.associationRulesSet.size()
                + "\">");

        for (r = 0; r < this.associationRulesSet.size(); r++) {
          ar = this.associationRulesSet.get(r);

          rules_writer.println("<rule id = \"" + r + "\" />");
          values_writer.println(
              "<rule id=\""
                  + r
                  + "\" rule_support=\""
                  + ar.getRuleSupport()
                  + "\" antecedent_support=\""
                  + ar.getAntecedentSupport()
                  + "\" confidence=\""
                  + ar.getConfidence()
                  + "\"/>");
          rules_writer.println("<antecedents>");
          itemset = ar.getAntecedent();

          for (i = 0; i < itemset.size(); i++)
            this.createRule(itemset.get(i), this.proc.getAdjustedFuzzyAttributes(), rules_writer);

          rules_writer.println("</antecedents>");

          rules_writer.println("<consequents>");
          itemset = ar.getConsequent();

          for (i = 0; i < itemset.size(); i++)
            this.createRule(itemset.get(i), this.proc.getAdjustedFuzzyAttributes(), rules_writer);

          rules_writer.println("</consequents>");

          rules_writer.println("</rule>");
        }

        rules_writer.println("</rules>");
        values_writer.println("</values>");

        rules_writer.close();
        values_writer.close();

        System.out.println("\nAlgorithm Finished");
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      }
    }
  }