Skip to content

Commit

Permalink
Add ISolver interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricksladkey committed Mar 3, 2010
1 parent 343fb38 commit 57633cf
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Engine/Deadlocks/ForewardsSubsetSolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Sokoban.Engine.Deadlocks
{
class ForewardsSubsetSolver : SubsetSolver
{
private Solver solver;
private ISolver solver;

public ForewardsSubsetSolver(Level level)
: base(level)
Expand Down
2 changes: 1 addition & 1 deletion Engine/Deadlocks/TableBasedDeadlockFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private void AddDeadlockEntry(Array2D<bool> map, params Coordinate2D[] coords)
private void VerifyDeadlock(Array2D<bool> map, params Coordinate2D[] coords)
{
Level subsetLevel = LevelUtils.GetSubsetLevel(level, false, coords);
Solver solver = Solver.CreateInstance(SolverAlgorithm.LowerBound);
ISolver solver = Solver.CreateInstance(SolverAlgorithm.LowerBound);
solver.Level = subsetLevel;
solver.OptimizeMoves = false;
solver.OptimizePushes = true;
Expand Down
1 change: 1 addition & 0 deletions Engine/Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<Compile Include="Deadlocks\DeadlockFinder.cs" />
<Compile Include="Deadlocks\DeadlockUtils.cs" />
<Compile Include="Solvers\CurrentState.cs" />
<Compile Include="Solvers\ISolver.cs" />
<Compile Include="Solvers\MoveState.cs" />
<Compile Include="Solvers\NodeFlags.cs" />
<Compile Include="Solvers\NodeHelper.cs" />
Expand Down
47 changes: 47 additions & 0 deletions Engine/Solvers/ISolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2010 by Rick Sladkey
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General License for more details.
*
* You should have received a copy of the GNU General License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sokoban.Engine.Levels;
using Sokoban.Engine.Utilities;

namespace Sokoban.Engine.Solvers
{
public interface ISolver
{
string DeadlocksDirectory { get; set; }
bool CollectSolutions { get; set; }
bool CalculateDeadlocks { get; set; }
bool HardCodedDeadlocks { get; set; }
Level Level { get; set; }
int MaximumNodes { get; set; }
int InitialCapacity { get; set; }
bool OptimizeMoves { get; set; }
bool OptimizePushes { get; set; }
bool DetectNoInfluencePushes { get; set; }
bool Validate { get; set; }
bool Verbose { get; set; }
CancelInfo CancelInfo { get; set; }
MoveList Solution { get; }
string Error { get; }
bool Solve();
}
}
6 changes: 3 additions & 3 deletions Engine/Solvers/Solver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@

namespace Sokoban.Engine.Solvers
{
public abstract class Solver
public abstract class Solver : ISolver
{
public static Solver CreateInstance()
public static ISolver CreateInstance()
{
return CreateInstance(SolverAlgorithm.BruteForce);
}

public static Solver CreateInstance(SolverAlgorithm algorithm)
public static ISolver CreateInstance(SolverAlgorithm algorithm)
{
switch (algorithm)
{
Expand Down
14 changes: 7 additions & 7 deletions LevelGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private class State
public int Index;
public int Seed;
public Random Random;
public Solver Solver;
public ISolver Solver;
public Logger Log;
public int CurrentDesign;
public int CurrentSize;
Expand Down Expand Up @@ -142,7 +142,7 @@ public class LevelInfo
private Semaphore semaphore;
private int designCount;
private DesignInfo[] designInfo;
private Queue<Solver> solverQueue;
private Queue<ISolver> solverQueue;

public string OutputFile
{
Expand Down Expand Up @@ -514,7 +514,7 @@ public Generator()
rejectSokobanOnTarget = false;
moveSokoban = true;
outputFile = "LevelGenerator.xsb";
solverQueue = new Queue<Solver>();
solverQueue = new Queue<ISolver>();
}

private void Initialize()
Expand Down Expand Up @@ -561,7 +561,7 @@ private void Initialize()

private MoveList Solve(State state, Level level, bool optimizeMoves, bool optimizePushes)
{
Solver solver = state.Solver;
ISolver solver = state.Solver;
solver.OptimizeMoves = optimizeMoves;
solver.OptimizePushes = optimizePushes;
solver.MaximumNodes = nodes;
Expand All @@ -583,7 +583,7 @@ private MoveList Solve(State state, Level level, bool optimizeMoves, bool optimi
return null;
}

private Solver GetSolver()
private ISolver GetSolver()
{
if (!reuseSolver)
{
Expand All @@ -601,7 +601,7 @@ private Solver GetSolver()
}
}

private void ReleaseSolver(Solver solver)
private void ReleaseSolver(ISolver solver)
{
if (!reuseSolver)
{
Expand All @@ -614,7 +614,7 @@ private void ReleaseSolver(Solver solver)
}
}

private Solver CreateSolver()
private ISolver CreateSolver()
{
return Solver.CreateInstance(SolverAlgorithm.BruteForce);
}
Expand Down
4 changes: 2 additions & 2 deletions LevelSorter/Sorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class LevelInfo

TransformationInvariantLevelSet set;
List<LevelInfo> results;
private Solver persistentSolver;
private ISolver persistentSolver;

public string OutputFile
{
Expand Down Expand Up @@ -568,7 +568,7 @@ private MoveList Solve(Level level, bool optimizeMoves, bool optimizePushes)
{
try
{
Solver solver = reuseSolver ? persistentSolver : Solver.CreateInstance();
ISolver solver = reuseSolver ? persistentSolver : Solver.CreateInstance();
solver.OptimizeMoves = optimizeMoves;
solver.OptimizePushes = optimizePushes;
solver.MaximumNodes = nodes;
Expand Down
2 changes: 1 addition & 1 deletion Player/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ private void SolubleInformation()
string soluble = "unknown";
try
{
Solver solver = Solver.CreateInstance();
ISolver solver = Solver.CreateInstance();
solver.OptimizeMoves = false;
solver.OptimizePushes = false;
solver.Level = level;
Expand Down
2 changes: 1 addition & 1 deletion Player/SolverDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace Sokoban.Player
public partial class SolverDialog : Form
{
private MainWindow mainWindow;
private Solver solver;
private ISolver solver;
private Thread solverThread;
private MoveList solution;
private bool solving;
Expand Down
2 changes: 1 addition & 1 deletion SolverTool/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace Sokoban.SolverTool
{
public class Tool
{
private Solver solver;
private ISolver solver;

public Tool()
{
Expand Down
4 changes: 2 additions & 2 deletions UnitTests/SolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class SolverTests
[Test]
public void SolverInstanceTest()
{
Solver solver = Solver.CreateInstance();
ISolver solver = Solver.CreateInstance();
}

[Test]
Expand Down Expand Up @@ -205,7 +205,7 @@ public void Nabokosmos1Test()
public void SolveTwiceTest()
{
Level level = TestUtils.LoadLevelSetLevel(TestData.NabokosmosLevelSetFile, 1);
Solver solver = Solver.CreateInstance();
ISolver solver = Solver.CreateInstance();
solver.Level = level;
solver.OptimizeMoves = false;
solver.OptimizePushes = true;
Expand Down
4 changes: 2 additions & 2 deletions UnitTests/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static Level LoadLevelSetLevel(string levelSetFile, int level)

public static MoveList QuickSolve(Level level, bool optimizeMoves, bool optimizePushes, bool useLowerBound)
{
Solver solver = Solver.CreateInstance(useLowerBound ? SolverAlgorithm.BruteForce : SolverAlgorithm.BruteForce);
ISolver solver = Solver.CreateInstance(useLowerBound ? SolverAlgorithm.BruteForce : SolverAlgorithm.BruteForce);
Log.DebugPrint("Solving level {0}", level.Name);
solver.Level = level;
solver.OptimizeMoves = optimizeMoves;
Expand All @@ -176,7 +176,7 @@ public static List<MoveList> QuickSolve(IEnumerable<Level> levels, bool optimize
if (reuseSolver)
{
List<MoveList> solutions = new List<MoveList>();
Solver solver = Solver.CreateInstance(useLowerBound ? SolverAlgorithm.BruteForce : SolverAlgorithm.BruteForce);
ISolver solver = Solver.CreateInstance(useLowerBound ? SolverAlgorithm.BruteForce : SolverAlgorithm.BruteForce);
int index = 0;
foreach (Level level in levels)
{
Expand Down

0 comments on commit 57633cf

Please sign in to comment.