简体   繁体   中英

Generating an array of integers without violating constraints

I am struggling with a problem for hours. It is a constraint satisfaction problem. Let me describe it on a simple example:

Assume there is an array of integers with length 8. Every cell can take certain values. First 4 cells can take 0, 1 or 2 and the other half can take 0 or 1. These 3 arrays can be some examples.

{2,1,0,2,1,1,0,1}
{2,2,1,0,0,1,0,0}
{0,0,0,2,0,0,0,1}

However there are some constraints to construct the arrays as follows:

constraint1 = {1,-,-,-,-,1,-,-}  // !(cell2=1 && cell6=1) cell2 and cell6 can not be in these format. 
constraint2 = {0,-,-,-,-,-,-,0}  // !(cell1=0 && cell8=0)
constraint3 = {-,-,-,2,1,1,-,-}  // !(cell4=2 && cell5=1 && cell6=1)
constraint4 = {1,1,-,-,-,-,-,-}  // !(cell1=1 && cell2=1)

For better understanding;

{0,1,1,2,0,1,0,0}  // this is not valid, because it violates the constraint2
{1,1,2,2,1,1,0,1}  // this is not valid, because it violates the constraint3 and constraint4
{1,1,0,0,0,1,0,0}  // this is not valid, because it violates the constraint4

I need to generate an array of integers which does not violates any of the given constraints.

In my approach;

1) Create an array (called myArray) and initialize every cell to -1
2) Count the number of cells which are used in constraints. Above example, cell1 is used 3 times, cell2 is used 1 time, cell3 is not used, so on so forth.
3) Choose the cell which is used more in constraints (it is cell1, used 3 times)
4) Find the distribution of numbers in this cell. (In cell1, 1 is used 2 times and 0 is used 1 time)
5) Change this chosen cell in myArray to the number which is used less. (In cell1, since 0 is used less than 1, cell1 in myArray will be 0) 
6) Delete all the constraints from the list which has 1 or 2 in their cell1.
7) Go to step 2 and do same steps until all constraints are eliminated

The idea of this algorithm is to chose the cell and its value in such a way that it will eliminate more constraints.

However, this algorithm is not working, when the number of constraints are higher.

Important Note: This is just a simple example. In normal case, length of the array is longer (averagely 100) and number of constraints is higher (more than 200). My input is length of the array, N constraints and the values each cell can take.

Is there anyone who has better idea to solve this problem?

Here is a code that I have written in C# to generate a random matrix and then to remove the constraint in the matrix.

class Program
{
    static void Main(string[] args)
    {

        int[] inputData = new int[4] { 3, 7, 3, 3 };
        int matrixRowSize = 6;

        /////////////////////////// Constraints 

        int []constraint1 = new int[4] { 1, -1, -1, 2}; // here is the constaint that i want to remove.
        // note the constaints could be more than 1, so there could be a generic method

        Random r = new Random();
        int[,] Random_matrix = new int[matrixRowSize, inputData.Length];
        ///////////// generate random matrix 
        for (int i = 0; i < inputData.Length; i++)
        {
            for (int j = 0; j < matrixRowSize; j++)
            {       
                int k = r.Next(0, inputData[i]);  


                Random_matrix[j, i] = k;
            }
        }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM