Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

int CSP::updateDomain(const State state) { std::vector iterator updated_state; for (int i = 0; i < 81; i++) { int col = i % 9; int

int CSP::updateDomain(const State state) { std::vector iterator updated_state; for (int i = 0; i < 81; i++) { int col = i % 9; int row = i / 9;

if (state.values[row][col] == 0) { variables[row][col].domain.clear();

for (int num = 1; num <= 9; num++) { variables[row][col].domain.push_back(num); } updated_state = variables[row][col].domain.begin(); while (updated_state != variables[row][col].domain.end()) { if (checkForRow(row, col, state, *updated_state) || checkForColumn(row, col, state, *updated_state) || checkForGrid(row, col, state, *updated_state)) { variables[row][col].domain.erase(updated_state); updated_state = variables[row][col].domain.begin(); } else { updated_state++; } } } } }

/*Arc consistency use*/ void CSP::arcConsistency(const State state) {

} CSP.h

#ifndef Sudoku_CSP_h #define Sudoku_CSP_h

#include #include #include #include #include // std::time #include #endif

using namespace std;

/*This structure defines the variable format for the Sudoku problem*/ struct Variable{ vector domain; //the possible values of this variable, only when the varible has not been assigned yet int assignment; //0 means this variable has not been assigned yet };

/*State structure: one state of assignment for the 9 by 9 variables*/ struct State{ int values[9][9]; //reload the equal check operator bool operator==(const State& s) const { for(int i = 0; i < 81; i++) { int y = i / 9; int x = i % 9; if(values[y][x] != s.values[y][x]) { return false; } } return true; } };

/*Struct of arrow, which is a pair of node with directions*/ struct Arrow{ int head; int tail; Arrow(); Arrow(int h, int t) { head = h; tail = t; } bool operator==(const Arrow& a) const { if(head == a.head && tail == a.tail) return true; else return false; } };

/*This is the class that solve the constraint problem by using the algroithms learned in the class, such as backtrack, forward checking,...*/ class CSP{ public: State cur_state; //it has 9*9 values inside the variable. Each value represents the assignment for the corresponding cell. If it is 0, it means no assignment for the cell yet. Variable variables[9][9]; //Each varible represents one cell of the sudocu table. Each variables[i][j] stack assigned_variables; //store the order of varaibles that have been assigned, so every time when back track happens, just need to //update the lastest variable value and update the corresponding domain sets. vector repeating_list; //store the repeated pattern int alg_opt; //algorithm_option in VisualDisplay: 1 - backtrack, 2-forward checking, 3-arc consistency int chosen_cell; int random; //decide whether to use random value from the domain

public: CSP(); ~CSP(); int updateDomain(State state); //for forward checking used. Based on current setting, update the domain. void arcConsistency(const State state); //similar to the updateDomain() in the forward checking, this unction is stronger for arcChecking use bool goalCheck(const State state); bool checkForAssignment(int r, int s, State state, int number); bool checkForRow(int r, int s, State state, int number); bool checkForColumn(int r, int s, State state, int number); bool checkForGrid(int r, int s, State state, int number); void setData(int *data); int goBack(int *chosen_cell); bool backTrack(int *chosen_cell); bool forwardChecking(int *chosen_cell); bool forwardCheckingOrder(int *chosen_cell); bool arcChecking(int *chosen_cell); bool arcCheckingOrder(int *chosen_cell); void clearData(); // this is called everytime when the search algorithm (radio button) is switched to another one. void reshuffleDomain(); //make the domain values in random order void sortDomain(); //make the domain in increasing order };

Complete the function Arc Consistency. Iterator updated_state throws an error. Can you resolve that ?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions

Question

6. Identify seven types of hidden histories.

Answered: 1 week ago

Question

What is human nature?

Answered: 1 week ago