- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
This is mostly a consulting question.
我开发了一种遗传算法来解决 TSP ,长话短说,我写了两个不同的代码,我用了 this作为我的数据集。程序找到的解决方案如下所示。
很明显,第一个程序 ( Prog# 1
) 的建议解决方案比第二个程序 ( Prog# 2
) 更有希望被优化;和来自 Prog# 2
的解决方案似乎是最有可能的随机解决方案。
但是 Prog# 1
的成本正如我计算的那样是 97314.36
和 Prog# 2
的成本是 74635.31
这比 Prog# 1
的解决方案成本几乎 20K 小 ,并且由于成本暗示了 Prog# 2
找到的解决方案应该比第一个解决方案优化得多。
1) 为什么Prog# 2
找到解的路径图?不支持(视觉上)计算的成本值?
2) 考虑到打击脚本,有什么我遗漏的吗?
为了完整起见,我发布了用于绘制和计算成本值的脚本。
function tsp_plot_output()
close all;
disp('loading data....');
x=load('out.res');
dist = 0;
for i=1:2:size(x, 1)-1
dist = dist + norm(x(i,:) - x(i+1,:), 2);
end
dist = dist + norm(x(1,:) - x(end,:), 2);
fprintf('COST IS: %.2f\n', dist);
disp('ploting data....');
xx = x(:,1); xy = x(1:size(xx, 1),2);
zxx = x(:,1); zxy = x(1:size(zxx),2);
plot(xx, xy), title('Found TSP solution');
hold
plot(zxx, zxy, 'r.');
end
我在 Prog# 1
中使用的代码倒出溶液是
std::ofstream os(outputfile);
BOOST_FOREACH(size_t city, *best->_genes) {
auto loc = _data->get(city);
os<<loc.longitude<<" "<<loc.latitude<<endl;
}
os.close();
Prog# 2
中的代码相同
ofstream out(jconfig["output-file"].as_string());
for(int i = 0; i < p->lchrom; i++) {
city c = data.at(best_found->chrom[i]);
out<<c.x<<" "<<c.y<<endl;
}
out.close();
最佳答案
你在 MATLAB 中的距离计算是错误的。你有:
dist = 0;
for i=1:2:size(x, 1)-1
dist = dist + norm(x(i,:) - x(i+1,:), 2);
end
dist = dist + norm(x(1,:) - x(end,:), 2);
for i=1:2:size(x,1)-1
你从 i=1
开始,然后添加 2
在每个步骤中,直到达到 size(x,1)-1
。因此,您添加从 1-2
到 3-4
的距离,依此类推。当然应该是从1-2
开始,然后是2-3
等等。这是通过
dist = 0;
for k=1:size(x,1)-1
dist = dist + norm(x(k+1,:) - x(k,:),2);
end
dist = dist + norm(x(end,:) - x(1,:),2);
举个例子x = [0,0; 1,1; 1,0]
旧例程返回 2.4142
,而更正后的例程返回正确的 sqrt(2) + 1 + 1 = 3.4142
。
PS:我把运行变量改成了k
,因为在MATLAB中i
代表虚数单位(详见this question)。我还更改了 norm
中 x
的顺序。当然你的没有错,但是这样很明显你从当前点 k
到下一个点 k+1
而不是另一个方向.
关于c++ - TSP解决方案解读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29536372/
引言 深拷贝是指创建一个新对象,该对象的值与原始对象完全相同,但在内存中具有不同的地址。这意味着如果您对原始对象进行更改,则不会影响到复制的对象 常见的C#常见的深拷贝方式有以下4类:
人工智能是一种未来性的技术,目前正在致力于研究自己的一套工具。一系列的进展在过去的几年中发生了:无事故驾驶超过300000英里并在三个州合法行驶迎来了自动驾驶的一个里程碑;IBM Waston击败了
我已经阅读了所有 HERE Maps API 文档,但找不到答案。 HERE实时流量REST API输出中的XML标签是什么意思? 有谁知道如何解释这个输出(我在我的请求中使用了接近参数)? 最佳答
我的 iPad 应用程序工作正常,我将其留在现场进行测试,但现在崩溃了[保存时?] 这是崩溃日志, Incident Identifier: 80FC6810-9604-4EBA-A982-2009A
我的程序需要 qsort 的功能才能运行,但到目前为止还没有完成它的工作。 我实际上是在对单个字符值的数组进行排序,以便将它们分组,这样我就可以遍历数组并确定每个属性的计数。我的问题是 qsort 返
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
我正在尝试使用 AVR 代码对 Arduino Uno 进行编程,因为我不被允许在 9 月份开始的高级项目中使用 Arduino 库。我找到了数据表,让数字引脚正常工作,然后尝试通过 USB 串行连接
我遇到了多次崩溃,似乎 native iOS 方法正在从第三方库调用函数。这是一个例子: Thread: Unknown Name (Crashed) 0 libsystem_kernel.d
我理解如何按照 Dijkstra 算法的解释找到从头到尾的最短路径,但我不明白的是解释。在这里,从图中的图形来看,从 A 到 E 添加到我已知集合的顺序是 A,C,B,D,F,H,G,E 我没有得到的
我正在查看一些 Django 源代码并遇到了 this . encoding = property(lambda self: self.file.encoding) 究竟是做什么的? 最佳答案 其他两
Sentry 提供了很好的图表来显示消息频率,但关于它们实际显示的内容的信息很少。 这些信息是每分钟吗? 5分钟? 15分钟?小时? 最佳答案 此图表按分钟显示。这是负责存储该图数据的模型。 http
我对 JavaScript 和 Uniswap 还很陌生。我正在使用 Uniswap V3 从 DAI/USDC 池中获取价格。我的“主要”功能如下所示: async function main()
我正在尝试弄清楚我下载的 Chrome 扩展程序是如何工作的(这是骗子用来窃取 CS:GO 元素的东西,并不重要...)。我想知道使用什么电子邮件地址(或使用什么其他通信方式)来提交被钓鱼的数据。 这
引言 今天同事问了我一个问题, System.Windows.Forms.Timer 是前台线程还是后台线程,我当时想的是它是跟着UI线程一起结束的,应该是前台线程吧? 我确实没有仔
我需要一些使用 scipy.stats.t.interval() 函数的帮助 http://docs.scipy.org/doc/scipy/reference/generated/scipy.sta
当我在 Oracle 查询计划中看到类似的内容时: HASH JOIN TABLE1 TABLE2 这两个表中的哪一个是 hashed ? Oracle 文档指的是通常被散列的“较小”
我想知道 GridSearchCV 返回的分数与按如下方式计算的 R2 指标之间的差异。在其他情况下,我收到的网格搜索分数非常负(同样适用于 cross_val_score),我将不胜感激解释它是什么
本文分享自华为云社区《 多主创新,让云数据库性能更卓越 》,作者: GaussDB 数据库。 华为《Taurus MM: bringing multi-master to the clou
我真的需要一些帮助来破译这个崩溃报告: Process: Farm Hand [616] Path: /Applications/Farm
我写了一个从 YUV_420_888 到 Bitmap 的转换,考虑到以下逻辑(据我所知): 总结该方法:内核的坐标 x 和 y 与 Y 平面(2d 分配)的非填充部分的 x 和 y 以及输出位图的
我是一名优秀的程序员,十分优秀!