RooterBoy
New member
- Joined
- Dec 10, 2020
- Messages
- 2
- Programming Experience
- 1-3
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
Mod Edit Add code to post please :
Output of this Python code block:
This is my tried code block:
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
Mod Edit Add code to post please :
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: