Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions Interfaces/SolverInterface.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using API.Interfaces.JSON_Objects;
using API.Interfaces.JSON_Objects.Graphs;
using API.Tools;

namespace API.Interfaces;

Expand All @@ -10,6 +7,21 @@ interface ISolver {
string source {get;}
string[] contributors { get; }

bool timerHasExpired { get; set; }

/// <summary>
/// Called when the run time timer for this solver has run out. The solver is
/// expected to check the "timerHasExpired" periodically and abandon the solution
/// if the flag is found to be true.
/// </summary>
public void TimerExpired()
{
timerHasExpired = true;
}
public void ResetTimer()
{
timerHasExpired = false;
}
string solve(string problem);

List<string> GetSteps(string instance)
Expand All @@ -23,10 +35,29 @@ string ISolver.solve(string problem) {
// Should there be some sort of contraint that assures there is a constructor
// that matches the signature of a single `string` argument?
// Perhaps a static `FromInstance(string instance)` method for `IProblem` will work.
return solve((T)Activator.CreateInstance(typeof(T), problem));
T problemInstance = (T)Activator.CreateInstance(typeof(T), problem);
if (problemInstance == null)
throw new ArgumentException($"Could not create problem instance for {problem}.");

string result = "no solution found";
Thread thread = new Thread(() => result = solve(problemInstance));

// start the thread
ResetTimer();
thread.Start();

// after 5 seconds w/out finishing, tell the thread it's time
// is up and wait for it to finish up.
// XXX make the solution time configurable
if (thread.Join(new TimeSpan(0, 0, 5)) == false)
{
TimerExpired();
thread.Join();
}
return result;
}
string solve(T problem);

string solve(T problem);

List<string> ISolver.GetSteps(string instance)
{
Expand Down
1 change: 1 addition & 0 deletions Problems/NPComplete/NPC_ARCSET/Solvers/ArcSetBruteForce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ArcSetBruteForce : ISolver<ARCSET> {
public string source {get;} = "";

public string[] contributors {get;} = { "Alex Diviney","Caleb Eardley","Russell Phillips"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public ArcSetBruteForce() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class BernsteinVaziraniClassicalSolver : ISolver<BERNSTEINVAZIRANI> {
public string solverDefinition { get; } = "This is a classical verifier for the Bernstein-Vazirani problem which runs in O(n) time.";
public string source {get;} = "";
public string[] contributors {get;} = { "Jason L. Wright" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public BernsteinVaziraniClassicalSolver() {}
Expand Down
1 change: 1 addition & 0 deletions Problems/NPComplete/NPC_CLIQUE/Solvers/CliqueBruteForce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CliqueBruteForce : ISolver<CLIQUE> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Clique problem";
public string source {get;} = "";
public string[] contributors {get;} = {"Caleb Eardley", "Kaden Marchetti"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public CliqueBruteForce() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CliqueCoverBruteForce : ISolver<CLIQUECOVER> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Clique Cover problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public CliqueCoverBruteForce()
Expand Down
3 changes: 2 additions & 1 deletion Problems/NPComplete/NPC_CUT/Solvers/CutBruteForce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class CutBruteForce : ISolver<CUT> {
public string solverDefinition {get;} = "This is a brute force solver for the Cut problem";
public string source {get;} = "";
public string[] contributors {get;} = {"Andrija Sevaljevic"};
public bool timerHasExpired { get; set; }

public CutBruteForce() {
public CutBruteForce() {

}
private long factorial(long x){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class DeutschClassicalSolver : ISolver<DEUTSCH> {
public string solverDefinition {get;} = "This is a classical solver for the Deutsch Problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Jason L. Wright" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public DeutschClassicalSolver() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DeutschJozsaClassicalSolver : ISolver<DEUTSCHJOZSA> {
public string solverDefinition {get;} = "This is a classical solver for the Deutsch Jozsa Problem";
public string source {get;} = "TODO";
public string[] contributors {get;} = { "George Lake", "Eric Hill", "Paul Gilbreath", "Max Gruenwoldt", "Alex Svancara" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public DeutschJozsaClassicalSolver() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class DirectedHamiltonianBruteForce : ISolver<DIRECTEDHAMILTONIAN> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Directed Hamiltonian Path problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public DirectedHamiltonianBruteForce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ThreeDimensionalMatchingBruteForce : ISolver<DM3> {
public string solverDefinition {get;} = "This is a generic local search solver for 3-Dimensional Matching, which, while possible, removes one constraint from the current solution, and swaps in two more constraints.";
public string source {get;} = "";
public string[] contributors {get;} = { "Caleb Eardley"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public ThreeDimensionalMatchingBruteForce() {
Expand Down
1 change: 1 addition & 0 deletions Problems/NPComplete/NPC_EXACTCOVER/Solvers/DancingLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DancingLinks : ISolver<EXACTCOVER> {
public string solverDefinition {get;} = "";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public DancingLinks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ExactCoverBruteForce : ISolver<EXACTCOVER> {
public string solverDefinition {get;} = "This is a generic brute force solver for Exact Cover";
public string source {get;} = "";
public string[] contributors {get;} = { "Caleb Eardley"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public ExactCoverBruteForce() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class ExactCoverRecursive : ISolver<EXACTCOVER> {
public string solverDefinition {get;} = "This is a optimized recursive solver for Exact Cover";
public string source {get;} = "";
public string[] contributors {get;} = { "Russell Phillips"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public ExactCoverRecursive() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GraphColoringBruteForce : ISolver<GRAPHCOLORING> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Graph Coloring problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public GraphColoringBruteForce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class HamiltonianBruteForce : ISolver<HAMILTONIAN> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Hamiltonian Path problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public HamiltonianBruteForce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class HittingSetBruteForce : ISolver<HITTINGSET> {
public string solverDefinition {get;} = "This is a brute force solver for Hitting Set";
public string source {get;} = "";
public string[] contributors {get;} = {"Russell Phillips"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public HittingSetBruteForce() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class IndependentSetBruteForce : ISolver<INDEPENDENTSET> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Independent Set problem";
public string source {get;} = "";
public string[] contributors {get;} = {"Russell Phillips"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public IndependentSetBruteForce() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class IntegerProgrammingBruteForce : ISolver<INTPROGRAMMING01> {
public string solverDefinition {get;} = "This is a generic brute force solver for 0-1 Integer Programming";
public string source {get;} = "";
public string[] contributors {get;} = { "Caleb Eardley"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public IntegerProgrammingBruteForce() {
Expand Down
1 change: 1 addition & 0 deletions Problems/NPComplete/NPC_JOBSEQ/Solvers/JobSeqBruteForce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class JobSeqBruteForce : ISolver<JOBSEQ> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Job Sequencing problem";
public string source {get;} = "";
public string[] contributors {get;} = {"Russell Phillips"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public JobSeqBruteForce() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class KnapsackBruteForce : ISolver<KNAPSACK> {
public string solverDefinition {get;} = "This a brute force solver for the 0-1 Knapsack problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Russell Phillips"};
public bool timerHasExpired { get; set; }


public string complexity {get;} = "O(2^n)";
Expand Down
3 changes: 2 additions & 1 deletion Problems/NPComplete/NPC_NODESET/Solvers/NodeSetBruteForce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class NodeSetBruteForce : ISolver<NODESET> {
public string solverDefinition {get;} = "This is a brute force solver for the Node Set problem";
public string source {get;} = "";
public string[] contributors {get;} = {"Andrija Sevaljevic"};
public bool timerHasExpired { get; set; }

public NodeSetBruteForce() {
public NodeSetBruteForce() {

}
private long factorial(long x){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class PartitionBruteForce : ISolver<PARTITION> {
public string solverDefinition {get;} = "This is a brute force solver for the Partition problem";
public string source {get;} = "";
public string[] contributors {get;} = {"Andrija Sevaljevic"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public PartitionBruteForce() {
Expand Down
6 changes: 5 additions & 1 deletion Problems/NPComplete/NPC_SAT/Solvers/SATBruteForceSolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class SATBruteForceSolver : ISolver {
public string solverDefinition {get;} = "This is a simple brute force solver for SAT";
public string source {get;} = "";
public string[] contributors {get;} = { "Daniel Igbokwe, Show Pratoomratana"};
public bool timerHasExpired { get; set; }

#endregion

Expand Down Expand Up @@ -117,7 +118,10 @@ public string solve(string SATInstance){
// Loop through all combinations. The total number of binary choices you can make is 2^(number of items). E.G. 3 variables is 2^3.
for (int currentCombination = 0; currentCombination < Math.Pow(2, literals.Count); currentCombination++){
int trueClauses = 0;
foreach (List<string> currentClause in clause){
if (timerHasExpired)
return "timeout";

foreach (List<string> currentClause in clause){
// change the T/F values of the literals. Starts with at least 1 being true by incrementing at the start.
literalDict = increment(literalDict);
bool currentEvaluation = evaluate(literalDict, currentClause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Sat3BacktrackingSolver : ISolver<SAT3> {
public string solverDefinition {get;} = "This is a O(n!) solution algorithm for the 3SAT problem which implements a back tracking algorithm to find and exact assignment boolean assignment of variable to satisfy the broblem instance.";
public string source {get;} = "";
public string[] contributors {get;} = {"David Lindeman","Kaden Marchetti"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public Sat3BacktrackingSolver() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class HeuristicSolver : ISolver<SETCOVER> {
public string solverDefinition {get;} = "";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public HeuristicSolver()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SetCoverBruteForce : ISolver<SETCOVER> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Set Cover problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public SetCoverBruteForce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class SteinerTreeBruteForce : ISolver<STEINERTREE> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Steiner Tree problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public SteinerTreeBruteForce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SubsetSumBruteForce : ISolver<SUBSETSUM> {
public string solverDefinition {get;} = "This is a brute force solver for Subset Sum";
public string source {get;} = "";
public string[] contributors {get;} = { "Caleb Eardley","Garret Stouffer"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public SubsetSumBruteForce() {
Expand Down
1 change: 1 addition & 0 deletions Problems/NPComplete/NPC_TSP/Solvers/TSPBruteForce.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TSPBruteForce : ISolver<TSP> {
public string solverDefinition {get;} = "This is a brute force solver for the NP-Complete Traveling Sales Person problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic" };
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public TSPBruteForce()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class VertexCoverBruteForce : ISolver<VERTEXCOVER> {
public string solverDefinition {get;} = "This solver simply tests combinations of nodes of size k until a solution is found, or all combinations are tested.";
public string source {get;} = "";
public string[] contributors {get;} = { "Caleb Eardley"};
public bool timerHasExpired { get; set; }

// --- Methods Including Constructors ---
public VertexCoverBruteForce() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class WeightedCutBruteForce : ISolver<WEIGHTEDCUT> {
public string solverDefinition {get;} = "This is a brute force solver for the Weighted Cut problem";
public string source {get;} = "";
public string[] contributors {get;} = { "Andrija Sevaljevic" };
public bool timerHasExpired { get; set; }

public WeightedCutBruteForce()
{
Expand Down Expand Up @@ -126,4 +127,3 @@ public string solve(WEIGHTEDCUT cut)
return "{}";
}
}