资源简介
Introduction
In this project, you are going to construct a CSP for N-Queens problem. You will be given several python files.
Files to Edit and Submit: You will fill in portions of submission.py during the assignment. You should submit this file with your code and comments. Please do NOT change th
In this project, you are going to construct a CSP for N-Queens problem. You will be given several python files.
Files to Edit and Submit: You will fill in portions of submission.py during the assignment. You should submit this file with your code and comments. Please do NOT change th

代码片段和文件信息
import copy
from csp import CSP
def create_n_queens_csp(n=8):
“““Create an N-Queen problem on the board of size n * n.
You should call csp.add_variable() and csp.add_binary_factor().
Args:
n: int number of queens or the size of one dimension of the board.
Returns
csp: A CSP problem with correctly configured factor tables
such that it can be solved by a weighted CSP solver
“““
csp = CSP()
# TODO: Problem b
# TODO: BEGIN_YOUR_CODE
variables = []
for i in range(n):
# name variables from y1 to yn
var = ‘y{}‘.format(i+1)
# assign values to variables
csp.add_variable(varrange(1n+1))
variables.append(var)
for i in range(len(variables)):
for j in range(len(variables)):
if variables[i] != variables[j]:
distance = abs(i-j)
# Generate binary constraints between queens
csp.add_binary_factor(variables[i] variables[j] lambda y1 y2: y1 != y2 and distance != abs(y1-y2))
#raise NotImplementedError
# TODO: END_YOUR_CODE
return csp
class BacktrackingSearch:
“““A backtracking algorithm that solves CSP.
Attributes:
num_assignments: keep track of the number of assignments
(identical when the CSP is unweighted)
num_operations: keep track of number of times backtrack() gets called
first_assignment_num_operations: keep track of number of operations to
get to the very first successful assignment (maybe not optimal)
all_assignments: list of all solutions found
csp: a weighted CSP to be solved
mcv: bool if True use Most Constrained Variable heuristics
ac3: bool if True AC-3 will be used after each variable is made
domains: dictionary of domains of every variable in the CSP
Usage:
search = BacktrackingSearch()
search.solve(csp)
“““
def __init__(self):
self.num_assignments = 0
self.num_operations = 0
self.first_assignment_num_operations = 0
self.all_assignments = []
self.csp = None
self.mcv = False
self.ac3 = False
self.domains = {}
def reset_results(self):
“““Resets the statistics of the different aspects of the CSP solver.“““
self.num_assignments = 0
self.num_operations = 0
self.first_assignment_num_operations = 0
self.all_assignments = []
def check_factors(self assignment var val):
“““Check consistency between current assignment and a new variable.
Given a CSP a partial assignment and a proposed new value for a
variable return the change of weights after assigning the variable
with the proposed value.
Args:
assignment: A dictionary of current assignment.
Unassigned variables do not have entries while an assigned
variable has the assigned va
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 10420 2019-10-24 22:57 submission.py
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论