人工智能 第6章 课后题 6.10着色问题的随机地图生成 python

人工智能 第三版 课后题 6.10 着色问题随机地图生成

  • 人工智能 第6章 6.10着色问题的随机地图生成 python
    • python代码

人工智能 第6章 6.10着色问题的随机地图生成 python

有1、、繁琐,写的不够漂亮,还在努力中。。。

随机的14个点浏览图片: 人工智能 第6章 课后题 6.10着色问题的随机地图生成 python_第1张图片

python代码

import random
import numpy as np
import matplotlib.pyplot as plt
n = 14 ### n个随机点
data_list = []
random.seed(10)
#随机生成n个坐标点
X = random.sample(range(0, 100), n)
Y = random.sample(range(0, 100), n)
#print(X,Y)
plt.plot(X,Y,'ro')
def l2dist(vector1,vector2):####计算两点坐标的距离
    vector1 =np.array(vector1)
    vector2 = np.array(vector2)
    op1 = np.sqrt(np.sum(np.square(vector1-vector2)))
    op2 = np.linalg.norm(vector1-vector2)
    return op2

distance_matrix = np.zeros((n,n))
connect_matrix = np.zeros((n,n))
#######计算距离矩阵
for i in range(n):
    for j in range(n):
        distance_matrix[i,j]=l2dist([X[i],Y[i]],[X[j],Y[j]])


def cross_point(point11,point12, point21,point22):  # 计算两条直线的相交的交点
    x1 = point11[0]  # 取四点坐标
    y1 = point11[1]
    x2 = point12[0]
    y2 = point12[1]

    x3 = point21[0]
    y3 = point21[1]
    x4 = point22[0]
    y4 = point22[1]

    if (x4 - x3) == 0:  # L2直线斜率不存在操作
        k2 = None
        b2 = 0
        x = x3
        k1 = (y2 - y1) * 1.0 / (x2 - x1)  # 计算k1,由于点均为整数,需要进行浮点数转化
        b1 = y1 * 1.0 - x1 * k1 * 1.0  # 整型转浮点型是关键
        y = k1 * x * 1.0 + b1 * 1.0
    elif (x2 - x1) == 0:
        k1 = None
        b1 = 0
        x = x1
        k2 = (y4 - y3) * 1.0 / (x4 - x3)
        b2 = y3 * 1.0 - x3 * k2 * 1.0
        y = k2 * x * 1.0 + b2 * 1.0
    else:
        k1 = (y2 - y1) * 1.0 / (x2 - x1)  # 计算k1,由于点均为整数,需要进行浮点数转化
        k2 = (y4 - y3) * 1.0 / (x4 - x3)  # 斜率存在操作
        if k1 == k2:
            return False
        else:
            b1 = y1 * 1.0 - x1 * k1 * 1.0  # 整型转浮点型是关键
            b2 = y3 * 1.0 - x3 * k2 * 1.0
            x = (b2 - b1) * 1.0 / (k1 - k2)
            y = k1 * x * 1.0 + b1 * 1.0
    return [x, y]
def jiaocha(point11,point12,point21,point22):#######用来判断两条线段是否交叉
    point= cross_point(point11,point12, point21,point22)
    if point == False:
        return False
    #print([xx,yy])
    if max(min(point11[0],point12[0]),min(point21[0],point22[0]))<point[0]<min(max(point11[0],point12[0]),max(point21[0],point22[0])) and max(min(point11[1], point12[1]), min(point21[1], point22[1])) < point[1] < min(max(point11[1], point12[1]),
                                                                                 max(point21[1], point22[1])):
        return True
    else:
        return False


def connect_matrix(distance_matrix):######用来输出连接矩阵
    connect_matrix = np.zeros((n,n))
    connect_point = []
    for k in range(100):#######随机选取k个点进行连接
        y = random.randint(0, n-1)
        not_connect = []
        not_connect_point = [i for i, x in enumerate(connect_matrix[:, y]) if x == 0]##首先将于y点未连接的点选取到一个列表中
        not_connect_point.remove(y)
        for i in not_connect_point:
            not_connect.append(distance_matrix[i,y])######将未连接的点与y的距离输入到列表中
        jiaocha_point = []
        if not_connect_point !=[]:######用来判断是否还有点与y点相连接
            for j in not_connect_point:
                jiaocha_list = []
                if connect_point == []:######用来判断已连接的点集是否为空集
                    not_connect.append(distance_matrix[j, y])
                else:
                    for p in connect_point:###将与已连接的线段是否相交的判断结果输入到列表中
                        jiaocha_list.append(jiaocha([X[j], Y[j]], [X[y], Y[y]], [X[p[0]], Y[p[0]]], [X[p[1]], Y[p[1]]]))
                if True in jiaocha_list:########如果存在交叉
                    jiaocha_point.append(j)
            for i in jiaocha_point:######将存在交叉情况的点从与y未连接的点中删除
                ii=not_connect_point.index(i)
                del not_connect[ii]
                not_connect_point.remove(i)
        if not_connect != []:######如果还存在未连接的点,在这些点中找到距离最近的点
            min_index = not_connect_point[not_connect.index(min(not_connect))]
        else:
            continue
            #######将距离最近的点与y点向量
        connect_matrix[min_index, y] = 1
        connect_matrix[y, min_index] = 1
        connect_point.append([y,min_index])
        #返回连接矩阵
    return connect_matrix


connect = connect_matrix(distance_matrix)
print(connect)
####对连线的点进行画图
for i in range(n):
    for j in range(i,n):
        if connect[i,j]==1:
            xx=[X[i],X[j]]
            yy=[Y[i],Y[j]]
            plt.plot(xx,yy)
plt.show()

原创内容,转载请说明!

你可能感兴趣的:(python,人工智能)