org.optaplanner.benchmark.impl.ProblemBenchmarksFactory Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.optaplanner.benchmark.impl;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.optaplanner.benchmark.api.PlannerBenchmarkFactory;
import org.optaplanner.benchmark.config.ProblemBenchmarksConfig;
import org.optaplanner.benchmark.config.statistic.ProblemStatisticType;
import org.optaplanner.benchmark.config.statistic.SingleStatisticType;
import org.optaplanner.benchmark.impl.loader.FileProblemProvider;
import org.optaplanner.benchmark.impl.loader.InstanceProblemProvider;
import org.optaplanner.benchmark.impl.loader.ProblemProvider;
import org.optaplanner.benchmark.impl.result.PlannerBenchmarkResult;
import org.optaplanner.benchmark.impl.result.ProblemBenchmarkResult;
import org.optaplanner.benchmark.impl.result.SingleBenchmarkResult;
import org.optaplanner.benchmark.impl.result.SolverBenchmarkResult;
import org.optaplanner.benchmark.impl.result.SubSingleBenchmarkResult;
import org.optaplanner.benchmark.impl.statistic.ProblemStatistic;
import org.optaplanner.core.config.util.ConfigUtils;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import org.optaplanner.core.impl.solver.DefaultSolverFactory;
import org.optaplanner.persistence.common.api.domain.solution.SolutionFileIO;
public class ProblemBenchmarksFactory {
private final ProblemBenchmarksConfig config;
public ProblemBenchmarksFactory(ProblemBenchmarksConfig config) {
this.config = config;
}
public void buildProblemBenchmarkList(SolverBenchmarkResult solverBenchmarkResult,
Solution_[] extraProblems) {
PlannerBenchmarkResult plannerBenchmarkResult = solverBenchmarkResult.getPlannerBenchmarkResult();
List unifiedProblemBenchmarkResultList = plannerBenchmarkResult
.getUnifiedProblemBenchmarkResultList();
for (ProblemProvider problemProvider : buildProblemProviderList(
solverBenchmarkResult, extraProblems)) {
// 2 SolverBenchmarks containing equal ProblemBenchmarks should contain the same instance
ProblemBenchmarkResult newProblemBenchmarkResult = buildProblemBenchmark(
plannerBenchmarkResult, problemProvider);
ProblemBenchmarkResult problemBenchmarkResult;
int index = unifiedProblemBenchmarkResultList.indexOf(newProblemBenchmarkResult);
if (index < 0) {
problemBenchmarkResult = newProblemBenchmarkResult;
unifiedProblemBenchmarkResultList.add(problemBenchmarkResult);
} else {
problemBenchmarkResult = unifiedProblemBenchmarkResultList.get(index);
}
buildSingleBenchmark(solverBenchmarkResult, problemBenchmarkResult);
}
}
private List> buildProblemProviderList(
SolverBenchmarkResult solverBenchmarkResult, Solution_[] extraProblems) {
if (ConfigUtils.isEmptyCollection(config.getInputSolutionFileList()) && extraProblems.length == 0) {
throw new IllegalArgumentException(
"The solverBenchmarkResult (" + solverBenchmarkResult.getName() + ") has no problems.\n"
+ "Maybe configure at least 1 directly or indirectly by inheriting it.\n"
+ "Or maybe pass at least one problem to " + PlannerBenchmarkFactory.class.getSimpleName()
+ ".buildPlannerBenchmark().");
}
List> problemProviderList = new ArrayList<>(
extraProblems.length
+ (config.getInputSolutionFileList() == null ? 0 : config.getInputSolutionFileList().size()));
DefaultSolverFactory defaultSolverFactory =
new DefaultSolverFactory<>(solverBenchmarkResult.getSolverConfig());
SolutionDescriptor solutionDescriptor = defaultSolverFactory.getSolutionDescriptor();
int extraProblemIndex = 0;
for (Solution_ extraProblem : extraProblems) {
if (extraProblem == null) {
throw new IllegalStateException("The benchmark problem (" + extraProblem + ") is null.");
}
String problemName = "Problem_" + extraProblemIndex;
problemProviderList.add(new InstanceProblemProvider<>(problemName, solutionDescriptor, extraProblem));
extraProblemIndex++;
}
if (ConfigUtils.isEmptyCollection(config.getInputSolutionFileList())) {
if (config.getSolutionFileIOClass() != null) {
throw new IllegalArgumentException("Cannot use solutionFileIOClass (" + config.getSolutionFileIOClass()
+ ") with an empty inputSolutionFileList (" + config.getInputSolutionFileList() + ").");
}
} else {
SolutionFileIO solutionFileIO = buildSolutionFileIO();
for (File inputSolutionFile : config.getInputSolutionFileList()) {
if (!inputSolutionFile.exists()) {
throw new IllegalArgumentException("The inputSolutionFile (" + inputSolutionFile
+ ") does not exist.");
}
problemProviderList.add(new FileProblemProvider<>(solutionFileIO, inputSolutionFile));
}
}
return problemProviderList;
}
private SolutionFileIO buildSolutionFileIO() {
if (config.getSolutionFileIOClass() == null) {
throw new IllegalArgumentException(
"The solutionFileIOClass (" + config.getSolutionFileIOClass() + ") cannot be null.");
}
return (SolutionFileIO) ConfigUtils.newInstance(config, "solutionFileIOClass",
config.getSolutionFileIOClass());
}
private ProblemBenchmarkResult buildProblemBenchmark(
PlannerBenchmarkResult plannerBenchmarkResult, ProblemProvider problemProvider) {
ProblemBenchmarkResult problemBenchmarkResult = new ProblemBenchmarkResult<>(plannerBenchmarkResult);
problemBenchmarkResult.setName(problemProvider.getProblemName());
problemBenchmarkResult.setProblemProvider(problemProvider);
problemBenchmarkResult.setWriteOutputSolutionEnabled(
config.getWriteOutputSolutionEnabled() == null ? false : config.getWriteOutputSolutionEnabled());
List problemStatisticList;
if (config.getProblemStatisticEnabled() != null && !config.getProblemStatisticEnabled()) {
if (!ConfigUtils.isEmptyCollection(config.getProblemStatisticTypeList())) {
throw new IllegalArgumentException("The problemStatisticEnabled (" + config.getProblemStatisticEnabled()
+ ") and problemStatisticTypeList (" + config.getProblemStatisticTypeList()
+ ") cannot be used together.");
}
problemStatisticList = Collections.emptyList();
} else {
List problemStatisticTypeList_ = config.determineProblemStatisticTypeList();
problemStatisticList = new ArrayList<>(problemStatisticTypeList_.size());
for (ProblemStatisticType problemStatisticType : problemStatisticTypeList_) {
problemStatisticList.add(problemStatisticType.buildProblemStatistic(problemBenchmarkResult));
}
}
problemBenchmarkResult.setProblemStatisticList(problemStatisticList);
problemBenchmarkResult.setSingleBenchmarkResultList(new ArrayList<>());
return problemBenchmarkResult;
}
private void buildSingleBenchmark(SolverBenchmarkResult solverBenchmarkResult,
ProblemBenchmarkResult problemBenchmarkResult) {
SingleBenchmarkResult singleBenchmarkResult = new SingleBenchmarkResult(solverBenchmarkResult, problemBenchmarkResult);
buildSubSingleBenchmarks(singleBenchmarkResult, solverBenchmarkResult.getSubSingleCount());
List singleStatisticTypeList = config.determineSingleStatisticTypeList();
for (SubSingleBenchmarkResult subSingleBenchmarkResult : singleBenchmarkResult.getSubSingleBenchmarkResultList()) {
subSingleBenchmarkResult.setPureSubSingleStatisticList(new ArrayList<>(singleStatisticTypeList.size()));
}
for (SingleStatisticType singleStatisticType : singleStatisticTypeList) {
for (SubSingleBenchmarkResult subSingleBenchmarkResult : singleBenchmarkResult
.getSubSingleBenchmarkResultList()) {
subSingleBenchmarkResult.getPureSubSingleStatisticList().add(
singleStatisticType.buildPureSubSingleStatistic(subSingleBenchmarkResult));
}
}
singleBenchmarkResult.initSubSingleStatisticMaps();
solverBenchmarkResult.getSingleBenchmarkResultList().add(singleBenchmarkResult);
problemBenchmarkResult.getSingleBenchmarkResultList().add(singleBenchmarkResult);
}
private void buildSubSingleBenchmarks(SingleBenchmarkResult parent, int subSingleCount) {
List subSingleBenchmarkResultList = new ArrayList<>(subSingleCount);
for (int i = 0; i < subSingleCount; i++) {
SubSingleBenchmarkResult subSingleBenchmarkResult = new SubSingleBenchmarkResult(parent, i);
subSingleBenchmarkResultList.add(subSingleBenchmarkResult);
}
parent.setSubSingleBenchmarkResultList(subSingleBenchmarkResultList);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy