Ejemplo n.º 1
0
void CSP::clearData()
{
    /*Initially assign the domain, assignment for each variable and initialize the current state*/
    for(int y = 0; y < 9; y++)
    {
        for(int x = 0; x < 9; x++)
        {
            variables[y][x].assignement = 0; //Initialize the assignment
            
            /*Initialize the domain*/
            variables[y][x].domain.clear();
            for(int i = 1; i <= 9; i++)
            {
                variables[y][x].domain.push_back(i);
            }
            
            cur_state.values[y][x] = 0; //Initizlize the current state
            
        }
    }
    
    /*Check whether a random domain is use*/
    if(random == 1)
        reshuffleDomain();
    
    repeating_list.clear();
    while(!assigned_variables.empty())
    {
        assigned_variables.pop();
        repeating_list.clear();
    }
    
}
Ejemplo n.º 2
0
/*Update Domain for the forward checking*/
void CSP::updateDomain(const State state)
{
    //for all the cells
    for (int i=0; i< 81;i++)
    {
        int row = i / 9;
        int col = i % 9;
        if (state.values[row][col]!=0) {    //if the value is assigned
            int temp=state.values[row][col];
            for(int j = 0; j < 9; j++)
            {
                if(state.values[row][j] == 0&&j!=col)   //check the cells on the same row
                {
                    auto iter=std::find(variables[row][j].domain.begin(),
                                        variables[row][j].domain.end(),
                                        temp);
                    if (iter!=variables[row][j].domain.end())
                    {
                        variables[row][j].domain.erase(iter);
                    }
                }
                if (state.values[j][col]==0&&j!=row)
                {
                    auto iter=std::find(variables[j][col].domain.begin(),
                                        variables[j][col].domain.end(),
                                        temp);
                    if (iter!=variables[j][col].domain.end())
                    {
                            variables[j][col].domain.erase(iter);
                    }
                }
            }
                int tempRow = row / 3 * 3;
                int tempCol = col / 3 * 3;
                for(int j = tempRow; j < tempRow + 3;j++){
                    for(int k = tempCol; k < tempCol + 3; k++){
                        if(state.values[j][k]==0)
                        {
                            auto iter=std::find(variables[j][k].domain.begin(),
                                                variables[j][k].domain.end(),
                                                temp);
                            if (iter!=variables[j][k].domain.end()) {
                                variables[j][k].domain.erase(iter);
                        }
                    }
                }
            }
        }
        if (state.values[row][col]==0) {
            int temp;
            variables[row][col].domain.clear();
                for (int i=1; i<10; i++) {
                    
                    variables[row][col].domain.push_back(i);
                }
            if(random == 1)
                reshuffleDomain();
                for(int j = 0; j < 9; j++)
                {
                    if(state.values[row][j] != 0 && j!=col)   //check the cells on the same row
                    {
                        temp=state.values[row][j];
                        auto iter=std::find(variables[row][col].domain.begin(),
                                            variables[row][col].domain.end(),
                                            temp);
                        if (iter!=variables[row][col].domain.end())
                        {
                            variables[row][col].domain.erase(iter);
                        }
                    }
                    if (state.values[j][col]!=0&&j!=row)
                    {
                        temp=state.values[j][col];
                        auto iter=std::find(variables[row][col].domain.begin(),
                                            variables[row][col].domain.end(),
                                            temp);
                        if (iter!=variables[row][col].domain.end())
                        {
                            variables[row][col].domain.erase(iter);
                        }
                    }
                }
                int tempRow = row / 3 * 3;
                int tempCol = col / 3 * 3;
                for(int j = tempRow; j < tempRow + 3;j++){
                    for(int k = tempCol; k < tempCol + 3; k++){
                        if(state.values[j][k]!=0)
                        {
                            temp=state.values[j][k];
                            auto iter=std::find(variables[row][col].domain.begin(),
                                                variables[row][col].domain.end(),
                                                temp);
                            if (iter!=variables[row][col].domain.end()) {
                                variables[row][col].domain.erase(iter);
                            }
                        }
                    }
                }
        }
    }
}