mapNumEl = numel(mapSize);cameFrom = zeros(1, mapNumEl);
时间: 2024-04-03 12:30:37 浏览: 46
这两行代码的作用是为A*算法中的cameFrom数组分配内存空间。第一行代码计算出地图大小(mapSize)中所有元素的个数,并将其赋值给mapNumEl变量。第二行代码创建一个与mapNumEl大小相同的1维数值型数组cameFrom,并将其所有元素初始化为0。在A*算法中,cameFrom数组用于存储每个节点的父节点,因此需要为cameFrom数组分配足够的内存空间。通过计算地图大小中所有元素的个数,可以确定cameFrom数组的大小,从而为其分配内存空间。
相关问题
if ~islogical(map) error('MAP must be logical') end if ~isa(costs, 'double') error('COSTS must be double') end % Avoid div by zero costs(costs == 0) = eps; % Normalize such that smallest cost is 1. costs = costs / min(costs(:)); % default return - empty for failure case final = []; mapSize = size(map); mapNumEl = numel(mapSize); % Initialize the open set, with START openSet = false(mapSize); openSet(start) = true; % Initialize closed set. Closed set consists of visited locations on % the map closedSet = false(mapSize); cameFrom = zeros(1, mapNumEl); gScore = inf(mapSize); gScore(start) = 0; % Linear index -> row, col subscripts for the goal [gr, gc] = ind2sub(mapSize, goal); fScore = inf(mapSize); fScore(start) = compute_cost(mapSize, start, gr, gc); S2 = sqrt(2);
这段代码是一个 A* 算法的实现,用于在二维逻辑地图上找到从起点到终点的最短路径。其中,map 表示逻辑地图,costs 表示移动到每个位置所需的代价,start 表示起点,goal 表示终点。该算法使用 openSet 和 closedSet 两个集合来记录搜索过程中的开放节点和关闭节点。在搜索过程中,根据当前节点的代价和预期代价,选择下一个要扩展的节点,直至找到终点或无法继续扩展为止。
详细解释如下matlab中的自定义函数:function net = cnnsetup(net, x, y) assert(~isOctave() || compare_versions(OCTAVE_VERSION, '3.8.0', '>='), ['Octave 3.8.0 or greater is required for CNNs as there is a bug in convolution in previous versions. See http://savannah.gnu.org/bugs/?39314. Your version is ' myOctaveVersion]); inputmaps = 1; mapsize = size(squeeze(x(:, :, 1))); for l = 1 : numel(net.layers) % layer if strcmp(net.layers{l}.type, 's') mapsize = mapsize / net.layers{l}.scale; assert(all(floor(mapsize)==mapsize), ['Layer ' num2str(l) ' size must be integer. Actual: ' num2str(mapsize)]); for j = 1 : inputmaps net.layers{l}.b{j} = 0; end end if strcmp(net.layers{l}.type, 'c') mapsize = mapsize - net.layers{l}.kernelsize + 1; fan_out = net.layers{l}.outputmaps * net.layers{l}.kernelsize ^ 2; for j = 1 : net.layers{l}.outputmaps % output map fan_in = inputmaps * net.layers{l}.kernelsize ^ 2; for i = 1 : inputmaps % input map net.layers{l}.k{i}{j} = (rand(net.layers{l}.kernelsize) - 0.5) * 2 * sqrt(6 / (fan_in + fan_out)); net.layers{l}.dk_old{i}{j} = zeros(size(net.layers{l}.k{i}{j})); net.layers{l}.dk{i}{j} = net.layers{l}.dk_old{i}{j}; end net.layers{l}.b{j} = 0; end inputmaps = net.layers{l}.outputmaps; end end % 'onum' is the number of labels, that's why it is calculated using size(y, 1). If you have 20 labels so the output of the network will be 20 neurons. % 'fvnum' is the number of output neurons at the last layer, the layer just before the output layer. % 'ffb' is the biases of the output neurons. % 'ffW' is the weights between the last layer and the output neurons. Note that the last layer is fully connected to the output layer, that's why the size of the weights is (onum * fvnum) fvnum = prod(mapsize) * inputmaps; onum = size(y, 1); net.ffb = zeros(onum, 1); net.ffW = (rand(onum, fvnum) - 0.5) * 2 * sqrt(6 / (onum + fvnum)); end
这是一个用于设置卷积神经网络的自定义函数,函数名为“cnnsetup”,其输入参数为“net”、“x”和“y”,输出参数为“net”。该函数的目的是初始化神经网络的参数。
首先,在该函数中使用assert函数进行判断,如果运行该函数的Octave版本小于3.8.0,则会抛出异常,并给出一个链接以解决这个问题。
接下来,该函数会计算输入数据的通道数,并确定第一层卷积层的输出尺寸。然后,对于每一层,根据该层的类型和参数对输出尺寸进行更新,并初始化该层的权重和偏置。
对于卷积层,该函数会根据卷积核的大小、输入和输出通道数等参数初始化该层的权重。对于全连接层,该函数会计算全连接层的输入和输出尺寸,并初始化该层的权重和偏置。
最后,该函数返回初始化后的神经网络参数“net”。
阅读全文