Rewrite Bellman-Ford and min-cost flow, especially to stop the latter from crashing.
[match/match.git] / program / Test.hs
CommitLineData
967c39ef
MM
1module Test (
2 -- Export everything we need to have fun in GHCi:
3
4 -- See the results of examples.
5 module Test,
6
7 -- Generate instances.
8 module Instance,
9 module InstanceGenerator,
10
11 -- Solve instances.
12 module ProposalMatcher,
13 module ProposalMatcherConfig,
14
15 -- Run randomized things.
16 module System.Random,
17 module RandomizedMonad,
18
19 -- Visualize graphs.
20 module Data.Graph.Inductive.Graphviz
21) where
22import Instance
23import InstanceGenerator
24import ProposalMatcher
25import ProposalMatcherConfig
26import System.Random
27import RandomizedMonad
28import Data.Graph.Inductive.Graphviz
29
30-- Other imports we need
d7d9561e 31import BellmanFord
5a07db44 32import NaiveMinCostFlow
967c39ef
MM
33import Data.Array.IArray
34import Data.Array.Unboxed
d7d9561e
MM
35import Data.Graph.Inductive.Graph
36import Data.Graph.Inductive.Tree
967c39ef 37import ArrayStuff
2e7d5426 38
d7d9561e 39myGraph = mkGraph [(0, ()), (1, ()), (2, ())]
5a07db44 40 [(0, 1, (0, 2)), (0, 2, (1, 3)), (2, 1, (2, -2))] :: Gr () (Int, Int)
d7d9561e 41
5a07db44 42bfResult = bellmanFord snd 0 myGraph
d7d9561e 43
5a07db44
MM
44flowArray = minCostFlow (0, 2) fst (const 1) snd myGraph (0, 1)
45
46myNCGraph = mkGraph [(0, ())] [(0, 0, -1)] :: Gr () Int
47bfNCResult = bellmanFord id 0 myNCGraph
48
49data REdgeF = REdgeF Int Int Int Wt
50instance Show REdgeF where
51 show (REdgeF idx cap flow cost) = "#" ++ (show idx) ++ ": "
52 ++ (show flow) ++ " of " ++ (show cap) ++ " @ " ++ (show cost)
53flowAnnotate g fa =
54 mkGraph (labNodes g) (map (\(n1, n2, REdge i ca co) ->
55 (n1, n2, REdgeF i ca (fa ! i) co)) $ labEdges g) :: Gr () REdgeF
d7d9561e
MM
56
57-- Example from idea book p. 425
58{-
59(myNumRvrs, myNumProps) = (4, 3)
60
61myPrefsArray = array ((0,0), (myNumRvrs-1,myNumProps-1)) [
62 ((0, 0), 15), ((1, 0), 10), ((2, 0), 15), ((3, 0), 40),
63 ((0, 1), 30), ((1, 1), 7), ((2, 1), 10), ((3, 1), 15),
64 ((0, 2), 15), ((1, 2), 25), ((2, 2), 20), ((3, 2), 20)
65 ]
66-}
67
68(myNumRvrs, myNumProps) = (5, 3)
69
967c39ef
MM
70myPrefs = transposeArray $ listArray ((0,0), (myNumProps-1,myNumRvrs-1)) [
71 15, 10, 15, 40, 20,
72 30, 7, 10, 15, 15,
73 15, 25, 20, 20, 15
74 ] :: UArray (Int, Int) Wt
d7d9561e 75
967c39ef 76myInst = Instance myNumRvrs myNumProps (funcArray (0, myNumRvrs-1) $ const 1) myPrefs
d7d9561e 77
5a07db44
MM
78rdnResult = doReduction myInst
79ReductionResult rrg rrso rrsi rreib rredi = rdnResult
80rdnFlowArray = minCostFlow rreib reIdx reCap reCost rrg (rrso, rrsi)
81rrg2 = flowAnnotate rrg rdnFlowArray
d7d9561e 82myMatching = doMatching myInst