Rewrite Bellman-Ford and min-cost flow, especially to stop the latter from crashing.
[match/match.git] / program / Test.hs
1 module 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
22 import Instance
23 import InstanceGenerator
24 import ProposalMatcher
25 import ProposalMatcherConfig
26 import System.Random
27 import RandomizedMonad
28 import Data.Graph.Inductive.Graphviz
29
30 -- Other imports we need
31 import BellmanFord
32 import NaiveMinCostFlow
33 import Data.Array.IArray
34 import Data.Array.Unboxed
35 import Data.Graph.Inductive.Graph
36 import Data.Graph.Inductive.Tree
37 import ArrayStuff
38
39 myGraph = mkGraph [(0, ()), (1, ()), (2, ())]
40         [(0, 1, (0, 2)), (0, 2, (1, 3)), (2, 1, (2, -2))] :: Gr () (Int, Int)
41
42 bfResult = bellmanFord snd 0 myGraph
43
44 flowArray = minCostFlow (0, 2) fst (const 1) snd myGraph (0, 1)
45
46 myNCGraph = mkGraph [(0, ())] [(0, 0, -1)] :: Gr () Int
47 bfNCResult = bellmanFord id 0 myNCGraph
48
49 data REdgeF = REdgeF Int Int Int Wt
50 instance Show REdgeF where
51         show (REdgeF idx cap flow cost) = "#" ++ (show idx) ++ ": "
52                 ++ (show flow) ++ " of " ++ (show cap) ++ " @ " ++ (show cost)
53 flowAnnotate 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
56
57 -- Example from idea book p. 425
58 {- 
59 (myNumRvrs, myNumProps) = (4, 3)
60
61 myPrefsArray = 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
70 myPrefs = 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
75
76 myInst = Instance myNumRvrs myNumProps (funcArray (0, myNumRvrs-1) $ const 1) myPrefs
77
78 rdnResult = doReduction myInst
79 ReductionResult rrg rrso rrsi rreib rredi = rdnResult
80 rdnFlowArray = minCostFlow rreib reIdx reCap reCost rrg (rrso, rrsi)
81 rrg2 = flowAnnotate rrg rdnFlowArray
82 myMatching = doMatching myInst