Commit 575555f5 authored by Kai Zhang's avatar Kai Zhang

add findCommunity

parent d868daf3
module IGraph.Community
( communityLeadingEigenvector
( CommunityOpt(..)
, CommunityMethod(..)
, findCommunity
) where
import Control.Monad
......@@ -10,26 +12,62 @@ import System.IO.Unsafe (unsafePerformIO)
import Data.List
import Data.Ord
import Data.Function (on)
import Data.Default.Class
import IGraph
import IGraph.Mutable (U)
import IGraph.Internal.Data
import IGraph.Internal.Constants
import IGraph.Internal.Community
import IGraph.Internal.Arpack
communityLeadingEigenvector :: LGraph U v e
-> Maybe [Double] -- ^ extract weights
-> Int -- ^ number of steps
-> [[Int]]
communityLeadingEigenvector gr ws step = unsafePerformIO $ do
ap <- igraphArpackNew
vptr <- igraphVectorNew 0
wptr <- case ws of
data CommunityOpt = CommunityOpt
{ _method :: CommunityMethod
, _weights :: Maybe [Double]
, _nIter :: Int -- ^ [LeadingEigenvector] number of iterations, default is 10000
, _nSpins :: Int -- ^ [Spinglass] number of spins, default is 25
, _startTemp :: Double -- ^ [Spinglass] the temperature at the start
, _stopTemp :: Double -- ^ [Spinglass] the algorithm stops at this temperature
, _coolFact :: Double -- ^ [Spinglass] the cooling factor for the simulated annealing
, _gamma :: Double -- ^ [Spinglass] the gamma parameter of the algorithm.
}
data CommunityMethod = LeadingEigenvector
| Spinglass
instance Default CommunityOpt where
def = CommunityOpt
{ _method = LeadingEigenvector
, _weights = Nothing
, _nIter = 10000
, _nSpins = 25
, _startTemp = 1.0
, _stopTemp = 0.01
, _coolFact = 0.99
, _gamma = 1.0
}
findCommunity :: LGraph U v e -> CommunityOpt -> [[Int]]
findCommunity gr opt = unsafePerformIO $ do
result <- igraphVectorNew 0
ws <- case _weights opt of
Just w -> listToVector w
_ -> liftM VectorPtr $ newForeignPtr_ $ castPtr nullPtr
igraphCommunityLeadingEigenvector (_graph gr) wptr nullPtr vptr step ap nullPtr
False nullPtr nullPtr nullPtr nullFunPtr nullPtr
xs <- vectorPtrToList vptr
return $ map f $ groupBy ((==) `on` snd) $ sortBy (comparing snd) $ zip [0..] xs
where
f = fst . unzip
case _method opt of
LeadingEigenvector -> do
ap <- igraphArpackNew
igraphCommunityLeadingEigenvector (_graph gr) ws nullPtr result
(_nIter opt) ap nullPtr False
nullPtr nullPtr nullPtr
nullFunPtr nullPtr
Spinglass ->
igraphCommunitySpinglass (_graph gr) ws nullPtr nullPtr result
nullPtr (_nSpins opt) False (_startTemp opt)
(_stopTemp opt) (_coolFact opt)
IgraphSpincommUpdateConfig (_gamma opt)
IgraphSpincommImpOrig 1.0
liftM ( map (fst . unzip) . groupBy ((==) `on` snd)
. sortBy (comparing snd) . zip [0..] ) $ vectorPtrToList result
......@@ -7,23 +7,43 @@ import Foreign.C.Types
{#import IGraph.Internal.Arpack #}
{#import IGraph.Internal.Graph #}
{#import IGraph.Internal.Data #}
{#import IGraph.Internal.Constants #}
#include "cbits/haskelligraph.c"
{#fun igraph_community_leading_eigenvector as ^ { `IGraphPtr'
, `VectorPtr'
, id `Ptr MatrixPtr'
, `VectorPtr'
, `Int'
, `ArpackOptPtr'
, id `Ptr CDouble'
, `Bool'
, id `Ptr VectorPtr'
, id `Ptr VectorPPtr'
, id `Ptr VectorPtr'
, id `T'
, id `Ptr ()'
} -> `Int' #}
{#fun igraph_community_spinglass as ^
{ `IGraphPtr'
, `VectorPtr'
, id `Ptr CDouble'
, id `Ptr CDouble'
, `VectorPtr'
, id `Ptr VectorPtr'
, `Int'
, `Bool'
, `Double'
, `Double'
, `Double'
, `SpincommUpdate'
, `Double'
, `SpinglassImplementation'
, `Double'
} -> `Int' #}
{#fun igraph_community_leading_eigenvector as ^
{ `IGraphPtr'
, `VectorPtr'
, id `Ptr MatrixPtr'
, `VectorPtr'
, `Int'
, `ArpackOptPtr'
, id `Ptr CDouble'
, `Bool'
, id `Ptr VectorPtr'
, id `Ptr VectorPPtr'
, id `Ptr VectorPtr'
, id `T'
, id `Ptr ()'
} -> `Int' #}
type T = FunPtr ( Ptr VectorPtr
-> CLong
......
......@@ -8,3 +8,7 @@ import Foreign
{#enum igraph_neimode_t as Neimode {underscoreToCase} deriving (Show, Eq) #}
{#enum igraph_edgeorder_type_t as EdgeOrderType {underscoreToCase} deriving (Show, Eq) #}
{#enum igraph_spincomm_update_t as SpincommUpdate {underscoreToCase} deriving (Show, Eq) #}
{#enum igraph_spinglass_implementation_t as SpinglassImplementation {underscoreToCase} deriving (Show, Eq) #}
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