示例#1
0
        static public IntVarMatrix operator+(IntVarMatrix lhs, IntVarMatrix rhs)
        {
            if ((lhs.RowCount != rhs.RowCount) ||
                (lhs.ColCount != rhs.ColCount))
            {
                throw new Exception("");
            }

            Solver solver   = lhs.Solver;
            int    rowCount = lhs.RowCount;
            int    colCount = lhs.ColCount;

            IntVarList list = new IntVarList(solver);

            for (int row = 0; row < rowCount; ++row)
            {
                for (int col = 0; col < colCount; ++col)
                {
                    IntVarExprVar expr = lhs[row, col] + rhs[row, col];
                    solver.Add(expr);

                    list.Add(expr.Var0);
                }
            }

            return(new IntVarMatrix(solver, rowCount, colCount, list));
        }
示例#2
0
        static public IntVarMatrix operator*(IntVarMatrix lhs, int[] rhs)
        {
            if (lhs.ColCount != rhs.Length)
            {
                throw new Exception("");
            }

            Solver     solver = lhs.Solver;
            IntVarList list   = new IntVarList(solver);

            for (int row = 0; row < lhs.RowCount; ++row)
            {
                IntVar[] rowList = new IntVar[lhs.ColCount];

                for (int col = 0; col < lhs.ColCount; ++col)
                {
                    rowList[col] = lhs.Cell(row, col);
                }

                IntVarListDotProduct dp = new IntVarListDotProduct(solver, rowList, rhs);
                solver.Add(dp);

                list.Add(dp.Var0);
            }

            return(new IntVarMatrix(solver, lhs.ColCount, 1, list));
        }
示例#3
0
        public IntVarList Col(int vcol, int rowStart, int rowStop)
        {
            IntVarList list = new IntVarList(m_Solver);

            for (int row = rowStart; row < rowStop; ++row)
            {
                list.Add(Cell(row, vcol));
            }

            return(list);
        }
示例#4
0
        public IntVarList Row(int vrow, int colStart, int colStop)
        {
            IntVarList list = new IntVarList(m_Solver);

            for (int col = colStart; col < colStop; ++col)
            {
                list.Add(Cell(vrow, col));
            }

            return(list);
        }
示例#5
0
        public IntVarMatrix Matrix(int rowOffset, int colOffset, int rowCount, int colCount)
        {
            IntVarList list = new IntVarList(m_Solver);

            for (int row = 0; row < rowCount; ++row)
            {
                for (int col = 0; col < colCount; ++col)
                {
                    list.Add(Cell(rowOffset + row, colOffset + col));
                }
            }

            return(new IntVarMatrix(m_Solver, rowCount, colCount, list));
        }
示例#6
0
文件: Golomb.cs 项目: nofear/Mara
        public Golomb( int n )
            : base()
        {
            int maxLength	= (int) Math.Pow( 2, (n-1) ) - 1;

            m_Solver.Horizon	= new IntInterval( 0, maxLength );

            m_MarkList		= new IntVarList( m_Solver );
            for( int idx = 0; idx < n; ++idx )
            {
                m_MarkList.Add( new IntVar( m_Solver, idx, maxLength, "m" + ( idx + 1 ).ToString() ) );
            }

            int mark	= 0;
            int pos		= 0;

            m_DiffList	= new IntVarList( m_Solver, n * ( n -1 ) );
            for( int i = 0; i < n - 1; ++i )
            {
                for( int j = i + 1; j < n; ++j )
                {
                    if( i == n/2 && j == n-1 )
                        mark = pos;

                    IntVarExpr exp	= m_MarkList[ j ] - m_MarkList[ i ];
                    exp.Var0.Min	= 1;
                    exp.Var0.Name	= "#" + m_MarkList[ j ].Name + "-" + m_MarkList[ i ].Name;
                    m_Solver.Add( exp );

                    m_DiffList.Add( exp.Var0 );

                    ++pos;
                }
            }

            IntVarListAllDifferent ad	= m_DiffList.AllDifferent();
            //ad.Level	= Constraint.PropagateLevel.High;
            m_Solver.Add( ad );

            // start mark at 0
            m_MarkList[ 0 ].Value	= 0;

            // lower half should be less than the half difference
            IntVarCmp cmp		=  m_MarkList[ (n-1)/2 ] < m_DiffList[ mark ];
            m_Solver.Add( cmp );

            Console.WriteLine( cmp.ToString() +", " + m_DiffList[ mark ].ToString() );
        }
