打开/关闭菜单
打开/关闭个人菜单
未登录
如果您进行任何编辑,您的IP地址会公开展示。

用户:JesseM1024:修订间差异

576次编辑2023年6月30日 (星期五)注册
无编辑摘要
第5行: 第5行:
=== 调包 ===
=== 调包 ===
<pre>import numpy as np
<pre>import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from collections import deque</pre>
from collections import deque</pre>


=== 函数定义 ===
=== 函数定义 ===
<pre>def bfs_depth(grid, ini_pos):
<pre>def bfs_depth(grid, ini_pos):
    '''
    grid: 2d np array, only 0, 1
    ini_pos: 2-len python int list, position vec [a,b]


   <nowiki>'''</nowiki>
    returns the depth 2d array
 
    '''
   grid: 2d np array, only 0, 1
    if not grid.size:
 
        return np.array([], dtype=int).reshape(0, 0)
   ini_pos: 2-len python int list, position vec [a,b]
   
 
    # 初始化访问数组并全部填充为-1
   returns the depth 2d array
    visited = np.full_like(grid, -1, dtype=int)
 
    temp = ini_pos.copy()
   <nowiki>'''</nowiki>
    temp.append(0)
 
    # 起始点
   if not grid.size:
    queue = deque([tuple(temp)]) # (row, col, depth)
 
   
       return np.array([], dtype=int).reshape(0, 0)
    # 定义四个方向的移动
 
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
   
   
 
    while queue:
   # 初始化访问数组并全部填充为-1
        row, col, depth = queue.popleft()
 
       
   visited = np.full_like(grid, -1, dtype=int)
        # 如果当前点不是障碍物(假设0表示障碍物),则更新访问数组
 
        if grid[row, col] != 0:
   temp = ini_pos.copy()
            visited[row, col] = depth
 
           
   temp.append(0)
            # 检查相邻的点
 
            for dx, dy in directions:
   # 起始点
                new_row, new_col = row + dx, col + dy
 
               
   queue = deque([tuple(temp)])  # (row, col, depth)
                # 如果相邻点在网格内且未被访问
 
                if 0 <= new_row < grid.shape[0] and 0 <= new_col < grid.shape[1] and visited[new_row, new_col] == -1:
   
                    queue.append((new_row, new_col, depth + 1))
 
   
   # 定义四个方向的移动
    return visited
 
   directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
 
   
 
   while queue:
 
       row, col, depth = queue.popleft()
 
       
 
       # 如果当前点不是障碍物(假设0表示障碍物),则更新访问数组
 
       if grid[row, col] != 0:
 
           visited[row, col] = depth
 
           
 
           # 检查相邻的点
 
           for dx, dy in directions:
 
               new_row, new_col = row + dx, col + dy
 
               
 
               # 如果相邻点在网格内且未被访问
 
               if 0 <= new_row < grid.shape[0] and 0 <= new_col < grid.shape[1] and visited[new_row, new_col] == -1:
 
                   queue.append((new_row, new_col, depth + 1))
 
   
 
   return visited
 




def edge_detector(grid):
def edge_detector(grid):
    row, col = grid.shape[0], grid.shape[1]
    ava_out_pos_list = []
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
   
    for i in range(row):
        for j in range(col):
            if grid[i,j]==1:
                if i==0 or i==row-1 or j==0 or j==col-1:
                    ava_out_pos_list.append([i,j])
                else:
                    exist_0 = False
                    for dx, dy in directions:
                        new_row, new_col = i + dx, j + dy
                        if grid[new_row, new_col] == 0:
                            exist_0 = True
                            break
                    if exist_0 :
                        ava_out_pos_list.append([i,j])


   row, col = grid.shape[0], grid.shape[1]
    return ava_out_pos_list</pre>
 
   ava_out_pos_list = []
 
   directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
 
   
 
   for i in range(row):
 
       for j in range(col):
 
           if grid[i,j]==1:
 
               if i==0 or i==row-1 or j==0 or j==col-1:
 
                   ava_out_pos_list.append([i,j])
 
               else:
 
                   exist_0 = False
 
                   for dx, dy in directions:
 
                       new_row, new_col = i + dx, j + dy
 
                       if grid[new_row, new_col] == 0:
 
                           exist_0 = True
 
                           break
 
                   if exist_0 :
 
                       ava_out_pos_list.append([i,j])
 
   return ava_out_pos_list</pre>


