Vous êtes sur la page 1sur 6

package rs.fon.whibo.blackbox; import java.io.File; import java.io.FileOutputStream; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.

OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import rs.fon.whibo.GDT.algorithm.GDTAlgorithm; import rs.fon.whibo.GDT.problem.GenericTreeProblemBuilder; import rs.fon.whibo.integration.adapters.parameter.ParameterTypeProblemFile; import rs.fon.whibo.integration.adapters.parameter.ProblemFileValueEditor; import rs.fon.whibo.problem.Problem; import rs.fon.whibo.problem.serialization.ProblemDecoder; import com.rapidminer.example.ExampleSet; import com.rapidminer.gui.properties.PropertyTable; import com.rapidminer.operator.OperatorDescription; import com.rapidminer.operator.OperatorException; import com.rapidminer.operator.learner.AbstractLearner; import com.rapidminer.operator.learner.LearnerCapability; import com.rapidminer.operator.learner.tree.Tree; import com.rapidminer.operator.learner.tree.TreeModel; import com.rapidminer.parameter.ParameterType; import com.rapidminer.parameter.ParameterTypeDouble; import com.rapidminer.parameter.ParameterTypeInt; /** * Separate RapidMiner operator implementing CART algorithm described in [ref] realized through WhiBo components. * Components are fixed, but all the parameters are open for the user to set. * * This operator is used for comparing the black-box approach with the white-box (WhiBo) approach. * It represents classic interface for the end user, contrasting WhiBo user interface. */ public class CART extends AbstractLearner { /** The WhiBo .wba file. */ protected static String PROBLEM_FILE = "problem file"; /** The Algorithm parameter that defines the maximal depth of the tree built. */ public static final String PARAMETER_MAX_TREE_DEPTH = "max_tree_depth";

/** The Algorithm parameter that defines the minimal size of each node of the tree in terms of number of instances */ public static final String PARAMETER_MIN_NODE_SIZE = "min_node_size"; /** The Algorithm parameter that defines the minimal size of the leaf nodes of the tree in terms of number of instances */ public static final String PARAMETER_MIN_LEAF_SIZE = "min_leaf_size"; /** The Algorithm parameter that defines the severity of the prune proces. Larger values indicate smaller resulting tree */ public static final String PARAMETER_PRUNNING_SEVERITY = "prunning_severity";

/** * Instantiates a new cART. * * @param description of the operator used for RapidMiner's user interface */ public CART(OperatorDescription description) { super(description); //point of integration with .parameter package PropertyTable.registerPropertyValueCellEditor(ParameterTypeProblemFile.class, ProblemFileValueEditor.class); } /* (non-Javadoc) * @see com.rapidminer.operator.Operator#getParameterTypes() */ @Override public List<ParameterType> getParameterTypes() { List<ParameterType> types = super.getParameterTypes(); ParameterType type = new ParameterTypeProblemFile(getProblemFile(), "Process file from which to load the generic tree", "wba", false, GenericTreeProblemBuilder.class); type.setDefaultValue("C:\\WhiBoHCI\\Algorithms\\CART.wba"); type.setExpert(true); type.setHidden(true); types.add(type); type = new ParameterTypeInt(PARAMETER_MAX_TREE_DEPTH, "The maximal tree depth of a leaf node.", 1, 10000, 10000); type.setExpert(false); types.add(type); type = new ParameterTypeInt(PARAMETER_MIN_NODE_SIZE, "The minimal number of instances in a node.", 1, 100000, 1);

type.setExpert(false); types.add(type); type = new ParameterTypeInt(PARAMETER_MIN_LEAF_SIZE, "The minimal number of instances in a leaf node to be left after prunning.", 1, 100000, 1); type.setExpert(false); types.add(type); type = new ParameterTypeDouble(PARAMETER_PRUNNING_SEVERITY, "The severity of the prune proces. Greater value means more prunning and smaller resulting tree.", 0.0001, 0.5, 0.0001); type.setExpert(false); types.add(type); return types; } /* (non-Javadoc) * @see com.rapidminer.operator.learner.Learner#learn(com.rapidminer.example.ExampleSet) */ @Override public TreeModel learn(ExampleSet exampleSet) throws OperatorException { modifyXML(); String fileLocation = getParameterAsString(getProblemFile()); File file = new File(fileLocation); Problem process = ProblemDecoder.decodeFromXMLToProces(file.getAbsolutePath()); TreeModel model; try{ GDTAlgorithm builder = new GDTAlgorithm(process); // learn tree Tree root = builder.learnTree(exampleSet); // create and return model model = new TreeModel(exampleSet, root); return model; }catch(Exception e){ throw new OperatorException(e.getMessage(),e); } } /* (non-Javadoc) * @see com.rapidminer.operator.learner.Learner#supportsCapability(com.rapidminer.operator.learner.Le

arnerCapability) */ @Override public boolean supportsCapability(LearnerCapability capability) { if (capability == com.rapidminer.operator.learner.LearnerCapability.BINOMINAL_ATTRIBUTES) return true; if (capability == com.rapidminer.operator.learner.LearnerCapability.POLYNOMINAL_ATTRIBUTES) return true; if (capability == com.rapidminer.operator.learner.LearnerCapability.NUMERICAL_ATTRIBUTES) return true; if (capability == com.rapidminer.operator.learner.LearnerCapability.POLYNOMINAL_CLASS) return true; if (capability == com.rapidminer.operator.learner.LearnerCapability.BINOMINAL_CLASS) return true; if (capability == com.rapidminer.operator.learner.LearnerCapability.WEIGHTED_EXAMPLES) return true; if (capability == com.rapidminer.operator.learner.LearnerCapability.NUMERICAL_CLASS) return true; return false; } /** * Gets the whibo file. * * @return whibo file string */ public String getProblemFile() { return PROBLEM_FILE; } /** * Sets the whibo file. * * @param whibo file string */ public void setProblemFile(String pf) { PROBLEM_FILE = pf; }

/* (non-Javadoc) * @see com.rapidminer.operator.learner.AbstractLearner#getOutputClasses() */ public Class<?>[] getOutputClasses() { return new Class[] { TreeModel.class }; }

/** * Used to take the parameters from the user interface, and to insert them into WhiBo xml file, as algorithm parameters * * @throws OperatorException */ private void modifyXML() throws OperatorException{ String fileLocation = getParameterAsString(getProblemFile()); int treeDepth = getParameterAsInt(PARAMETER_MAX_TREE_DEPTH); int minNodeSize = getParameterAsInt(PARAMETER_MIN_NODE_SIZE); int minLeafSize = getParameterAsInt(PARAMETER_MIN_LEAF_SIZE); double prunningSeverity = getParameterAsDouble(PARAMETER_PRUNNING_SEVERITY); setXMLParameter(fileLocation, "Tree_Depth", String.valueOf(treeDepth)); setXMLParameter(fileLocation, "Size", String.valueOf(minNodeSize)); setXMLParameter(fileLocation, "Size_Of_Leaf", String.valueOf(minLeafSize)); setXMLParameter(fileLocation, "Confidence_Level", String.valueOf(prunningSeverity)); } /** * Used by modifyXML() method to set the specific value of a parameter as a xml node inside the WhiBo file * * @param xmlFileLocation location of the WhiBo xml file * @param parameterName The name of the parameter to be set * @param newValue The value of the parameter to be set * * @throws OperatorException */ private void setXMLParameter(String xmlFileLocation, String parameterName, String newValue) throws OperatorException{ DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder; Document doc; try {

docBuilder = docFactory.newDocumentBuilder(); doc = docBuilder.parse(xmlFileLocation); NodeList list = doc.getElementsByTagName("void"); Node node; Node parameterNode; int end = list.getLength(); for (int i = 0; i < end; i++){ node = list.item(i); // Parameter Node Discovery if (node.getAttributes().item(0).getNodeValue().equals("nameOfParameter")){ if (node.getChildNodes().item(1).getFirstChild().getNodeValue().equals(parameterName)){ node = list.item(i+2); parameterNode = node.getChildNodes().item(1).getFirstChild(); parameterNode.setNodeValue(newValue); } } } Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //initialize StreamResult with File object to save to file //StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, new StreamResult(new FileOutputStream(xmlFileLocation))); } catch (Exception e) { throw new OperatorException("Error working with xml file"); } } }

Vous aimerez peut-être aussi