/// <summary> /// Runs the main program /// </summary> /// <param name="argv">input arguments</param> /// <returns>return code - 0 for success; otherwise fail</returns> static int Main(string[] argv) { // Define the input file parameter string inputFile; // Determine if we take the input from the arguments or as the default file based on the current directory if (argv.Length < 1) { inputFile = Path.Combine(Directory.GetCurrentDirectory(), "input.txt"); } else { inputFile = argv[0]; } // Ensure that the file exists if (!File.Exists(inputFile)) { Console.WriteLine(string.Format("Cannot find input file {0:}", inputFile)); return(-1); } // Define the number of threads to use and the respective variable parameters int numThreads = 1; Thread[] threads = new Thread[numThreads]; SolverManager[] results = new SolverManager[numThreads]; // Define the battle grid and print BattleGrid battleGrid = BattleGrid.FromFile(inputFile); Console.WriteLine("Input Grid:"); Console.WriteLine(battleGrid.ToString()); //GridInstance inst = new GridInstance(battleGrid); //inst.Solve(); //return -1; // Start the solve parameters Console.WriteLine(); Console.WriteLine(string.Format("Solving on {0:} threads...", numThreads)); // Save the start time to be able to check the total elapsed time DateTime startTime = DateTime.UtcNow; // Initialize and start each of the threads for (int i = 0; i < numThreads; ++i) { ThreadSolverState state = new ThreadSolverState() { BattleGrid = battleGrid, ThreadIndex = i, ThreadCount = numThreads, abort = false }; results[i] = new SolverManager(state); threads[i] = new Thread(new ThreadStart(results[i].Run)); threads[i].Start(); } // Define the resulting solved grid and continue conditions GridInstance solvedGrid = null; bool anyRunning = true; // Loop while the problem hasn't been solved while (anyRunning && solvedGrid == null) { // Check if any thread is still running anyRunning = false; for (int i = 0; i < numThreads; ++i) { if (threads[i].IsAlive) { anyRunning = true; break; } } // Check if any thread has found the solution for (int i = 0; i < numThreads; ++i) { results[i].mutex.WaitOne(); if (results[i].SolvedGridValid) { solvedGrid = results[i].SolvedGrid; } results[i].mutex.ReleaseMutex(); } // Sleep to wait until the next loop check Thread.Sleep(100); } // Set each of the remaining threads to abort for (int i = 0; i < numThreads; ++i) { if (threads[i].IsAlive) { results[i].InputState.abort = true; } } // Join the remaining threads with the main thread for (int i = 0; i < numThreads; ++i) { threads[i].Join(); } // Print an empty line Console.WriteLine(); // Print the solution if found; otherwise print to solution foudn if (solvedGrid != null) { Console.WriteLine("Solution:"); Console.WriteLine(solvedGrid.ToString()); TimeSpan dT = DateTime.UtcNow - startTime; Console.Write("Solved in "); if (dT.TotalSeconds <= 90) { Console.WriteLine(string.Format("{0:} seconds", dT.TotalSeconds)); } else { Console.WriteLine(string.Format("{0:} minutes", dT.TotalMinutes)); } return(0); } else { Console.WriteLine("No solution found"); return(1); } }
/// <summary> /// Defines a new SolverManager instance with a given state /// </summary> /// <param name="solverState">thread state to define the manager with</param> public SolverManager(ThreadSolverState solverState) { SolvedGrid = null; InputState = solverState; }