lifebeyondfife / Decider

An Open Source .Net Constraint Programming Solver
MIT License
150 stars 21 forks source link

ArgumentOutOfRangeException when calling `SearchAllSolutions` #55

Closed toburger closed 3 years ago

toburger commented 3 years ago

When I run the sample code from the readme in a C# console application I get the following error message when trying to run the find all solutions example code:

var searchResult = state.SearchAllSolutions();

foreach (var solution in state.Solutions)
{
    for (var i = 0; i < variables.Length; ++i)
    {
        for (var j = 0; j < variables.Length; ++j)
            Console.Write(solution[i.ToString()].InstantiatedValue == j ? "Q " : ". ");

        Console.WriteLine();
    }
    Console.WriteLine();
}

This throws the following exception:

Unhandled exception. System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.SZArrayHelper.set_Item[T](Int32 index, T value)
   at Decider.Csp.Integer.StateInteger.Search(StateOperationResult& searchResult, LinkedList`1 unassignedVariables, IList`1 instantiatedVariables, Stopwatch& stopwatch, Int32 timeOut)   
   at Decider.Csp.Integer.StateInteger.SearchAllSolutions()
   at decider.Program.Main(String[] args) in C:\Users\Tobias\Desktop\decider-test\Program.cs:line 47

Full code:

using System;
using System.Collections.Generic;
using Decider.Csp.Integer;
using Decider.Csp.Global;
using Decider.Csp.BaseTypes;

namespace DeciderTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = new VariableInteger("s", 0, 9);
            var e = new VariableInteger("e", 0, 9);
            var n = new VariableInteger("n", 0, 9);
            var d = new VariableInteger("d", 0, 9);
            var m = new VariableInteger("m", 1, 9);
            var o = new VariableInteger("o", 0, 9);
            var r = new VariableInteger("r", 0, 9);
            var y = new VariableInteger("y", 0, 9);
            var c0 = new VariableInteger("c0", 0, 1);
            var c1 = new VariableInteger("c1", 0, 1);
            var c2 = new VariableInteger("c2", 0, 1);
            var c3 = new VariableInteger("c3", 0, 1);

            var constraints = new List<IConstraint>
            {
                new AllDifferentInteger(new [] { s, e, n, d, m, o, r, y }),
                new ConstraintInteger(d + e == (10 * c0) + y),
                new ConstraintInteger(n + r + c0 == (10 * c1) + e),
                new ConstraintInteger(e + o + c1 == (10 * c2) + n),
                new ConstraintInteger(s + m + c2 == (10 * c3) + o),
                new ConstraintInteger(c3 == m)
            };

            var variables = new [] { c0, c1, c2, c3, s, e, n, d, m, o, r, y };
            var state = new StateInteger(variables, constraints);

            var searchResult = state.SearchAllSolutions();

            foreach (var solution in state.Solutions)
            {
                for (var i = 0; i < variables.Length; ++i)
                {
                    for (var j = 0; j < variables.Length; ++j)
                        Console.Write(solution[i.ToString()].InstantiatedValue == j ? "Q " : ". ");

                    Console.WriteLine();
                }
                Console.WriteLine();
            }
        }
    }
}

Am I missing something?

lifebeyondfife commented 3 years ago

Thanks for trying out Decider, @toburger. I've recreated this bug and will debug and resolve as soon as I can.

lifebeyondfife commented 3 years ago

Fixed by https://github.com/lifebeyondfife/Decider/commit/88bf19a195a361aba936a165a24eee2273cfef1d

Thanks again, Tobias.