资源简介
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实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论