示例#7
0
        private void InitMatrix(IntDomain domain)
        {
            m_VarList = new IntVarList(m_Solver, m_RowCount * m_ColCount);

            for (int row = 0; row < m_RowCount; ++row)
            {
                for (int col = 0; col < m_ColCount; ++col)
                {
                    string name = row.ToString() + "." + col.ToString();

                    IntVar cell = new IntVar(m_Solver, domain, name);

                    m_VarList.Add(cell);
                }
            }
        }
示例#8
0
        public IntVarList DiagRightTopToBottomLeft()
        {
            IntVarList list = new IntVarList(m_Solver);

            if (m_RowCount == m_ColCount)
            {
                int size = m_RowCount;

                for (int idx = 0; idx < size; ++idx)
                {
                    list.Add(Cell(idx, (size - 1) - idx));
                }
            }

            return(list);
        }
示例#9
0
        private void InitMatrix( IntDomain domain )
        {
            m_VarList	= new IntVarList( m_Solver, m_RowCount * m_ColCount );

            for( int row = 0; row < m_RowCount; ++row )
            {
                for( int col = 0; col < m_ColCount; ++col )
                {
                    string name		= row.ToString() + "." + col.ToString();

                    IntVar cell		= new IntVar( m_Solver, domain, name );

                    m_VarList.Add( cell );
                }
            }
        }
示例#10
0
        public IntVarList Row( int vrow, int colStart, int colStop )
        {
            IntVarList list	= new IntVarList( m_Solver );

            for( int col = colStart; col < colStop; ++col )
            {
                list.Add( Cell( vrow, col ) );
            }

            return list;
        }
示例#11
0
        public IntVarMatrix Matrix( int rowOffset, int colOffset, int rowCount, int colCount )
        {
            IntVarList list	= new IntVarList( m_Solver );

            for( int row = 0; row < rowCount; ++row )
            {
                for( int col = 0; col < colCount; ++col )
                {
                    list.Add( Cell( rowOffset + row, colOffset + col ) );
                }
            }

            return new IntVarMatrix( m_Solver, rowCount, colCount, list );
        }
示例#12
0
        public IntVarList DiagRightTopToBottomLeft()
        {
            IntVarList list	= new IntVarList( m_Solver );

            if( m_RowCount == m_ColCount )
            {
                int size	= m_RowCount;

                for( int idx = 0; idx < size; ++idx )
                {
                    list.Add( Cell( idx, ( size - 1 ) - idx ) );
                }
            }

            return list;
        }
示例#13
0
        public IntVarList Col( int vcol, int rowStart, int rowStop )
        {
            IntVarList list	= new IntVarList( m_Solver );

            for( int row = rowStart; row < rowStop; ++row )
            {
                list.Add( Cell( row, vcol ) );
            }

            return list;
        }
示例#14
0
        public static IntVarMatrix operator +( IntVarMatrix lhs, IntVarMatrix rhs )
        {
            if( ( lhs.RowCount != rhs.RowCount )
                    || ( lhs.ColCount != rhs.ColCount ) )
                throw new Exception( "" );

            Solver solver	= lhs.Solver;
            int rowCount	= lhs.RowCount;
            int colCount	= lhs.ColCount;

            IntVarList list	= new IntVarList( solver );

            for( int row = 0; row < rowCount; ++row )
            {
                for( int col = 0; col < colCount; ++col )
                {
                    IntVarExprVar expr	=  lhs[ row, col ] + rhs[ row, col ];
                    solver.Add( expr );

                    list.Add( expr.Var0 );
                }
            }

            return new IntVarMatrix( solver, rowCount, colCount, list );
        }
示例#15
0
        public static IntVarMatrix operator *( IntVarMatrix lhs, int[] rhs )
        {
            if( lhs.ColCount != rhs.Length )
                throw new Exception( "" );

            Solver solver	= lhs.Solver;
            IntVarList list	= new IntVarList( solver );

            for( int row = 0; row < lhs.RowCount; ++row )
            {
                IntVar[] rowList	= new IntVar[ lhs.ColCount ];

                for( int col = 0; col < lhs.ColCount; ++col )
                {
                    rowList[ col ]	= lhs.Cell( row, col );
                }

                IntVarListDotProduct dp		= new IntVarListDotProduct( solver, rowList, rhs );
                solver.Add( dp );

                list.Add( dp.Var0 );
            }

            return new IntVarMatrix( solver, lhs.ColCount, 1, list );
        }