=== 参数定义 ===
=== 参数定义 ===
第137行: 第83行:
=== 运行1:最优结果绘制 ===
=== 运行1:最优结果绘制 ===
<pre>ava_start = edge_detector(crafter_array)
<pre>ava_start = edge_detector(crafter_array)
cur_minimum_steps = np.inf
cur_minimum_steps = np.inf
result = None
result = None
start = None
start = None
edge_result = np.ones((crafter_array.shape[0],crafter_array.shape[1]))
edge_result = np.ones((crafter_array.shape[0],crafter_array.shape[1]))
edge_result = edge_result*-1


edge_result = edge_result*-1


for start_pos in ava_start:
for start_pos in ava_start:
 
    arr = bfs_depth(crafter_array,start_pos)
   arr = bfs_depth(crafter_array,start_pos)
    temp_step = np.max(arr)
 
    edge_result[start_pos[0],start_pos[1]] = temp_step
   temp_step = np.max(arr)
    if temp_step < cur_minimum_steps:
 
        cur_minimum_steps = temp_step
   edge_result[start_pos[0],start_pos[1]] = temp_step
        result = arr
 
        start = start_pos
   if temp_step < cur_minimum_steps:
 
       cur_minimum_steps = temp_step
 
       result = arr
 
       start = start_pos
 
print('minimum steps =',cur_minimum_steps,'and start at ',start)
print('minimum steps =',cur_minimum_steps,'and start at ',start)
print(result)
print(result)
plt.figure()
plt.figure()


plt.imshow(result, cmap='viridis', aspect='auto')
plt.imshow(result, cmap='viridis', aspect='auto')
plt.colorbar()
plt.colorbar()


plt.title('crafter depth matrix')
plt.title('crafter depth matrix')
plt.xlabel('X')
plt.xlabel('X')
plt.ylabel('Y')
plt.ylabel('Y')


第184行: 第113行:
=== 运行2:边缘结果绘制 ===
=== 运行2:边缘结果绘制 ===
<pre>print(edge_result)
<pre>print(edge_result)
plt.figure()
plt.figure()


plt.imshow(edge_result, cmap='viridis', aspect='auto')
plt.imshow(edge_result, cmap='viridis', aspect='auto')
plt.colorbar()
plt.colorbar()


plt.title('all output step matrix')
plt.title('all output step matrix')
plt.xlabel('X')
plt.xlabel('X')
plt.ylabel('Y')
plt.ylabel('Y')



2024年7月31日 (三) 10:29的版本

代码备份如下

机械动力动力合成器矩阵周期最小化.ipynb

调包

import numpy as np
import matplotlib.pyplot as plt
from collections import deque

函数定义

def bfs_depth(grid, ini_pos):
    '''
    grid: 2d np array, only 0, 1
    ini_pos: 2-len python int list, position vec [a,b]

    returns the depth 2d array
    '''
    if not grid.size:
        return np.array([], dtype=int).reshape(0, 0)
    
    # 初始化访问数组并全部填充为-1
    visited = np.full_like(grid, -1, dtype=int)
    temp = ini_pos.copy()
    temp.append(0)
    # 起始点
    queue = deque([tuple(temp)])  # (row, col, depth)
    
    # 定义四个方向的移动
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    
    while queue:
        row, col, depth = queue.popleft()
        
        # 如果当前点不是障碍物(假设0表示障碍物),则更新访问数组
        if grid[row, col] != 0:
            visited[row, col] = depth
            
            # 检查相邻的点
            for dx, dy in directions:
                new_row, new_col = row + dx, col + dy
                
                # 如果相邻点在网格内且未被访问
                if 0 <= new_row < grid.shape[0] and 0 <= new_col < grid.shape[1] and visited[new_row, new_col] == -1:
                    queue.append((new_row, new_col, depth + 1))
    
    return visited


def edge_detector(grid):
    row, col = grid.shape[0], grid.shape[1]
    ava_out_pos_list = []
    directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
    
    for i in range(row):
        for j in range(col):
            if grid[i,j]==1:
                if i==0 or i==row-1 or j==0 or j==col-1:
                    ava_out_pos_list.append([i,j])
                else:
                    exist_0 = False
                    for dx, dy in directions:
                        new_row, new_col = i + dx, j + dy
                        if grid[new_row, new_col] == 0:
                            exist_0 = True
                            break
                    if exist_0 :
                        ava_out_pos_list.append([i,j])

    return ava_out_pos_list

参数定义

crafter_array = np.array([[0,0,0,0,1,0,0,0,0],
                          [0,0,0,1,1,1,0,0,0],
                          [1,1,1,1,1,1,1,1,1],
                          [1,1,1,1,1,1,1,1,1],
                          [0,1,1,1,1,1,1,1,0],
                          [0,0,1,1,1,1,1,0,0],
                          [0,1,1,1,1,1,1,1,0],
                          [1,1,1,1,0,1,1,1,1],
                          [1,1,1,0,0,0,1,1,1]
                         ])

运行1:最优结果绘制

ava_start = edge_detector(crafter_array)
cur_minimum_steps = np.inf
result = None
start = None
edge_result = np.ones((crafter_array.shape[0],crafter_array.shape[1]))
edge_result = edge_result*-1


for start_pos in ava_start:
    arr = bfs_depth(crafter_array,start_pos)
    temp_step = np.max(arr)
    edge_result[start_pos[0],start_pos[1]] = temp_step
    if temp_step < cur_minimum_steps:
        cur_minimum_steps = temp_step
        result = arr
        start = start_pos
print('minimum steps =',cur_minimum_steps,'and start at ',start)
print(result)
plt.figure()

plt.imshow(result, cmap='viridis', aspect='auto')
plt.colorbar()

plt.title('crafter depth matrix')
plt.xlabel('X')
plt.ylabel('Y')

plt.show()

运行2:边缘结果绘制

print(edge_result)
plt.figure()

plt.imshow(edge_result, cmap='viridis', aspect='auto')
plt.colorbar()

plt.title('all output step matrix')
plt.xlabel('X')
plt.ylabel('Y')

plt.show()

CCT获取电脑后方贴着的方块的可用方法.lua

--index--
local target_block  = "back"
local testing_method = 1
local text_scale = 0.5
local has_args = false
local arg = 4
--index end--

curx = 1
cury = 1
--monitor ini
local monitor = peripheral.find("monitor")
monitor.setCursorPos(curx, cury)
monitor.clear()
monitor.setTextScale(text_scale)

local method_table = peripheral.getMethods(target_block)
if method_table ~= nil then
    while(true) do
        curx = 1
        cury = 1
        monitor.setCursorPos(curx, cury)
        --method test, add arg if needed
        
        for i = 1, #method_table do
            --iterate through method table and write it on screen
            if i == testing_method then
                --setting highlight color
                monitor.setTextColor(colors.lightBlue)
            else
                monitor.setTextColor(colors.white)
            end
            monitor.write(i..": "..method_table[i])
            cury = cury+1
            monitor.setCursorPos(curx, cury)
        end

        --start testing method
        if has_args then
            local test_result = peripheral.call(target_block,method_table[testing_method],arg)
            monitor.write("method "..testing_method.." w/ arg= "..arg.." has value:")
        else
            local test_result = peripheral.call(target_block,method_table[testing_method])
            monitor.write("method "..testing_method.." has value:")
        end
        
        --start a new line and write the result
        cury = cury+1
        monitor.setCursorPos(curx, cury)
        monitor.write(tostring(test_result))
        sleep(1)
        --refresh every second
    end
end