Skip to content

Commit

Permalink
cleanup and update versions
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed May 25, 2021
1 parent d93e0a5 commit 1de1de8
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 44 deletions.
10 changes: 5 additions & 5 deletions README.md
@@ -1,6 +1,6 @@
# Kaliningraph

[![Kotlin 1.5.0](https://img.shields.io/badge/Kotlin-1.5.0-blue.svg?style=flat&logo=kotlin)](http://kotlinlang.org)
[![Kotlin 1.5.10](https://img.shields.io/badge/Kotlin-1.5.10-blue.svg?style=flat&logo=kotlin)](http://kotlinlang.org)
[![](https://jitpack.io/v/breandan/kaliningraph.svg)](https://jitpack.io/#breandan/kaliningraph)
[![CI](https://github.com/breandan/kaliningraph/workflows/CI/badge.svg)](https://github.com/breandan/kaliningraph/actions)

Expand All @@ -20,7 +20,7 @@ repositories {
}

dependencies {
implementation("com.github.breandan:kaliningraph:0.1.6")
implementation("com.github.breandan:kaliningraph:0.1.7")
}
```

Expand All @@ -38,7 +38,7 @@ dependencies {
<dependency>
<groupId>com.github.breandan</groupId>
<artifactId>kaliningraph</artifactId>
<version>0.1.6</version>
<version>0.1.7</version>
</dependency>
</project>
```
Expand All @@ -49,7 +49,7 @@ To access notebook support, use the following line magic:

```
@file:Repository("https://jitpack.io")
@file:DependsOn("com.github.breandan:kaliningraph:0.1.6")
@file:DependsOn("com.github.breandan:kaliningraph:0.1.7")
```

For more information, explore our tutorials:
Expand All @@ -67,7 +67,7 @@ What are neighbors? Neighbors are a graph.

## Getting Started

Run [the demo](src/main/kotlin/edu/mcgill/kaliningraph/HelloKaliningraph.kt) via `./gradlew HelloKaliningraph` to get started.
Run [the demo](src/test/kotlin/edu/mcgill/kaliningraph/HelloKaliningraph.kt) via `./gradlew HelloKaliningraph` to get started.

## Usage

Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Expand Up @@ -4,13 +4,13 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`maven-publish`
kotlin("jvm") version "1.5.0"
kotlin("jupyter.api") version "0.10.0-17"
kotlin("jvm") version "1.5.10"
kotlin("jupyter.api") version "0.10.0-42"
id("com.github.ben-manes.versions") version "0.38.0"
}

group = "com.github.breandan"
version = "0.1.6"
version = "0.1.7"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion notebooks/Hello Kaliningraph.ipynb
Expand Up @@ -9,7 +9,7 @@
"outputs": [],
"source": [
"@file:Repository(\"https://jitpack.io\")\n",
"@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.6\")"
"@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.7\")"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion notebooks/Program Graphs.ipynb
Expand Up @@ -7,7 +7,7 @@
"outputs": [],
"source": [
"@file:Repository(\"https://jitpack.io\")\n",
"@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.6\")"
"@file:DependsOn(\"com.github.breandan:kaliningraph:0.1.7\")"
]
},
{
Expand Down
11 changes: 1 addition & 10 deletions src/main/kotlin/edu/mcgill/kaliningraph/Graph.kt
Expand Up @@ -2,12 +2,9 @@ package edu.mcgill.kaliningraph

import edu.mcgill.kaliningraph.matrix.*
import edu.mcgill.kaliningraph.typefamily.*
import guru.nidi.graphviz.attribute.Label
import guru.nidi.graphviz.model.*
import org.ejml.kotlin.*
import kotlin.math.*
import kotlin.math.sqrt
import kotlin.random.Random
import kotlin.reflect.KProperty

abstract class Graph<G, E, V>(override val vertices: Set<V> = setOf()):
IGraph<G, E, V>,
Expand Down Expand Up @@ -149,8 +146,6 @@ abstract class Graph<G, E, V>(override val vertices: Set<V> = setOf()):
abstract class Edge<G, E, V>(override val source: V, override val target: V): IEdge<G, E, V>
where G: Graph<G, E, V>, E: Edge<G, E, V>, V: Vertex<G, E, V> {
override val graph by lazy { target.graph }
operator fun component1() = source
operator fun component2() = target
}

abstract class Vertex<G, E, V>(override val id: String): IVertex<G, E, V>
Expand All @@ -160,10 +155,6 @@ abstract class Vertex<G, E, V>(override val id: String): IVertex<G, E, V>
override val neighbors by lazy { outgoing.map { it.target }.toSet() }
override val outdegree by lazy { neighbors.size }

override fun encode(): DoubleArray = id.vectorize()

override operator fun getValue(a: Any?, prop: KProperty<*>): V = V(prop.name)
override fun render(): MutableNode = Factory.mutNode(id).add(Label.of(toString()))
override fun equals(other: Any?) =
(other as? Vertex<*, *, *>)?.encode().contentEquals(encode())
override fun hashCode() = id.hashCode()
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/edu/mcgill/kaliningraph/LabeledGraph.kt
Expand Up @@ -53,6 +53,11 @@ open class LabeledGraph(override val vertices: Set<LGVertex> = setOf()):
companion object: LabeledGraph() {
operator fun invoke(builder: LGBuilder.() -> Unit) =
LGBuilder().also { it.builder() }.mutGraph

operator fun invoke(graph: String) =
graph.split(" ").fold(G()) { acc, it ->
acc + G(*it.toList().zipWithNext().toTypedArray())
}
}

var accumuator = mutableSetOf<String>()
Expand All @@ -63,7 +68,7 @@ open class LabeledGraph(override val vertices: Set<LGVertex> = setOf()):
fun rewrite(substitution: Pair<String, String>) =
randomWalk().take(200).toList().joinToString("")
.replace(substitution.first, substitution.second)
.let { LabeledGraph.G(it) }
.let { LabeledGraph(it) }

fun propagate() {
val (previousStates, unoccupied) = vertices.partition { it.occupied }
Expand Down Expand Up @@ -96,8 +101,7 @@ open class LabeledEdge(
override val source: LGVertex,
override val target: LGVertex,
val label: String? = null
):
Edge<LabeledGraph, LabeledEdge, LGVertex>(source, target) {
): Edge<LabeledGraph, LabeledEdge, LGVertex>(source, target) {
constructor(source: LGVertex, target: LGVertex): this(source, target, null)

override fun render() =
Expand Down
8 changes: 3 additions & 5 deletions src/main/kotlin/edu/mcgill/kaliningraph/Utils.kt
Expand Up @@ -63,7 +63,7 @@ fun BMat.show(filename: String = "temp") = matToImg().let { data ->
}
}.show()

val browserCmd = System.getProperty("os.name").toLowerCase().let { os ->
val browserCmd = System.getProperty("os.name").lowercase().let { os ->
when {
"win" in os -> "rundll32 url.dll,FileProtocolHandler"
"mac" in os -> "open"
Expand Down Expand Up @@ -91,8 +91,6 @@ fun BMat.matToImg(f: Int = 20) = toEJMLSparse().matToImg(f)

fun randomString() = UUID.randomUUID().toString().take(5)

private operator fun <K, V> Pair<K, V>.component2(): V = second
private operator fun <K, V> Pair<K, V>.component1(): K = first
operator fun MutableNode.minus(target: LinkTarget): Link = addLink(target).links().last()!!

fun randomMatrix(rows: Int, cols: Int = rows, rand: () -> Double = { Random.Default.nextDouble() }) =
Expand All @@ -101,7 +99,7 @@ fun randomMatrix(rows: Int, cols: Int = rows, rand: () -> Double = { Random.Defa
fun randomVector(size: Int, rand: () -> Double = { Random.Default.nextDouble() }) =
Array(size) { rand() }.toDoubleArray()

fun Array<DoubleArray>.toEJMLSparse() = SpsMat(size, this[0].size, sumBy { it.count { it == 0.0 } })
fun Array<DoubleArray>.toEJMLSparse() = SpsMat(size, this[0].size, sumOf { it.count { it == 0.0 } })
.also { s -> for (i in indices) for (j in this[0].indices) this[i][j].let { if (0.0 < it) s[i, j] = it } }

fun Array<DoubleArray>.toEJMLDense() = DMatrixRMaj(this)
Expand All @@ -115,7 +113,7 @@ fun <T> powBench(constructor: T, matmul: (T, T) -> T): Long =
measureTimeMillis { constructor.power(100, matmul) }

fun <T> T.power(exp: Int, matmul: (T, T) -> T) =
(0..exp).fold(this) { acc, i -> matmul(acc, this) }
generateSequence(this) { matmul(it, this) }.take(exp)

const val DEFAULT_FEATURE_LEN = 20
fun String.vectorize(len: Int = DEFAULT_FEATURE_LEN) =
Expand Down
32 changes: 18 additions & 14 deletions src/main/kotlin/edu/mcgill/kaliningraph/typefamily/IGraph.kt
Expand Up @@ -49,10 +49,6 @@ interface IGF<G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V>> {
}
)

// TODO: generify, only works for labeled graphs
fun G(graph: String): G =
graph.split(" ").fold(G()) { acc, it -> acc + G(it.toCharArray().toList()) }

// Gafter's gadget! http://gafter.blogspot.com/2006/12/super-type-tokens.html
private fun gev(): Array<Class<*>> =
(generateSequence(javaClass) { it.superclass as Class<IGF<G, E, V>> }
Expand Down Expand Up @@ -83,11 +79,16 @@ interface IGraph<G, E, V>: IGF<G, E, V>, Set<V>, (V) -> Set<V>
* - Pros: Useful for describing many algebraic path problems
* - Cons: Esoteric API / unsuitable as an abstract interface
*
* Algebraic perspective : https://github.com/snowleopard/alga-paper/releases/download/final/algebraic-graphs.pdf
* Type-family perspective : https://www.cs.cornell.edu/~ross/publications/shapes/shapes-pldi14-tr.pdf#page=3
* Inductive perspective : https://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf
* Mathematical perspective : https://doi.org/10.1007/978-0-387-75450-5
* Semiring perspective : http://stedolan.net/research/semirings.pdf
* Algebraic perspective : https://github.com/snowleopard/alga-paper/releases/download/final/algebraic-graphs.pdf
* : https://arxiv.org/pdf/1909.04881.pdf
* Type-family perspective : https://www.cs.cornell.edu/~ross/publications/shapes/shapes-pldi14-tr.pdf#page=3
* : https://www.cs.cornell.edu/andru/papers/familia/familia.pdf#page=8
* Inductive perspective : https://web.engr.oregonstate.edu/~erwig/papers/InductiveGraphs_JFP01.pdf
* : https://doi.org/10.1145/258949.258955
* : https://www.cs.utexas.edu/~wcook/Drafts/2012/graphs.pdf
* Semiring perspective : http://stedolan.net/research/semirings.pdf
* : https://doi.org/10.1007/978-0-387-75450-5
* : https://doi.org/10.2200/S00245ED1V01Y201001CNT003
*/

where G: IGraph<G, E, V>, E: IEdge<G, E, V>, V: IVertex<G, E, V> {
Expand Down Expand Up @@ -134,6 +135,9 @@ interface IEdge<G, E, V>: IGF<G, E, V>
val source: V
val target: V

operator fun component1() = source
operator fun component2() = target

fun render(): Link = (source.render() - target.render()).add(Label.of(""))
}

Expand All @@ -146,8 +150,8 @@ interface IVertex<G, E, V>: IGF<G, E, V>, Encodable
val outgoing: Set<E> get() = edgeMap(this as V).toSet()
val edgeMap: (V) -> Set<E> // Make a self-loop by passing this

open val neighbors get() = outgoing.map { it.target }.toSet()
open val outdegree get() = neighbors.size
val neighbors get() = outgoing.map { it.target }.toSet()
val outdegree get() = neighbors.size

// tailrec prohibited on open members? may be possible with deep recursion
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-deep-recursive-function/
Expand Down Expand Up @@ -181,6 +185,6 @@ infix fun Collection<Any>.allAre(that: Any) = all { it isA that }
infix fun Collection<Any>.anyAre(that: Any) = any { it isA that }

// https://github.com/amodeus-science/amod
//abstract class Map : IGraph<Map, Road, City>
//abstract class Road : IEdge<Map, Road, City>
//abstract class City : IVertex<Map, Road, City>
abstract class TMap : IGraph<TMap, TRoad, TCity>
abstract class TRoad : IEdge<TMap, TRoad, TCity>
abstract class TCity : IVertex<TMap, TRoad, TCity>
Expand Up @@ -20,5 +20,5 @@ class LabeledGraphTest {
)

@Test
fun testStringConstructor() = assertEquals(graph, LabeledGraph.G("abcde ace"))
fun testStringConstructor() = assertEquals(graph, LabeledGraph("abcde ace"))
}
2 changes: 1 addition & 1 deletion src/test/kotlin/edu/mcgill/kaliningraph/Rewriter.kt
Expand Up @@ -8,7 +8,7 @@ import kotlin.random.Random
@ExperimentalStdlibApi
fun main() {
animate(
LabeledGraph.G("abcdecfghia").also { println(it) }
LabeledGraph("abcdecfghia").also { println(it) }
) { _: Document, it: KeyboardEvent, graphs: MutableList<LabeledGraph> ->
when {
"Left" in it.key -> {
Expand Down

0 comments on commit 1de1de8

Please sign in to comment.