Skip to main content

Methods for building and simulating constraint hypergraphs.

Project description

Static Badge Read the Docs Static Badge GitHub Release GitHub last commit

ConstraintHg

This repository enables usage of hypergraphs to define and execute system models. It is not a rigorous data storage solution. Do not use this as a database. Note that this repo is under active development (no official release yet), therefore changes may occur rapidly. Fork the repository before using it.

Introduction

Hypergraphs are normal graphs but without the constraint that edges must only link between two nodes. Because of this expanded generality, hypergraphs can be used to model more complex relationships. For instance, the relationship A + B = C is a multinodal relationship between three nodes, A, B, and C. You can think of all three nodes being linked by a 2D hyperedge, so that to move along that hyperedge you need at least two of three nodes.

An constraint hypergraph is a hypergraph where the relationships are constraints that can be solved for by some execution engine, generally via API calls. These constraints reveal the behavior of the system. The goal is for the hypergraph to be platform agnostic, while API calls allow for edges to be processed on any available software.

Processing a series of nodes and edges (a "route") is what constitutes a simulation, so one of the uses of an constraint hypergraph is enabling high-level simulation ability from any possible entry point in a system model.

Getting started

Note that this demo is found in demos/demo_basic.py Let's build a basic action hypergraph of the following equations:

  • $A + B = C$
  • $A = -D$
  • $B = -E$
  • $D + E = F$
  • $F = -C$

First, import the classes.

from constrainthg.hypergraph import Hypergraph
import constrainthg.relations as R

A hypergraph consists of edges that map between a set of nodes to a single node. We provide the mapping by defining a constraint function (many of which are already defined in the relationships module). The two relationships defined in the governing equations are addition and negation. Using the typical syntax, we refer to the functions defined in relationships with R.<name>', in this case R.RsumandR.Rnegate`. To make the hypergraph we'll need to compose the 5 edges (equations) given above.

hg = Hypergraph()
hg.addEdge(['A', 'B'], C, R.Rsum)
hg.addEdge('A', 'D', R.Rnegate)
hg.addEdge('B', 'E', R.Rnegate)
hg.addEdge(['D', 'E'], 'F', R.Rsum)
hg.addEdge('F', 'C', R.Rnegate)

We can verify that the hypergraph was made correctly by tracing all possible paths for generating C using the printPaths function.

print(hg.printPaths('C'))

This should give us the following output. Hyperedges are indicated with a , with the last source separated from other edges with a .

└──C, cost=1
   ├◯─A, cost=0
   ├●─B, cost=0
   └──F, cost=3
      ├◯─D, cost=1
      │  └──A, cost=0
      └●─E, cost=1
         └──B, cost=0

Compute the value of $C$ by picking a set of source nodes (inputs), such as $A$ and $B$ or $A$ and $E$. Set values for the inputs and the solver will automatically calulate an optimized route to simulate $C$.

print("**Inputs A and E**")
hg.solve('C', {'A':3, 'E':-7}, toPrint=True)
print("**Inputs A and B**")
hg.solve('C', {'A':3, 'B':7}, toPrint=True)

The output of the above should be:

**Inputs A and E**
└──C= 10, cost=3
   └──F= -10, cost=2
      ├──D= -3, cost=1
      │  └──A= 3, cost=0
      └──E= -7, cost=0

**Inputs A and B**
└──C= 10, cost=1
   ├──A= 3, cost=0
   └──B= 7, cost=0

Check out the demos directory for more examples.

Licensing and Usage

Author: John Morris
Organization: PLM Center at Clemson University
Contact: Reach out to my GitHub profile (jmorris335)
Usage: An official release will likely be provided under the CC BY-NC-SA 4.0 license, but for now all rights are reserved. For usage, please reach out to the author directly.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

constrainthg-0.1.0a0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

constrainthg-0.1.0a0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file constrainthg-0.1.0a0.tar.gz.

File metadata

  • Download URL: constrainthg-0.1.0a0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for constrainthg-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 f9d40dcec9b00e93e563876316f847df38a6c0e3831a8637d75727acee5710a1
MD5 e8de7e5e291e93b8035040f8469c79b4
BLAKE2b-256 4eb543619b42c3c29b6a490e6cc053ae340f6b2d989c9a843f4a9133c3a3c6a3

See more details on using hashes here.

File details

Details for the file constrainthg-0.1.0a0-py3-none-any.whl.

File metadata

File hashes

Hashes for constrainthg-0.1.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 d483b5fd2d47d59079db07b14c638131ccadc5aa52bf7d6d7aa1961a13fcc020
MD5 4144eaafd5ef67b115586d4e63e99fd3
BLAKE2b-256 c5e1e41c8977212fb2a7e2fe7438a793c74af98580b54f67af2d9b57f792d3d8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page