Skip to content

Commit

Permalink
Revert "training: day 3" - This wasn't ment to be committed (should h…
Browse files Browse the repository at this point in the history
…ave been under DNC - Do Not Commit)

This reverts commit f585125.
  • Loading branch information
ge0ffrey committed May 5, 2015
1 parent 3a6a330 commit b0f50c1
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 24 deletions.
Expand Up @@ -54,7 +54,7 @@ public static String toDisplayString(NQueens nQueens) {
throw new IllegalStateException("The queenList is not in the expected order.");
}
displayString.append(" ");
if (queen.getRow() != null && queen.getRow() == row) {
if (queen.getRow() != null && queen.getRow().getIndex() == row) {
displayString.append("Q");
} else {
displayString.append("_");
Expand Down
Expand Up @@ -25,8 +25,6 @@
import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.solution.Solution;
import org.optaplanner.core.api.domain.valuerange.CountableValueRange;
import org.optaplanner.core.api.domain.valuerange.ValueRangeFactory;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.score.buildin.simple.SimpleScore;
import org.optaplanner.core.impl.score.buildin.simple.SimpleScoreDefinition;
Expand Down Expand Up @@ -65,18 +63,11 @@ public void setColumnList(List<Column> columnList) {
this.columnList = columnList;
}


@ValueRangeProvider(id = "rowRange")
public List<Row> getRowList() {
return rowList;
}

@ValueRangeProvider(id = "rowRange")
public CountableValueRange extractRowRange() {
return ValueRangeFactory.createIntValueRange(0, n);
}



public void setRowList(List<Row> rowList) {
this.rowList = rowList;
}
Expand Down
Expand Up @@ -30,7 +30,7 @@ public class Queen extends AbstractPersistable {
private Column column;

// Planning variables: changes during planning, between score calculations.
private Integer row;
private Row row;

public Column getColumn() {
return column;
Expand All @@ -40,12 +40,13 @@ public void setColumn(Column column) {
this.column = column;
}

@PlanningVariable(valueRangeProviderRefs = {"rowRange"})
public Integer getRow() {
@PlanningVariable(valueRangeProviderRefs = {"rowRange"},
strengthWeightFactoryClass = RowStrengthWeightFactory.class)
public Row getRow() {
return row;
}

public void setRow(Integer row) {
public void setRow(Row row) {
this.row = row;
}

Expand All @@ -61,7 +62,7 @@ public int getRowIndex() {
if (row == null) {
return Integer.MIN_VALUE;
}
return row;
return row.getIndex();
}

public int getAscendingDiagonalIndex() {
Expand Down
@@ -0,0 +1,88 @@
/*
* Copyright 2010 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.optaplanner.examples.nqueens.solver.move;

import java.util.Collection;
import java.util.Collections;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.optaplanner.core.impl.heuristic.move.AbstractMove;
import org.optaplanner.core.impl.heuristic.move.Move;
import org.optaplanner.core.impl.score.director.ScoreDirector;
import org.optaplanner.examples.nqueens.domain.Queen;
import org.optaplanner.examples.nqueens.domain.Row;

public class RowChangeMove extends AbstractMove {

private Queen queen;
private Row toRow;

public RowChangeMove(Queen queen, Row toRow) {
this.queen = queen;
this.toRow = toRow;
}

public boolean isMoveDoable(ScoreDirector scoreDirector) {
return !ObjectUtils.equals(queen.getRow(), toRow);
}

public Move createUndoMove(ScoreDirector scoreDirector) {
return new RowChangeMove(queen, queen.getRow());
}

public void doMove(ScoreDirector scoreDirector) {
scoreDirector.beforeVariableChanged(queen, "row"); // before changes are made
queen.setRow(toRow);
scoreDirector.afterVariableChanged(queen, "row"); // after changes are made
}

public Collection<? extends Object> getPlanningEntities() {
return Collections.singletonList(queen);
}

public Collection<? extends Object> getPlanningValues() {
return Collections.singletonList(toRow);
}

public boolean equals(Object o) {
if (this == o) {
return true;
} else if (o instanceof RowChangeMove) {
RowChangeMove other = (RowChangeMove) o;
return new EqualsBuilder()
.append(queen, other.queen)
.append(toRow, other.toRow)
.isEquals();
} else {
return false;
}
}

public int hashCode() {
return new HashCodeBuilder()
.append(queen)
.append(toRow)
.toHashCode();
}

public String toString() {
return queen + " {" + queen.getRow() + " -> " + toRow + "}";
}

}
@@ -0,0 +1,41 @@
/*
* Copyright 2010 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.optaplanner.examples.nqueens.solver.move.factory;

import java.util.ArrayList;
import java.util.List;

import org.optaplanner.core.impl.heuristic.move.Move;
import org.optaplanner.core.impl.heuristic.selector.move.factory.MoveListFactory;
import org.optaplanner.examples.nqueens.domain.NQueens;
import org.optaplanner.examples.nqueens.domain.Queen;
import org.optaplanner.examples.nqueens.domain.Row;
import org.optaplanner.examples.nqueens.solver.move.RowChangeMove;

public class RowChangeMoveFactory implements MoveListFactory<NQueens> {

public List<Move> createMoveList(NQueens nQueens) {
List<Move> moveList = new ArrayList<Move>();
for (Queen queen : nQueens.getQueenList()) {
for (Row toRow : nQueens.getRowList()) {
moveList.add(new RowChangeMove(queen, toRow));
}
}
return moveList;
}

}
Expand Up @@ -80,7 +80,7 @@ public void afterEntityRemoved(Object entity) {
}

private void insert(Queen queen) {
Integer row = queen.getRow();
Row row = queen.getRow();
if (row != null) {
int rowIndex = queen.getRowIndex();
List<Queen> rowIndexList = rowIndexMap.get(rowIndex);
Expand All @@ -96,7 +96,7 @@ private void insert(Queen queen) {
}

private void retract(Queen queen) {
Integer row = queen.getRow();
Row row = queen.getRow();
if (row != null) {
List<Queen> rowIndexList = rowIndexMap.get(queen.getRowIndex());
rowIndexList.remove(queen);
Expand Down
Expand Up @@ -63,7 +63,7 @@ public void afterEntityRemoved(Object entity) {
}

private void insert(Queen queen) {
Integer row = queen.getRow();
Row row = queen.getRow();
if (row != null) {
for (Queen otherQueen : insertedQueenList) {
if (queen.getRowIndex() == otherQueen.getRowIndex()) {
Expand All @@ -81,7 +81,7 @@ private void insert(Queen queen) {
}

private void retract(Queen queen) {
Integer row = queen.getRow();
Row row = queen.getRow();
if (row != null) {
insertedQueenList.remove(queen);
for (Queen otherQueen : insertedQueenList) {
Expand Down
@@ -0,0 +1,82 @@
/*
* Copyright 2014 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.optaplanner.examples.nqueens.solver.solution;

import java.util.List;

import org.optaplanner.core.impl.phase.custom.CustomPhaseCommand;
import org.optaplanner.core.impl.score.director.ScoreDirector;
import org.optaplanner.examples.nqueens.domain.NQueens;
import org.optaplanner.examples.nqueens.domain.Queen;
import org.optaplanner.examples.nqueens.domain.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Because N Queens is not NP-complete or NP-hard, it can be cheated.
* For this reason, N queens should not be used for benchmarking purposes.
* <p/>
* This class solves any N Queens instance using a polynomial time algorithm
* (<a href="http://en.wikipedia.org/wiki/Eight_queens_puzzle#Explicit_solutions">>explicit solutions algorithm</a>).
*/
public class CheatingNQueensPhaseCommand implements CustomPhaseCommand {

protected final transient Logger logger = LoggerFactory.getLogger(getClass());

public void changeWorkingSolution(ScoreDirector scoreDirector) {
NQueens nQueens = (NQueens) scoreDirector.getWorkingSolution();
int n = nQueens.getN();
List<Queen> queenList = nQueens.getQueenList();
List<Row> rowList = nQueens.getRowList();

if (n % 2 == 1) {
Queen a = queenList.get(n - 1);
scoreDirector.beforeVariableChanged(a, "row");
a.setRow(rowList.get(n - 1));
scoreDirector.afterVariableChanged(a, "row");
n--;
}
int halfN = n / 2;
if (n % 6 != 2) {
for (int i = 0; i < halfN; i++) {
Queen a = queenList.get(i);
scoreDirector.beforeVariableChanged(a, "row");
a.setRow(rowList.get((2 * i) + 1));
scoreDirector.afterVariableChanged(a, "row");

Queen b = queenList.get(halfN + i);
scoreDirector.beforeVariableChanged(b, "row");
b.setRow(rowList.get(2 * i));
scoreDirector.afterVariableChanged(b, "row");
}
} else {
for (int i = 0; i < halfN; i++) {
Queen a = queenList.get(i);
scoreDirector.beforeVariableChanged(a, "row");
a.setRow(rowList.get((halfN + (2 * i) - 1) % n));
scoreDirector.afterVariableChanged(a, "row");

Queen b = queenList.get(n - i - 1);
scoreDirector.beforeVariableChanged(b, "row");
b.setRow(rowList.get(n - 1 - ((halfN + (2 * i) - 1) % n)));
scoreDirector.afterVariableChanged(b, "row");
}
}

}

}
Expand Up @@ -77,7 +77,7 @@ public void resetPanel(Solution solution) {
throw new IllegalStateException("The queenList is not in the expected order.");
}
String toolTipText = "<html>Row " + row + "<br/>Column " + column + "</html>";
if (queen.getRow() != null && queen.getRow().intValue() == row) {
if (queen.getRow() != null && queen.getRow().getIndex() == row) {
JButton button = new JButton(new QueenAction(queen));
button.setMinimumSize(new Dimension(20, 20));
button.setPreferredSize(new Dimension(20, 20));
Expand Down
Expand Up @@ -51,7 +51,7 @@
<!--</localSearch>-->

<!-- To super scale out, replace <constructionHeuristic> and <localSearch> with this configuration: -->
<!--<customPhase>-->
<!--<customPhaseCommandClass>org.optaplanner.examples.nqueens.solver.solution.CheatingNQueensPhaseCommand</customPhaseCommandClass>-->
<!--</customPhase>-->
<customPhase>
<customPhaseCommandClass>org.optaplanner.examples.nqueens.solver.solution.CheatingNQueensPhaseCommand</customPhaseCommandClass>
</customPhase>
</solver>

0 comments on commit b0f50c1

Please sign in to comment.