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

org.drools.planner.benchmark.config.ProblemBenchmarksConfig Maven / Gradle / Ivy

Go to download

Drools Planner optimizes automated planning by combining metaheuristic search algorithms with rule engine powered score calculation. This is the drools-planner-core module which contains metaheuristic algorithms.

There is a newer version: 6.0.0.Alpha9
Show newest version
/*
 * Copyright 2011 JBoss Inc
 *
 * Licensed 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.drools.planner.benchmark.config;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import org.apache.commons.io.FilenameUtils;
import org.drools.planner.benchmark.api.ProblemIO;
import org.drools.planner.benchmark.core.PlannerBenchmarkResult;
import org.drools.planner.benchmark.core.ProblemBenchmark;
import org.drools.planner.benchmark.core.SolverBenchmark;
import org.drools.planner.benchmark.core.XStreamProblemIO;
import org.drools.planner.benchmark.core.statistic.ProblemStatistic;
import org.drools.planner.benchmark.core.statistic.ProblemStatisticType;
import org.drools.planner.config.util.ConfigUtils;

@XStreamAlias("problemBenchmarks")
public class ProblemBenchmarksConfig {

    private Class problemIOClass = null;
    @XStreamImplicit(itemFieldName = "xstreamAnnotatedClass")
    private List xstreamAnnotatedClassList = null;

    @XStreamImplicit(itemFieldName = "inputSolutionFile")
    private List inputSolutionFileList = null;
    
    @XStreamImplicit(itemFieldName = "problemStatisticType")
    private List problemStatisticTypeList = null;

    public Class getProblemIOClass() {
        return problemIOClass;
    }

    public void setProblemIOClass(Class problemIOClass) {
        this.problemIOClass = problemIOClass;
    }

    public List getXstreamAnnotatedClassList() {
        return xstreamAnnotatedClassList;
    }

    public void setXstreamAnnotatedClassList(List xstreamAnnotatedClassList) {
        this.xstreamAnnotatedClassList = xstreamAnnotatedClassList;
    }

    public List getInputSolutionFileList() {
        return inputSolutionFileList;
    }

    public void setInputSolutionFileList(List inputSolutionFileList) {
        this.inputSolutionFileList = inputSolutionFileList;
    }

    public List getProblemStatisticTypeList() {
        return problemStatisticTypeList;
    }

    public void setProblemStatisticTypeList(List problemStatisticTypeList) {
        this.problemStatisticTypeList = problemStatisticTypeList;
    }

    // ************************************************************************
    // Builder methods
    // ************************************************************************

    public List buildProblemBenchmarkList(
            List unifiedProblemBenchmarkList, SolverBenchmark solverBenchmark) {
        validate(solverBenchmark);
        ProblemIO problemIO = buildProblemIO();
        List problemBenchmarkList = new ArrayList(
                inputSolutionFileList.size());
        for (File inputSolutionFile : inputSolutionFileList) {
            // 2 SolverBenchmarks containing equal PProblemBenchmarks should contain the same instance
            ProblemBenchmark newProblemBenchmark = buildProblemBenchmark(
                    problemIO, inputSolutionFile);
            ProblemBenchmark problemBenchmark;
            int index = unifiedProblemBenchmarkList.indexOf(newProblemBenchmark);
            if (index < 0) {
                problemBenchmark = newProblemBenchmark;
                unifiedProblemBenchmarkList.add(problemBenchmark);
            } else {
                problemBenchmark = unifiedProblemBenchmarkList.get(index);
            }
            addPlannerBenchmarkResult(solverBenchmark, problemBenchmark);
            problemBenchmarkList.add(problemBenchmark);
        }
        return problemBenchmarkList;
    }

    private void validate(SolverBenchmark solverBenchmark) {
        if (inputSolutionFileList == null || inputSolutionFileList.isEmpty()) {
            throw new IllegalArgumentException(
                    "Configure at least 1  for the solverBenchmark (" + solverBenchmark.getName()
                            + ") directly or indirectly by inheriting it.");
        }
    }

    private ProblemIO buildProblemIO() {
        if (problemIOClass != null && xstreamAnnotatedClassList != null) {
            throw new IllegalArgumentException("Cannot use problemIOClass (" + problemIOClass
                    + ") and xstreamAnnotatedClassList (" + xstreamAnnotatedClassList + ") together.");
        }
        if (problemIOClass != null) {
            try {
                return problemIOClass.newInstance();
            } catch (InstantiationException e) {
                throw new IllegalArgumentException("problemIOClass (" + problemIOClass.getName()
                        + ") does not have a public no-arg constructor", e);
            } catch (IllegalAccessException e) {
                throw new IllegalArgumentException("problemIOClass (" + problemIOClass.getName()
                        + ") does not have a public no-arg constructor", e);
            }
        } else {
            Class[] xstreamAnnotatedClasses;
            if (xstreamAnnotatedClassList != null) {
                xstreamAnnotatedClasses = xstreamAnnotatedClassList.toArray(new Class[xstreamAnnotatedClassList.size()]);
            } else {
                xstreamAnnotatedClasses = new Class[0];
            }
            return new XStreamProblemIO(xstreamAnnotatedClasses);
        }
    }

    private ProblemBenchmark buildProblemBenchmark(
            ProblemIO problemIO, File inputSolutionFile) {
        ProblemBenchmark problemBenchmark = new ProblemBenchmark();
        String name = FilenameUtils.getBaseName(inputSolutionFile.getName());
        problemBenchmark.setName(name);
        problemBenchmark.setProblemIO(problemIO);
        problemBenchmark.setInputSolutionFile(inputSolutionFile);
        // outputSolutionFilesDirectory is set by DefaultPlannerBenchmark
        List problemStatisticList = new ArrayList(
                problemStatisticTypeList == null ? 0 : problemStatisticTypeList.size());
        if (problemStatisticTypeList != null) {
            for (ProblemStatisticType problemStatisticType : problemStatisticTypeList) {
                problemStatisticList.add(problemStatisticType.create());
            }
        }
        problemBenchmark.setProblemStatisticList(problemStatisticList);
        problemBenchmark.setPlannerBenchmarkResultList(new ArrayList());
        return problemBenchmark;
    }

    private void addPlannerBenchmarkResult(
            SolverBenchmark solverBenchmark, ProblemBenchmark problemBenchmark) {
        PlannerBenchmarkResult result = new PlannerBenchmarkResult();
        result.setSolverBenchmark(solverBenchmark);
        solverBenchmark.getPlannerBenchmarkResultList().add(result);
        result.setProblemBenchmark(problemBenchmark);
        problemBenchmark.getPlannerBenchmarkResultList().add(result);
    }

    public void inherit(ProblemBenchmarksConfig inheritedConfig) {
        problemIOClass = ConfigUtils.inheritOverwritableProperty(problemIOClass,
                inheritedConfig.getProblemIOClass());
        xstreamAnnotatedClassList = ConfigUtils.inheritMergeableListProperty(xstreamAnnotatedClassList,
                inheritedConfig.getXstreamAnnotatedClassList());
        inputSolutionFileList = ConfigUtils.inheritMergeableListProperty(inputSolutionFileList,
                inheritedConfig.getInputSolutionFileList());
        problemStatisticTypeList = ConfigUtils.inheritMergeableListProperty(problemStatisticTypeList,
                inheritedConfig.getProblemStatisticTypeList());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy