如何解决SML中的旅行商问题?

发布于 2024-08-24 21:00:27 字数 59 浏览 4 评论 0 原文

有人有标准机器学习中的旅行推销员问题解决方案吗,请告诉我。

我已经尝试了很多但没有成功。

Does anybody have a traveling salesman problem solution in Standard ML, please tell me.

I've tried a lot but am not successful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-08-31 21:00:27

旅行推销员的强力解决方案非常简单。您填充可能路径的列表并选择最小的路径。

至于在 SML 中执行此操作,有多种方法。它首先取决于您使用什么数据结构来执行此操作,其次取决于您是否希望使用“惰性”函数/流。

我给您的建议是首先编写一个简单的路径查找器,然后将其扩展以将所有路径生成为列表或其他数据结构。最后对该列表进行排序以获得最小的行程长度。在考虑如何解决此问题时,请使用 wiki 上的 TSP 作为有用的资源。

抱歉,我不关心为别人做作业。

很好的SML参考,以及另一个

The brute force solution to traveling salesman is very straight forward. You populate a list of possible paths and pick the smallest one.

As for doing this in SML there are a myriad of methods. It will firstly depend on what data structure you are using to do this, and secondly on whether or not you wish to use 'lazy' functions / streams.

My suggestion to you is to code a simple path finder first, then extend it to generating all paths as a list or other data structure. Finally sort that list for the smallest trip length. Use the TSP on wiki as a helpful resource when considering how to go about solving this problem.

I'm sorry, but I'm not in the business of doing others homework for them.

Good SML reference, and another

〆一缕阳光ご 2024-08-31 21:00:27

我不知道如何在 SML 中使用 2D 数组。这是一个 F# 解决方案:

let salesman2 (g:int array array) = 
    let n = Array.length g
    let rec salesman' visited last acc = 
        if Set.count visited = n then acc
        else 
            {0..n-1} 
            |> Seq.filter (fun i->not (Set.contains i visited)) 
            |> Seq.map (fun i->salesman' (Set.add i visited) i (acc + g.[last].[i]))
            |> Seq.min
    salesman' Set.empty 0 0 

let g = [|[|0;1;2;4|]; [|1;0;2;2;|]; [|2;2;0;3|]; [|4;2;3;0|] |]
salesman2 g

如果您了解 SML,那么将其转换为 SML 应该很简单。

I don't know how to use 2D arrays in SML. This is an F# solution:

let salesman2 (g:int array array) = 
    let n = Array.length g
    let rec salesman' visited last acc = 
        if Set.count visited = n then acc
        else 
            {0..n-1} 
            |> Seq.filter (fun i->not (Set.contains i visited)) 
            |> Seq.map (fun i->salesman' (Set.add i visited) i (acc + g.[last].[i]))
            |> Seq.min
    salesman' Set.empty 0 0 

let g = [|[|0;1;2;4|]; [|1;0;2;2;|]; [|2;2;0;3|]; [|4;2;3;0|] |]
salesman2 g

Translating it into SML should be straightforward if you know SML.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文