org.integratedmodelling.engine.modelling.learning.AbstractWEKAProcessContextualizer Maven / Gradle / Ivy
The newest version!
package org.integratedmodelling.engine.modelling.learning;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.integratedmodelling.api.knowledge.IObservation;
import org.integratedmodelling.api.metadata.IMetadata;
import org.integratedmodelling.api.modelling.IActiveDirectObservation;
import org.integratedmodelling.api.modelling.IActiveProcess;
import org.integratedmodelling.api.modelling.IModel;
import org.integratedmodelling.api.modelling.IObservableSemantics;
import org.integratedmodelling.api.modelling.IState;
import org.integratedmodelling.api.modelling.contextualization.ILearningProcessContextualizer;
import org.integratedmodelling.api.modelling.resolution.IResolutionScope;
import org.integratedmodelling.api.modelling.scheduling.ITransition;
import org.integratedmodelling.api.monitoring.IMonitor;
import org.integratedmodelling.api.project.IProject;
import org.integratedmodelling.collections.Pair;
import org.integratedmodelling.exceptions.KlabException;
import org.integratedmodelling.exceptions.KlabValidationException;
import weka.classifiers.Classifier;
import weka.core.Instances;
/**
* Use to derive any WEKA learning process contextualizer that can be trained through an
* AIRFF instance set. Have getClassifier() return a new classifier. Initialize() builds
* the training set and trains the classifier, then computes the initialized model.
* Transitions re-train updateable classifiers by adding any new archetypes or rebuild the
* full model if the classifier isn't updateable. Models can be exported if supported.
* Automatically documents the computation.
* Calling model, project and function parameters are available to extending classes
* through the {@link #model}, {@link #project} and {@link #parameters} protected members.
*
* @author ferdinando.villa
* @param
*/
public abstract class AbstractWEKAProcessContextualizer
implements ILearningProcessContextualizer {
protected WEKALearningProcess learningProcess = null;
boolean canDispose = false;
// these are available to children so they don't have to redefine setContext()
protected IModel model;
protected IProject project;
protected Map parameters;
private Instances instances;
@Override
public Pair> getModel(String relativePath, File outputDirectory) {
return learningProcess.saveModel();
}
@Override
public Map initialize(IActiveProcess process, IActiveDirectObservation contextSubject, IResolutionScope resolutionContext, Map expectedInputs, Map expectedOutputs, IMonitor monitor)
throws KlabException {
canDispose = process.getScale().getTime() == null;
Classifier classifier = getClassifier();
this.learningProcess = createLearningProcess(classifier, monitor);
this.learningProcess
.initialize(process, contextSubject, resolutionContext, expectedInputs, expectedOutputs);
this.instances = this.learningProcess.train(ITransition.INITIALIZATION);
Map ret = new HashMap<>();
ret.put(this.learningProcess.getOutputState().getMetadata()
.getString(IMetadata.DC_LABEL), this.learningProcess.getOutputState());
this.learningProcess.runModel(ITransition.INITIALIZATION, this.instances);
return ret;
}
protected WEKALearningProcess createLearningProcess(Classifier classifier, IMonitor monitor) {
return new WEKALearningProcess(classifier, monitor);
}
@Override
public Map compute(ITransition transition, Map inputs)
throws KlabException {
Map ret = new HashMap<>();
canDispose = transition.isLast();
/*
* TODO if there are new archetypes, re-train
*/
if (!inputs.isEmpty()) {
this.learningProcess.runModel(transition, this.instances);
}
return ret;
}
@Override
public boolean canDispose() {
return canDispose;
}
@Override
public void setContext(Map parameters, IModel model, IProject project)
throws KlabValidationException {
this.model = model;
this.project = project;
this.parameters = parameters;
}
protected abstract Classifier getClassifier();
}