Ejemplo n.º 1
0
        /// <summary>
        /// Adds new partial solution to the system.
        /// </summary>
        /// <param name="problemId">ID of the corresponding problem instance.</param>
        /// <param name="partialProblemId">ID of the corresponding partial problem.</param>
        /// <param name="data">Partial solution data.</param>
        /// <param name="computationsTime">Time of the foregoing computations.</param>
        /// <param name="timeoutOccured">Whether timeout stopped the computations.</param>
        public void AddPartialSolution(ulong problemId, ulong partialProblemId, byte[] data, ulong computationsTime,
                                       bool timeoutOccured)
        {
            var pairId = new Tuple <ulong, ulong>(problemId, partialProblemId);

            // Make sure the corresponding problem instance exists.
            if (!_problems.ContainsKey(problemId))
            {
                Logger.Error("Corresponding problem instance doesn't exist.");
                return;
            }

            var problem = _problems[problemId];

            // Make sure the corresponding partial problem instance exists.
            if (!_partialProblems.ContainsKey(pairId))
            {
                Logger.Error("Corresponding partial problem doesn't exist.");
                return;
            }

            var partialProblem = _partialProblems[pairId];

            // Make sure that state of the corresponding partial problem is set to "being computed".
            if (partialProblem.State != PartialProblem.PartialProblemState.BeingComputed)
            {
                Logger.Error("Corresponding partial problem's state is invalid.");
                return;
            }

            // Create partial solution instance.
            var partialSolution = new PartialSolution(partialProblem, data, computationsTime, timeoutOccured)
            {
                State         = PartialSolution.PartialSolutionState.BeingGathered,
                MergingNodeId = null
            };

            // Add partial solution to the set.
            _partialSolutions.Add(pairId, partialSolution);

            // Delete corresponding partial problem.
            _partialProblems.Remove(pairId);

            Logger.Info("Added new partial solution (id: " + problemId + "/" + partialProblemId + ", type: " +
                        problem.Type + ").");

            // Check if all partial solutions are in and set appropriate state.
            var gatheredPartialSolutions = GetPartialSolutions(problemId,
                                                               PartialSolution.PartialSolutionState.BeingGathered);

            if ((int)problem.NumberOfParts == gatheredPartialSolutions.Count)
            {
                foreach (var ps in gatheredPartialSolutions)
                {
                    ps.State = PartialSolution.PartialSolutionState.AwaitingMerge;
                }

                Logger.Info("Partial solutions are ready to merge (id: " + problemId + ").");
            }
        }
Ejemplo n.º 2
0
 public byte[] SerializePartialSolution(PartialSolution partialSolution)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         formatter.Serialize(stream, partialSolution);
         return(stream.ToArray());
     }
 }
Ejemplo n.º 3
0
        private static SolutionSet CreateFirstSolutionSet(Point[] points)
        {
            var partialSolutions = new PartialSolution[SolutionSet.BeamWidth];

            for (var i = 0; i < SolutionSet.BeamWidth; i++)
            {
                partialSolutions[i] = new PartialSolution(points[i]);
            }

            return(new SolutionSet(partialSolutions));
        }
Ejemplo n.º 4
0
        public static void Solve(PartialSolution sol)
        {
            int exam = sol.Examine();

            if (exam == PartialSolution.ACCEPT)
            {
                Console.WriteLine(sol);
            }
            else if (exam != PartialSolution.ABANDON)
            {
                foreach (PartialSolution p in sol.Extend())
                {
                    Solve(p);
                }
            }
        }
    /// <summary>
    /// Yields all extensions of this partial solution.
    /// </summary>
    /// <returns>An array of partial solutions that extend this solution.</returns>
    public PartialSolution[] Extend()
    {
        // Generate a new solution for each column.
        PartialSolution[] result = new PartialSolution[NQUEENS];
        for (int i = 0; i < result.Length; i++)
        {
            int size = queens.Length;

            // The new solution has one more queen (on the next higher row) than this one.
            result[i] = new PartialSolution(size + 1);

            // Copy this solution into the new one.
            for (int j = 0; j < size; j++)
            {
                result[i].queens[j] = queens[j];
            }

            // Append the new queen into the ith column.
            result[i].queens[size] = new Queen(size, i);
        }
        return(result);
    }