Changeset 15130 for stable/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers
- Timestamp:
- 07/06/17 10:09:53 (8 years ago)
- Location:
- stable/HeuristicLab.ExtLibs/HeuristicLab.Igraph
- Files:
-
- 2 edited
- 3 copied
Legend:
- Unmodified
- Added
- Removed
-
stable/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers/Graph.cs
r14234 r15130 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 22 using System; 2 23 using System.Collections.Generic; 3 24 4 namespace HeuristicLab. igraph.Wrappers {5 public class Graph : IDisposable {25 namespace HeuristicLab.IGraph.Wrappers { 26 public sealed class Graph : IDisposable { 6 27 private igraph_t graph; 28 internal igraph_t NativeInstance { get { return graph; } } 29 7 30 public int Vertices { get { return graph.n; } } 31 public bool IsDirected { get { return graph.directed; } } 8 32 9 33 public Graph() : this(0) { } … … 39 63 } 40 64 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; 43 69 } 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); 48 80 } 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 != Vertices || 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(Vertices, 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 != Vertices || 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(Vertices, 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 != Vertices || 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(Vertices, 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; 116 } 117 118 /// <summary> 119 /// Use multi-dimensional scaling to layout vertices. 120 /// A distance matrix can be used to specify the distances between the vertices. 121 /// Otherwise the distances will be calculated by shortest-path-length. 122 /// </summary> 123 /// <remarks> 124 /// For disconnected graphs, dimension must be 2. 125 /// </remarks> 126 /// <param name="dist">The distance matrix to layout the vertices.</param> 127 /// <param name="dim">How many dimensions should be used.</param> 128 /// <returns>The coordinates matrix of the aligned vertices.</returns> 129 public Matrix LayoutWithMds(Matrix dist = null, int dim = 2) { 130 var coords = new Matrix(Vertices, dim); 131 DllImporter.igraph_layout_mds(graph, coords.NativeInstance, dist != null ? dist.NativeInstance : null, dim); 132 return coords; 133 } 134 135 public void BreadthFirstWalk(BreadthFirstHandler handler, int root, DirectedWalkMode mode, bool includeUnreachableFromRoot = false, object tag = null) { 136 igraph_bfshandler_t wrapper = (t, vid, pred, succ, rank, dist, extra) => handler != null && handler(this, vid, pred, succ, rank, dist, tag); 137 DllImporter.igraph_bfs(graph, root, null, (igraph_neimode_t)mode, includeUnreachableFromRoot, null, null, null, null, null, null, null, wrapper, tag); 138 } 139 140 public void DepthFirstWalk(DepthFirstHandler inHandler, DepthFirstHandler outHandler, int root, DirectedWalkMode mode, bool includeUnreachableFromRoot = false, object tag = null) { 141 igraph_dfshandler_t inWrapper = (t, vid, dist, extra) => inHandler != null && inHandler(this, vid, dist, tag); 142 igraph_dfshandler_t outWrapper = (t, vid, dist, extra) => outHandler != null && outHandler(this, vid, dist, tag); 143 DllImporter.igraph_dfs(graph, root, (igraph_neimode_t)mode, includeUnreachableFromRoot, null, null, null, null, inWrapper, outWrapper, tag); 49 144 } 50 145 } -
stable/HeuristicLab.ExtLibs/HeuristicLab.Igraph/0.8.0-pre/HeuristicLab.Igraph-0.8.0-pre/Wrappers/Matrix.cs
r14234 r15130 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 3 21 4 namespace HeuristicLab.igraph.Wrappers { 5 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 6 public class Matrix : IDisposable { 22 using System; 23 24 namespace HeuristicLab.IGraph.Wrappers { 25 public sealed class Matrix : IDisposable { 7 26 igraph_matrix_t matrix; 8 27 internal igraph_matrix_t NativeInstance { get { return matrix; } } … … 11 30 12 31 public Matrix(int nrow, int ncol) { 32 if (nrow < 0 || ncol < 0) throw new ArgumentException("Rows and Columns must be >= 0"); 13 33 matrix = new igraph_matrix_t(); 14 34 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); 40 } 41 public Matrix(double[,] mat) { 42 if (mat == null) throw new ArgumentNullException("mat"); 43 matrix = new igraph_matrix_t(); 44 var nrows = mat.GetLength(0); 45 var ncols = mat.GetLength(1); 46 DllImporter.igraph_matrix_init(matrix, nrows, ncols); 47 var colwise = new double[ncols * nrows]; 48 for (var j = 0; j < ncols; j++) 49 for (var i = 0; i < nrows; i++) 50 colwise[j * nrows + i] = mat[i, j]; 51 DllImporter.igraph_vector_init_copy(matrix.data, colwise); 15 52 } 16 53 ~Matrix() { … … 23 60 matrix = null; 24 61 GC.SuppressFinalize(this); 62 } 63 64 public void Fill(double v) { 65 DllImporter.igraph_matrix_fill(matrix, v); 66 } 67 68 public void Transpose() { 69 DllImporter.igraph_matrix_transpose(matrix); 70 } 71 72 public void Scale(double by) { 73 DllImporter.igraph_matrix_scale(matrix, by); 25 74 } 26 75
Note: See TracChangeset
for help on using the changeset viewer.