Ejemplo n.º 1
0
        public MagicSquare( int count )
            : base(0, 10000)
        {
            m_Matrix			= new IntVarMatrix( m_Solver, count, count, new IntInterval( 1, count * count ) );
            m_MagicConstant		= ( count * ( count * count + 1 ) ) / 2;

            for( int idx = 0; idx < count; ++idx )
            {
                m_Solver.Add( m_Matrix.Row( idx ).Sum( m_MagicConstant ) );
                m_Solver.Add( m_Matrix.Col( idx ).Sum( m_MagicConstant ) );
            }

            bool mm		= false;
            if( mm )
            {
                if( count % 2 == 0 )
                {
                    int magicConstant2	= m_MagicConstant / 2;

                    for( int idx = 0; idx < count; ++idx )
                    {
                        m_Solver.Add( m_Matrix.Row( idx, 0, count / 2 ).Sum( magicConstant2 ) );
                        m_Solver.Add( m_Matrix.Row( idx, count / 2, count ).Sum( magicConstant2 ) );
                        m_Solver.Add( m_Matrix.Col( idx, 0, count / 2 ).Sum( magicConstant2 ) );
                        m_Solver.Add( m_Matrix.Col( idx, count / 2, count ).Sum( magicConstant2 ) );
                    }
                }
            }

            m_Solver.Add( m_Matrix.DiagLeftTopToBottomRight().Sum( m_MagicConstant ) );
            m_Solver.Add( m_Matrix.DiagRightTopToBottomLeft().Sum( m_MagicConstant ) );

            IntVarListAllDifferent ad	= m_Matrix.VarList.AllDifferent();
            m_Solver.Add( ad );

            // remove symmetry
            m_Solver.Add( m_Matrix[ 0, 0 ] < m_Matrix[ 0, count - 1 ] );
            m_Solver.Add( m_Matrix[ 0, 0 ] < m_Matrix[ count - 1, count - 1 ] );
            m_Solver.Add( m_Matrix[ 0, 0 ] < m_Matrix[ count - 1, 0 ] );
            m_Solver.Add( m_Matrix[ 0, count - 1 ] < m_Matrix[ count - 1, 0 ] );
        }
Ejemplo n.º 2
0
        public Sudoku()
            : base(1, 9)
        {
            int size	= 9;

            m_Matrix	= new IntVarMatrix( m_Solver, size, size, new IntInterval( 1, size ) );

            for( int idx = 0; idx < size; ++idx )
            {
                IntVarListAllDifferent constraint	= m_Matrix.Row( idx ).AllDifferent();
                constraint.Level	= PropagateLevel.High;

                m_Solver.Add( constraint );
            }

            for( int idx = 0; idx < size; ++idx )
            {
                IntVarListAllDifferent constraint	= m_Matrix.Col( idx ).AllDifferent();
                constraint.Level	= PropagateLevel.High;

                m_Solver.Add( constraint );
            }

            for( int subRow = 0; subRow < 3; ++subRow )
            {
                for( int subCol = 0; subCol < 3; ++subCol )
                {
                    IntVarListAllDifferent constraint	= m_Matrix.Matrix( subRow * 3, subCol * 3, 3, 3 ).VarList.AllDifferent();
                    constraint.Level	= PropagateLevel.High;

                    m_Solver.Add( constraint );
                }
            }
        }
Ejemplo n.º 3
0
        static void Mul1()
        {
            Solver solver	= new Solver( 0, 10000 );
            IntVarMatrix m1	= new IntVarMatrix( solver, 2, 2, new IntInterval( 0, 100 ) );
            int[] v			= new int[] { 1, 2 };
            IntVarMatrix m2	= m1 * v;
            solver.Propagate();
            solver.PrintVariables( Console.Out );

            Solver s1	= new Solver( -1000000, 1000000 );
            IntVar a	= new IntVar( s1, IntDomain.Random( -100, 100, 1 ), "a" );
            IntVar b	= new IntVar( s1, IntDomain.Random( -100, 100, 1 ), "b" );
            IntVar c	= new IntVar( s1, IntDomain.Random( -100, 100, 1 ), "c" );
            IntVarList l	= new IntVarList( s1, new IntVar[] { a, b, c } );
            s1.Add( l.Mul() );
            s1.Propagate();
            s1.PrintVariables( Console.Out );
        }