ai.libs.reduction.single.heterogeneous.bestofkrandom.BestOfKHeterogeneousReductionStumpExperimentRunnerWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mlplan-ext-reduction Show documentation
Show all versions of mlplan-ext-reduction Show documentation
This project provides an implementation of the AutoML tool ML-Plan.
package ai.libs.reduction.single.heterogeneous.bestofkrandom;
import java.io.File;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.api4.java.datastructure.kvstore.IKVStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ai.libs.jaicore.db.sql.SQLAdapter;
import ai.libs.reduction.single.ABestOfKReductionStumpExperimentRunnerWrapper;
import ai.libs.reduction.single.BestOfKAtRandomExperiment;
import ai.libs.reduction.single.MySQLReductionExperiment;
public class BestOfKHeterogeneousReductionStumpExperimentRunnerWrapper extends ABestOfKReductionStumpExperimentRunnerWrapper {
private static final Logger LOGGER = LoggerFactory.getLogger(BestOfKHeterogeneousReductionStumpExperimentRunnerWrapper.class);
private static final String TABLE_NAME = "reductionstumps_heterogeneous_random_bestofk";
private final Collection knownExperiments = new HashSet<>();
public BestOfKHeterogeneousReductionStumpExperimentRunnerWrapper(final String host, final String user, final String password, final String database, final int k, final int mccvRepeats) {
super(new SQLAdapter(host, user, password, database), TABLE_NAME, k, mccvRepeats);
try {
this.knownExperiments.addAll(this.getConductedExperiments());
} catch (SQLException e) {
LOGGER.error("Could not get the already conducted experiments from the database.", e);
}
}
public Collection getConductedExperiments() throws SQLException {
Collection experiments = new HashSet<>();
List rslist = this.getAdapter().getRowsOfTable(TABLE_NAME);
for (IKVStore rs : rslist) {
experiments.add(new MySQLReductionExperiment(rs.getAsInt("evaluation_id"), new BestOfKAtRandomExperiment(rs.getAsInt("seed"), rs.getAsString("dataset"), rs.getAsString("left_classifier"), rs.getAsString("inner_classifier"),
rs.getAsString("right_classifier"), rs.getAsInt("k"), rs.getAsInt("mccvrepeats"))));
}
return experiments;
}
public MySQLReductionExperiment createAndGetExperimentIfNotConducted(final int seed, final File dataFile, final String nameOfLeftClassifier, final String nameOfInnerClassifier, final String nameOfRightClassifier) {
/* first check whether exactly the same experiment (with the same seed) has been conducted previously */
BestOfKAtRandomExperiment exp = new BestOfKAtRandomExperiment(seed, dataFile.getAbsolutePath(), nameOfLeftClassifier, nameOfInnerClassifier, nameOfRightClassifier, this.getK(), this.getMCCVRepeats());
Optional existingExperiment = this.knownExperiments.stream().filter(e -> e.getExperiment().equals(exp)).findAny();
if (existingExperiment.isPresent()) {
return null;
}
Map map = new HashMap<>();
map.put("seed", seed);
map.put("dataset", dataFile.getAbsolutePath());
map.put("left_classifier", nameOfLeftClassifier);
map.put("inner_classifier", nameOfInnerClassifier);
map.put("right_classifier", nameOfRightClassifier);
map.put("k", this.getK());
map.put("mccvrepeats", this.getMCCVRepeats());
try {
int id = this.getAdapter().insert(TABLE_NAME, map)[0];
return new MySQLReductionExperiment(id, exp);
} catch (SQLException e) {
LOGGER.error("Could not create experiment entry", e);
return null;
}
}
}