Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let calcUpperBound problem =
- let N = problem.nodeCount
- let findEdge = getEdge problem.graph
- let rec loop tour unvisitedNodes includedEdges =
- match tour with
- | [] -> failwith "Empty list is bad"
- | xs when xs.Length = N -> tour
- | current :: xs ->
- let f = List.tryFind (fun f -> f.node1 = current || f.node2 = current) includedEdges
- match f with
- | Some(edge) ->
- let next =
- if edge.node1 = current then edge.node2
- else edge.node1
- loop (next :: tour) (removeFirst current unvisitedNodes) (removeFirst edge includedEdges)
- | None ->
- let next =
- List.map (fun x -> (x, findEdge current x |> edgeWeight)) unvisitedNodes
- |> List.minBy (fun (_, weight) -> weight)
- |> fst
- loop (next :: tour) (removeFirst current unvisitedNodes) includedEdges
- let allNodes = [ 0..problem.nodeCount - 1 ]
- let makeTour i =
- let t = loop [ i ] (removeFirst i allNodes) (List.ofSeq problem.includedEdges)
- let tour: Graph.Tour =
- { nodes = t
- totalWeight = tourWeight problem.graph t }
- tour
- let results = List.map makeTour allNodes
- List.minBy (fun (f: Graph.Tour) -> f.totalWeight) results
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement