Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/08/16 20:48:54 (8 years ago)
Author:
abeham
Message:

#2651: worked on igraph integration, additional layout algorithms, density, page rank

  • added unit tests
Location:
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers/Graph.cs

    r14234 r14244  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
     21
     22using System;
    223using System.Collections.Generic;
    324
    4 namespace HeuristicLab.igraph.Wrappers {
    5   public class Graph : IDisposable {
     25namespace HeuristicLab.IGraph.Wrappers {
     26  public sealed class Graph : IDisposable {
    627    private igraph_t graph;
     28    internal igraph_t NativeInstance { get { return graph; } }
     29
    730    public int Vertices { get { return graph.n; } }
     31    public bool IsDirected { get { return graph.directed; } }
    832
    933    public Graph() : this(0) { }
     
    3963    }
    4064
    41     public double[,] LayoutWithFruchtermanReingold() {
    42       return LayoutWithFruchtermanReingold(500, Math.Sqrt(Vertices));
     65    public double Density() {
     66      double density;
     67      DllImporter.igraph_density(graph, out density, false);
     68      return density;
    4369    }
    44     public double[,] LayoutWithFruchtermanReingold(int niter, double startTemp) {
    45       using (var coords = new Matrix(graph.n, 2)) {
    46         DllImporter.igraph_layout_fruchterman_reingold(graph, coords.NativeInstance, false, niter, startTemp, igraph_layout_grid_t.IGRAPH_LAYOUT_AUTOGRID, null, null, null, null, null);
    47         return coords.ToMatrix();
     70
     71    public Vector PageRank(double damping = 0.85, Vector weights = null) {
     72      var vec = new Vector(Vertices);
     73      var all = new igraph_vs_t();
     74      DllImporter.igraph_vs_all(ref all);
     75      try {
     76        double eigenv = 0;
     77        DllImporter.igraph_pagerank(graph, igraph_pagerank_algo_t.IGRAPH_PAGERANK_ALGO_PRPACK, vec.NativeInstance, out eigenv, all, IsDirected, damping, weights != null ? weights.NativeInstance : null);
     78      } finally {
     79        DllImporter.igraph_vs_destroy(ref all);
    4880      }
     81      return vec;
     82    }
     83
     84    public Matrix LayoutWithFruchtermanReingold(Matrix initialCoords = null) {
     85      return LayoutWithFruchtermanReingold(500, Math.Sqrt(Vertices), initialCoords);
     86    }
     87    public Matrix LayoutWithFruchtermanReingold(int niter, double startTemp, Matrix initialCoords = null) {
     88      if (initialCoords != null && (initialCoords.Rows != graph.n || initialCoords.Columns != 2))
     89        throw new ArgumentException("Initial coordinate matrix does not contain the required number of rows and columns.", "initialCoords");
     90      var coords = initialCoords != null ? new Matrix(initialCoords) : new Matrix(graph.n, 2);
     91      DllImporter.igraph_layout_fruchterman_reingold(graph, coords.NativeInstance, initialCoords != null, niter, startTemp, igraph_layout_grid_t.IGRAPH_LAYOUT_AUTOGRID, null, null, null, null, null);
     92      return coords;
     93    }
     94
     95    public Matrix LayoutWithKamadaKawai(Matrix initialCoords = null) {
     96      return LayoutWithKamadaKawai(50 * Vertices, 0, Vertices, initialCoords);
     97    }
     98    public Matrix LayoutWithKamadaKawai(int maxiter, double epsilon, double kkconst, Matrix initialCoords = null) {
     99      if (initialCoords != null && (initialCoords.Rows != graph.n || initialCoords.Columns != 2))
     100        throw new ArgumentException("Initial coordinate matrix does not contain the required number of rows and columns.", "initialCoords");
     101      var coords = initialCoords != null ? new Matrix(initialCoords) : new Matrix(graph.n, 2);
     102      DllImporter.igraph_layout_kamada_kawai(graph, coords.NativeInstance, initialCoords != null, maxiter, epsilon, kkconst, null, null, null, null, null);
     103      return coords;
     104    }
     105
     106    public Matrix LayoutWithDavidsonHarel(Matrix initialCoords = null) {
     107      var density = Density();
     108      return LayoutWithDavidsonHarel(10, Math.Max(10, (int)Math.Log(Vertices, 2)), 0.75, 1.0, 0.0, density / 10.0, 1.0 - Math.Sqrt(density), 0.2 * (1 - density), initialCoords);
     109    }
     110    public Matrix LayoutWithDavidsonHarel(int maxiter, int fineiter, double cool_fact, double weight_node_dist, double weight_border, double weight_edge_lengths, double weight_edge_crossings, double weight_node_edge_dist, Matrix initialCoords = null) {
     111      if (initialCoords != null && (initialCoords.Rows != graph.n || initialCoords.Columns != 2))
     112        throw new ArgumentException("Initial coordinate matrix does not contain the required number of rows and columns.", "initialCoords");
     113      var coords = initialCoords != null ? new Matrix(initialCoords) : new Matrix(graph.n, 2);
     114      DllImporter.igraph_layout_davidson_harel(graph, coords.NativeInstance, initialCoords != null, maxiter, fineiter, cool_fact, weight_node_dist, weight_border, weight_edge_lengths, weight_edge_crossings, weight_node_edge_dist);
     115      return coords;
    49116    }
    50117  }
  • trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers/Matrix.cs

    r14234 r14244  
    1 using System;
    2 using System.Runtime.InteropServices;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     4 *
     5 * This file is part of HeuristicLab.
     6 *
     7 * HeuristicLab is free software: you can redistribute it and/or modify
     8 * it under the terms of the GNU General Public License as published by
     9 * the Free Software Foundation, either version 3 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * HeuristicLab is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
     19 */
     20#endregion
    321
    4 namespace HeuristicLab.igraph.Wrappers {
    5   [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    6   public class Matrix : IDisposable {
     22using System;
     23
     24namespace HeuristicLab.IGraph.Wrappers {
     25  public sealed class Matrix : IDisposable {
    726    igraph_matrix_t matrix;
    827    internal igraph_matrix_t NativeInstance { get { return matrix; } }
     
    1130
    1231    public Matrix(int nrow, int ncol) {
     32      if (nrow < 0 || ncol < 0) throw new ArgumentException("Rows and Columns must be >= 0");
    1333      matrix = new igraph_matrix_t();
    1434      DllImporter.igraph_matrix_init(matrix, nrow, ncol);
     35    }
     36    public Matrix(Matrix other) {
     37      if (other == null) throw new ArgumentNullException("other");
     38      matrix = new igraph_matrix_t();
     39      DllImporter.igraph_matrix_copy(matrix, other.NativeInstance);
    1540    }
    1641    ~Matrix() {
Note: See TracChangeset for help on using the changeset viewer.