# QuestionBacktracking Search in C#

#### RooterBoy

##### New member
Hi everyone,

I have a question about backtracking search in C#.

I have a python code about it. I tried many times convert from python to C#, but i couldn't do it. Would you help me, please?

Some friends are professionel at this work, so i guess they will help me.

Python code: http://cdn.cs50.net/ai/2020/spring/lectures/3/src3/scheduling/schedule0.py

C#:
``````"""
Naive backtracking search without any heuristics or inference.
"""

VARIABLES = ["A", "B", "C", "D", "E", "F", "G"]
CONSTRAINTS = [
("A", "B"),
("A", "C"),
("B", "C"),
("B", "D"),
("B", "E"),
("C", "E"),
("C", "F"),
("D", "E"),
("E", "F"),
("E", "G"),
("F", "G")
]

def backtrack(assignment):
"""Runs backtracking search to find an assignment."""

# Check if assignment is complete
if len(assignment) == len(VARIABLES):
return assignment

# Try a new variable
var = select_unassigned_variable(assignment)
for value in ["Monday", "Tuesday", "Wednesday"]:
new_assignment = assignment.copy()
new_assignment[var] = value
if consistent(new_assignment):
result = backtrack(new_assignment)
if result is not None:
return result
return None

def select_unassigned_variable(assignment):
"""Chooses a variable not yet assigned, in order."""
for variable in VARIABLES:
if variable not in assignment:
return variable
return None

def consistent(assignment):
"""Checks to see if an assignment is consistent."""
for (x, y) in CONSTRAINTS:

# Only consider arcs where both are assigned
if x not in assignment or y not in assignment:
continue

# If both have same value, then not consistent
if assignment[x] == assignment[y]:
return False

# If nothing inconsistent, then assignment is consistent
return True

solution = backtrack(dict())
print(solution)``````

Output of this Python code block:

This is my tried code block:

C# Code:
``````namespace Namespace {
using System;
using System.Collections.Generic;
public class Module {
public List<string> VARIABLES = new List<string> {"A","B","C","D","E","F","G"};
public object CONSTRAINTS = new List<object> {
("A", "B"),

("A", "C"),

("B", "C"),

("B", "D"),

("B", "E"),

("C", "E"),

("C", "F"),

("D", "E"),

("E", "F"),

("E", "G"),

("F", "G")

};

// Runs backtracking search to find an assignment.

public object backtrack(List<string> assignment) {
// Check if assignment is complete

if (assignment.Count == VARIABLES.Count) {
return assignment;
}
// Try a new variable
var variable = Convert.ToInt32(select_unassigned_variable(assignment));
foreach (var value in new List<object> {
"Monday",
"Tuesday",
"Wednesday"
}) {
var new_assignment = assignment;
new_assignment[variable] = value.ToString();
if (Convert.ToBoolean(consistent(new_assignment))) {
var result = backtrack(new_assignment);
if (result != null) {
return result;

}
}
}
return null;
}
// Chooses a variable not yet assigned, in order.

public object select_unassigned_variable(List<string> assignment) {
foreach (var variable in VARIABLES) {
if (!assignment.Contains(variable)) {

return variable;

}
}
return null;
}
// Checks to see if an assignment is consistent.

public object consistent(List<string> assignment) {
foreach (var cons in CONSTRAINTS) {
var x = cons.Item1;
var y = cons.Item2;
// Only consider arcs where both are assigned
if (!assignment.Contains(x) || !assignment.Contains(y)) {
continue;
}
// If both have same value, then not consistent
if (assignment[x] == assignment[y]) {
return false;
}
}
// If nothing inconsistent, then assignment is consistent
return true;
}
//object solution = backtrack(new List<string>());

}

}``````

Last edited by a moderator:

#### Skydiver

Staff member
What problem are you specifically running into? Does the code compile? If not, what errors are you getting? If the code does compile, but running the code gives unexpected results, what results were you expecting to see, and what results are you actually getting? What have you done to try to debug your problem?

#### RooterBoy

##### New member
I code compiled. I have many errors. I would be glad if someone who knows c# as well can help me.

Compilation error (line 7, col 17): ) expected
Compilation error (line 7, col 22): Syntax error, ',' expected
Compilation error (line 33, col 16): Expected class, delegate, enum, interface, or struct
Compilation error (line 41, col 39): Expected class, delegate, enum, interface, or struct
Compilation error (line 47, col 42): Expected class, delegate, enum, interface, or struct
Compilation error (line 56, col 13): A namespace cannot directly contain members such as fields or methods
Compilation error (line 57, col 9): Type or namespace definition, or end-of-file expected

Replies
12
Views
575
Replies
3
Views
283
Replies
1
Views
116
Replies
8
Views
412
Replies
4
Views
188