Commit a7ca326b authored by Alexandre Delanoë's avatar Alexandre Delanoë

[ILouvain] ok for consecutives steps, testing now.

parent 2b1d848a
...@@ -183,25 +183,31 @@ modulare :: DynGraph gr => gr a b -> Set Node -> Double ...@@ -183,25 +183,31 @@ modulare :: DynGraph gr => gr a b -> Set Node -> Double
modulare = hmodularity modulare = hmodularity
hmodularity :: DynGraph gr => gr a b -> Set Node -> Double hmodularity :: DynGraph gr => gr a b -> Set Node -> Double
hmodularity gr ns = coverage - edgeDensity hmodularity g ns = coverage - edgeDensity
where where
coverage :: Double coverage :: Double
coverage = sizeSubGraph / sizeAllGraph coverage = -- trace ("coverage" :: Text) $
where sizeSubGraph / sizeAllGraph
sizeSubGraph :: Double where
sizeSubGraph = fromIntegral ( G.size $ subgraph' ns gr ) sizeSubGraph :: Double
sizeSubGraph = -- trace ("sizeSubGraph" :: Text) $
fromIntegral ( G.size $ subgraph' ns g )
sizeAllGraph :: Double sizeAllGraph :: Double
sizeAllGraph = fromIntegral (G.size gr) sizeAllGraph = -- trace ("sizeAllGraph" :: Text) $
fromIntegral (G.size g)
edgeDensity :: Double edgeDensity :: Double
edgeDensity = (sum (Set.map (\node -> (degree node) / links ) ns)) ** 2 edgeDensity = -- trace ("edgeDensity" :: Text) $
where (sum (Set.map (\node -> (degree node) / links ) ns)) ** 2
degree :: Node -> Double where
degree node = fromIntegral (G.deg gr node) degree :: Node -> Double
degree node = -- trace ("degree" :: Text) $
links :: Double fromIntegral (G.deg g node)
links = fromIntegral (2 * (G.size gr))
links :: Double
links = -- trace ("links" :: Text) $
fromIntegral (2 * (G.size g))
subgraph' :: DynGraph gr => Set Node -> gr a b -> gr a b subgraph' :: DynGraph gr => Set Node -> gr a b -> gr a b
subgraph' ns = G.subgraph (Set.toList ns) subgraph' ns = G.subgraph (Set.toList ns)
......
...@@ -29,55 +29,76 @@ type HyperContext a b = Context (Gr () a) b ...@@ -29,55 +29,76 @@ type HyperContext a b = Context (Gr () a) b
-- TODO Later (hypothesis still) -- TODO Later (hypothesis still)
-- type StreamGraph a b = Gr a (Gr () b) -- type StreamGraph a b = Gr a (Gr () b)
toNodes :: HyperGraph a a -> [[Node]] toNodes :: HyperGraph a a -> [[Node]]
toNodes g = map (\n -> hnodes g n) (nodes g) toNodes g = map (hnodes g) (nodes g)
iLouvain :: Eq a => HyperGraph a a -> HyperGraph a a iLouvain :: (Eq a, Show a)
iLouvain g = iLouvain' g (iLouvain' g g) => Int -> HyperGraph a a -> HyperGraph a a
iLouvain 1 g = (iLouvain' g g)
iLouvain 2 g = iLouvain' g (iLouvain 1 g)
iLouvain 3 g = iLouvain' g (iLouvain 2 g)
iLouvain 4 g = iLouvain' g (iLouvain 3 g)
iLouvain 5 g = iLouvain' g (iLouvain 4 g)
iLouvain' :: (Eq a) iLouvain' :: (Eq a, Show a)
=> HyperGraph a a => HyperGraph a a
-> HyperGraph a a -> HyperGraph a a
-> HyperGraph a a -> HyperGraph a a
iLouvain' g0 g = iLouvain'' g $ filter (\n -> elem n (nodes g)) ps iLouvain' g0 g = iLouvain'' g0 $ filter (\n -> elem n (nodes g)) ps
where where
ps = path' g0 -- quick trick to filter path but path of HyperGraph can be different
ps = nodes g0
-- ps = path' g0
iLouvain'' :: HyperGraph a a iLouvain'' :: Show a
=> HyperGraph a a
-> [Node] -> [Node]
-> HyperGraph a a -> HyperGraph a a
iLouvain'' g [ ] = g iLouvain'' g [ ] = g
iLouvain'' g [_] = g iLouvain'' g [_] = g
iLouvain'' g ns = foldl' (\g1 n -> step' g g1 n $ neighbors g1 n) g ns iLouvain'' g ns = foldl' (\g1 n -> step' g g1 n
$ filter (\m -> elem m (nodes g1))
step' :: HyperGraph a b $ neighbors g n
) g ns
-- /!\ Above fixes possible error
-- g1 has holes in network (case below):
-- iLouvain'' g ns = foldl' (\g1 n -> step' g g1 n $ neighbors g1 n) g ns
step' :: Show a
=> HyperGraph a b
-> HyperGraph a a -> HyperGraph a a
-> Node -> Node
-> [Node] -> [Node]
-> HyperGraph a a -> HyperGraph a a
step' g g' n ns = step' g g' n ns = -- trace ("step'" :: Text) $
foldl' (\g1 n' -> case match n g1 of foldl' (\g1 n' -> case match n g1 of
(Nothing, _) -> g1 (Nothing, _) -> g1
(Just _, _ ) -> step g g1 n n') g' ns (Just _, _ ) -> step g g1 n n'
) g' ns
step :: HyperGraph a b step :: Show a
=> HyperGraph a b
-> HyperGraph a a -> HyperGraph a a
-> Node -> Node
-> Node -> Node
-> HyperGraph a a -> HyperGraph a a
step g g' n1 n2 = trace (show n1 :: Text) $ step g g' n1 n2 = -- trace ("step" :: Text) $
if s2 > 0 && s2 > s1 -- if s2 > 0 && s2 >= s1
then mv g' [n1] [n2] if s2 >= s1
else g' then -- trace ("step:mv" :: Text) $
mv g' [n1] [n2]
else -- trace ("step:else" :: Text) $
g'
where where
s1 = imodularity g [n1] s1 = -- trace ("mod1" :: Text) $
s2 = imodularity g [n1,n2] imodularity g [n1]
s2 = -- trace ("mod2" :: Text) $
imodularity g [n1,n2]
------------------------------------------------------------------------ ------------------------------------------------------------------------
hnodes :: HyperGraph a b -> Node -> [Node] hnodes :: HyperGraph a b -> Node -> [Node]
hnodes g n = case match n g of hnodes g n = case match n g of
(Nothing, _) -> [] (Nothing , _) -> []
(Just (p, n, l, s), _) -> n : nodes l (Just (p, n, l, s), _) -> n : nodes l
{- {-
hdeg :: Graph gr => gr a b -> Node -> Maybe Int hdeg :: Graph gr => gr a b -> Node -> Maybe Int
...@@ -86,9 +107,11 @@ hdeg = undefined ...@@ -86,9 +107,11 @@ hdeg = undefined
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- TODO go depth in HyperGraph (modularity at level/depth) -- TODO go depth in HyperGraph (modularity at level/depth)
imodularity :: HyperGraph a b -> [Node] -> Double imodularity :: HyperGraph a b -> [Node] -> Double
imodularity g ns = H.hmodularity g (fromList ns') imodularity g ns = -- trace ("imodul" :: Text) $
where H.hmodularity g
ns' = concat $ map (\n -> hnodes g n) ns $ fromList
$ concat
$ map (hnodes g) ns
------------------------------------------------------------------------ ------------------------------------------------------------------------
toHyperGraph :: Gr () Double -> HyperGraph Double Double toHyperGraph :: Gr () Double -> HyperGraph Double Double
...@@ -131,7 +154,7 @@ spoon = mkGraph ns es ...@@ -131,7 +154,7 @@ spoon = mkGraph ns es
-- Move target type -- Move target type
-- let's start simple: path lenght <= 2 max -- let's start simple: path lenght <= 2 max
mv :: HyperGraph a a mv :: Show a => HyperGraph a a
-> [Node] -> [Node] -> [Node] -> [Node]
-> HyperGraph a a -> HyperGraph a a
mv g [ ] [ ] = g mv g [ ] [ ] = g
...@@ -154,13 +177,14 @@ mv g (x:xs) (y:ys) = panic "mv: path too long" ...@@ -154,13 +177,14 @@ mv g (x:xs) (y:ys) = panic "mv: path too long"
---------------------------- ----------------------------
-- | Start simple (without path) -- | Start simple (without path)
mv' :: HyperGraph a a mv' :: Show a => HyperGraph a a
-> Node -> Node -> Node -> Node
-> HyperGraph a a -> HyperGraph a a
mv' g n1 n2 = (mvMContext c1 c2) & g2 mv' g n1 n2 = -- trace (show (c1,c2) :: Text) $
where (mvMContext c1 c2) & g2
(c1, g1) = match n1 g where
(c2, g2) = match n2 g1 (c1, g1) = match n1 g
(c2, g2) = match n2 g1
mvMContext :: Maybe (HyperContext a a) mvMContext :: Maybe (HyperContext a a)
-> Maybe (HyperContext a a) -> Maybe (HyperContext a a)
...@@ -179,7 +203,7 @@ merge :: (Graph gr, DynGraph gr) ...@@ -179,7 +203,7 @@ merge :: (Graph gr, DynGraph gr)
merge = ufold (&) merge = ufold (&)
------------------------------------------------------------------------ ------------------------------------------------------------------------
test_mv :: Ord a => HyperGraph a a -> Node -> Node -> Bool test_mv :: (Ord a, Show a) => HyperGraph a a -> Node -> Node -> Bool
test_mv g a b = (mv (mv g [a] [b]) [b,a] []) == g test_mv g a b = (mv (mv g [a] [b]) [b,a] []) == g
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- Paths in the Graph to be tested -- Paths in the Graph to be tested
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment