org.optaplanner.examples.nqueens.persistence.NQueensGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of optaplanner-examples Show documentation
Show all versions of optaplanner-examples Show documentation
OptaPlanner solves planning problems.
This lightweight, embeddable planning engine implements powerful and scalable algorithms
to optimize business resource scheduling and planning.
This module contains the examples which demonstrate how to use it in a normal Java application.
/*
* Copyright 2010 Red Hat, Inc. and/or its affiliates.
*
* 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.optaplanner.examples.nqueens.persistence;
import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.optaplanner.examples.common.app.LoggingMain;
import org.optaplanner.examples.common.persistence.AbstractSolutionImporter;
import org.optaplanner.examples.common.persistence.SolutionDao;
import org.optaplanner.examples.nqueens.domain.Column;
import org.optaplanner.examples.nqueens.domain.NQueens;
import org.optaplanner.examples.nqueens.domain.Queen;
import org.optaplanner.examples.nqueens.domain.Row;
public class NQueensGenerator extends LoggingMain {
private static final File outputDir = new File("data/nqueens/unsolved/");
protected SolutionDao solutionDao;
public static void main(String[] args) {
new NQueensGenerator().generate();
}
public void generate() {
solutionDao = new NQueensDao();
writeNQueens(4);
// writeNQueens(5);
// writeNQueens(6);
// writeNQueens(7);
writeNQueens(8);
// writeNQueens(9);
// writeNQueens(10);
// writeNQueens(11);
// writeNQueens(12);
// writeNQueens(13);
// writeNQueens(14);
// writeNQueens(15);
writeNQueens(16);
// writeNQueens(17);
// writeNQueens(18);
// writeNQueens(19);
// writeNQueens(20);
// writeNQueens(21);
// writeNQueens(22);
// writeNQueens(23);
// writeNQueens(24);
// writeNQueens(25);
// writeNQueens(26);
// writeNQueens(27);
// writeNQueens(28);
// writeNQueens(29);
// writeNQueens(30);
// writeNQueens(31);
writeNQueens(32);
writeNQueens(64);
writeNQueens(256);
// writeNQueens(10000);
// writeNQueens(100000);
// writeNQueens(1000000);
}
private void writeNQueens(int n) {
String outputFileName = n + "queens.xml";
File outputFile = new File(outputDir, outputFileName);
NQueens nQueens = createNQueens(n);
solutionDao.writeSolution(nQueens, outputFile);
}
public NQueens createNQueens(int n) {
NQueens nQueens = new NQueens();
nQueens.setId(0L);
nQueens.setN(n);
nQueens.setColumnList(createColumnList(nQueens));
nQueens.setRowList(createRowList(nQueens));
nQueens.setQueenList(createQueenList(nQueens));
BigInteger possibleSolutionSize = BigInteger.valueOf(nQueens.getN()).pow(nQueens.getN());
logger.info("NQueens {} has {} queens with a search space of {}.",
n, nQueens.getN(),
AbstractSolutionImporter.getFlooredPossibleSolutionSize(possibleSolutionSize));
return nQueens;
}
private List createColumnList(NQueens nQueens) {
int n = nQueens.getN();
List columnList = new ArrayList(n);
for (int i = 0; i < n; i++) {
Column column = new Column();
column.setId((long) i);
column.setIndex(i);
columnList.add(column);
}
return columnList;
}
private List createRowList(NQueens nQueens) {
int n = nQueens.getN();
List rowList = new ArrayList(n);
for (int i = 0; i < n; i++) {
Row row = new Row();
row.setId((long) i);
row.setIndex(i);
rowList.add(row);
}
return rowList;
}
private List createQueenList(NQueens nQueens) {
int n = nQueens.getN();
List queenList = new ArrayList(n);
long id = 0;
for (Column column : nQueens.getColumnList()) {
Queen queen = new Queen();
queen.setId(id);
id++;
queen.setColumn(column);
// Notice that we leave the PlanningVariable properties on null
queenList.add(queen);
}
return queenList;
}
}