All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ai.libs.jaicore.search.algorithms.standard.bestfirst.StandardBestFirstFactory Maven / Gradle / Ivy

package ai.libs.jaicore.search.algorithms.standard.bestfirst;

import java.util.Objects;

import org.api4.java.ai.graphsearch.problem.implicit.graphgenerator.IPathGoalTester;
import org.api4.java.ai.graphsearch.problem.pathsearch.pathevaluation.IPathEvaluator;
import org.api4.java.common.control.ILoggingCustomizable;
import org.api4.java.datastructure.graph.implicit.IGraphGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ai.libs.jaicore.search.probleminputs.GraphSearchWithSubpathEvaluationsInput;

public class StandardBestFirstFactory> extends BestFirstFactory, N, A, V> implements ILoggingCustomizable {

	private Logger logger = LoggerFactory.getLogger(StandardBestFirstFactory.class);

	public void setNodeEvaluator(final IPathEvaluator nodeEvaluator) {
		GraphSearchWithSubpathEvaluationsInput problem = this.getInput();
		IGraphGenerator gg = problem != null ? problem.getGraphGenerator() : null;
		IPathGoalTester gt = problem != null ? problem.getGoalTester() : null;
		this.setProblemInput(new GraphSearchWithSubpathEvaluationsInput<>(gg, gt, nodeEvaluator));
	}

	public void setGraphGenerator(final IGraphGenerator graphGenerator) {
		GraphSearchWithSubpathEvaluationsInput problem = this.getInput();
		Objects.requireNonNull(problem);
		IPathGoalTester gt = problem.getGoalTester();
		IPathEvaluator evaluator = problem.getPathEvaluator();
		this.setProblemInput(new GraphSearchWithSubpathEvaluationsInput<>(graphGenerator, gt, evaluator));
	}

	@Override
	public BestFirst, N, A, V> getAlgorithm() {
		if (this.getInput().getGraphGenerator() == null) {
			throw new IllegalStateException("Cannot produce BestFirst searches before the graph generator is set in the problem.");
		}
		if (this.getInput().getPathEvaluator() == null) {
			throw new IllegalStateException("Cannot produce BestFirst searches before the node evaluator is set.");
		}

		/* determine search problem */
		GraphSearchWithSubpathEvaluationsInput problem = this.getInput();
		this.logger.debug("Created algorithm input with\n\tgraph generator: {}\n\tnode evaluator: {}", problem.getGraphGenerator(), problem.getPathEvaluator());
		BestFirst, N, A, V> search = new BestFirst<>(problem);
		search.setTimeoutForComputationOfF(this.getTimeoutForFInMS(), this.getTimeoutEvaluator());
		if (this.getLoggerName() != null && this.getLoggerName().length() > 0) {
			search.setLoggerName(this.getLoggerName());
		}
		return search;
	}

	@Override
	public String getLoggerName() {
		return this.logger.getName();
	}

	@Override
	public void setLoggerName(final String name) {
		this.logger = LoggerFactory.getLogger(name);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy