Skip to content

Commit

Permalink
Brushing up docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
FiveMovesAhead committed May 13, 2024
1 parent 420d62d commit 15d567b
Show file tree
Hide file tree
Showing 33 changed files with 383 additions and 229 deletions.
228 changes: 13 additions & 215 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,224 +4,22 @@ This repository contains the implementation of The Innovation Game (TIG).

## Important Links

* [TIG's tech explainer](docs/1_basics.md)
* [Getting Started with Innovating](docs/1_basics.md)
* [Challenge descriptions](tig-challenges/docs/knapsack.md)
* [TIG's tech explainer](docs/tech/1_basics.md)
* [Getting Started with Innovating](docs/guides/innovating.md)
* [Challenge descriptions](docs/challenges/satisfiability.md)

## Repo Contents
### tig-algorithms

A Rust crate that hosts algorithm submissions made by Innovators in TIG.

Submissions are committed to their own branch with name:

`<challenge_name>\<algorithm_name>`

Submissions only get merged to the main branch after earning sufficient merge points.

WASM blobs for an algorithm are stored in the `wasm` subfolder and can be downloaded via:

`https://raw.githubusercontent.com/tig-foundation/tig-monorepo/<branch_name>/tig-algorithms/wasm/<branch_name>.wasm`

### tig-api

A Rust crate for making requests to TIG's API.

Developers must either enable feature `request` (uses `reqwest`) or `request-js` (uses `web-sys`)

### tig-benchmarker

A Rust crate that implements a Benchmarker for TIG that can run in the browser.

### tig-challenges

A Rust crate that contains the implementation of TIG's challenges (computational problems adapted for proof-of-work).

### tig-protocol

A Rust crate that contains the implementation of TIG's core protocol logic.

### tig-structs

A Rust crate that contains the definitions of structs used throughout TIG.

### tig-token

Solidity contract for TIG's ERC20 token that is deployed on Ethereum L2 Base chain.

### tig-utils

A Rust crate that contains utility functions used throughout TIG.

### tig-wasm

A Rust crate for wrapping algorithm submissions for compilation into WASM with an exported `entry_point`.

### tig-worker

A Rust crate for verifying and computing solutions.

Solutions are computed by executing an algorithm in a WASM virtual machine ([TIG's fork of wasmi](https://github.com/tig-foundation/wasmi)).

## Getting Started with Innovating

### Setting up Private Fork

Innovators will want to create a private fork so that they can test that their algorithm can be successfully compiled into WASM by the CI.

1. Create private repository on GitHub
2. Create empty git repository on your local machine
```
mkdir tig-monorepo
cd tig-monorepo
git init
```
3. Setup remotes with origin pointed to your private repository
```
git remote add origin <your private repo>
git remote add public https://github.com/tig-foundation/tig-monorepo.git
```
4. Pulling `blank_slate` from TIG public repository (branch with no algorithms)
```
git fetch public
git checkout -b blank_slate
git pull public blank_slate
```
5. Push to your private repository
```
git push origin blank_slate
```
### Checking out Existing Algorithms
Every algorithm has its own `<branch>` with name `<challenge_name>/<algorithm_name>`.
Only algorithms that are successfully compiled into WASM have their branch pushed to this public repository.
Each algorithm branch will have 2 files:
1. Rust code @ `tig-algorithms/src/<branch>.rs`
2. Wasm blob @ `tig-algorithms/wasm/<branch>.wasm`
To pull an existing algorithm from TIG public repository, run the following command:
```
git fetch public
git pull public <branch>
```
### Developing Your Algorithm
1. Pick a challenge (`<challenge_name>`) to develop an algorithm for
2. Make a copy of an existing algorithm's rust code or `tig-algorithms/<challenge_name>/template.rs`
3. Rename the file with your own `<algorithm_name>`
4. Edit `tig-algorithms/<challenge_name>/mod.rs` to export your algorithm and test it:
```
pub mod <algorithm_name>;
#[cfg(test)]
mod tests {
use super::*;
use tig_challenges::{<challenge_name>::*, *};
#[test]
fn test_<algorithm_name>() {
let difficulty = Difficulty {
// Uncomment the relevant fields.
// -- satisfiability --
// num_variables: 50,
// clauses_to_variables_percent: 300,
// -- vehicle_routing --
// num_nodes: 40,
// better_than_baseline: 250,
// -- knapsack --
// num_items: 50,
// better_than_baseline: 10,
};
let seed = 0;
let challenge = Challenge::generate_instance(seed, &difficulty).unwrap();
<algorithm_name>::solve_challenge(&challenge).unwrap();
}
}
```
5. Check that your algorithm compiles & runs:
```
cargo test -p tig-algorithms
```
Notes:
* Do not include tests in your algorithm file. TIG will reject your algorithm submission.
* Only your algorithm's rust code gets submitted. You should not be adding dependencies to `tig-algorithms` as they will not be available when TIG compiles your algorithm
### Locally Compiling Your Algorithm into WASM
These steps replicate what TIG's CI does (`.github/workflows/build_algorithm.yml`):
1. Set environment variables to match the algorithm you are compiling:
```
export CHALLENGE=<challenge_name>
export ALGORITHM=<algorithm_name>
```
2. Compile your algorithm
```
cargo build -p tig-wasm --target wasm32-wasi --release --features entry-point
```
3. Optimise the WASM and save it into `tig-algorithms/wasm`:
```
mkdir -p tig-algorithms/wasm/${CHALLENGE}
wasm-opt target/wasm32-wasi/release/tig_wasm.wasm -o tig-algorithms/wasm/${CHALLENGE}/${ALGORITHM}.wasm -O2 --remove-imports
```
### Testing Performance of Algorithms
Performance testing is done by `tig-worker` in a sandboxed WASM Virtual Machine.
**IMPORTANT**: You can compile / test existing algorithms as binary executables, but be sure to throughly vet the code beforehand for malicious routines!
1. Pull an existing algorithm or compile your own algorithm to WASM
2. Set environment variables to match the algorithm you are testing:
```
export CHALLENGE=<challenge_name>
export ALGORITHM=<algorithm_name>
```
3. Pick a difficulty & create `settings.json`:
```
{
"block_id": "",
"algorithm_id": "",
"challenge_id": "",
"player_id": "",
"difficulty": [50, 300]
}
```
4. Test the algorithm:
```
cargo run -p tig-worker --release -- settings.json tig-algorithms/wasm/${CHALLENGE}/${ALGORITHM}.wasm
```
Notes:
* You can query the latest difficulty ranges via TIG's API:
```
query https://api.tig.foundation/play/get-block for <block_id>
query https://api.tig.foundation/play/get-challenges?block_id=<block_id> for qualifier_difficulties
```
### Checking CI Successfully Compiles Your Algorithm
TIG pushes all algorithms to their own branch which triggers the CI (`.github/workflows/build_algorithm.yml`).
To trigger the CI on your private repo, your branch just needs to have a particular name:
```
git checkout -b <challenge_name>/<algorithm_name>
git push origin <challenge_name>/<algorithm_name>
```
### Making Your Submission
You will need to burn 0.001 ETH to make a submission. Visit https://play.tig.foundation/innovator and follow the instructions.
* [tig-algorithms](./tig-algorithms/README.md) - A Rust crate that hosts algorithm submissions made by Innovators in TIG
* [tig-api](./tig-api/README.md) - A Rust crate for making requests to TIG's API
* [tig-benchmarker](./tig-benchmarker/README.md) - A Rust crate that implements a Benchmarker for TIG that can run in the browser
* [tig-challenges](./tig-challenges/README.md) - A Rust crate that contains the implementation of TIG's challenges (computational problems adapted for proof-of-work)
* [tig-protocol](./tig-protocl/README.md) - A Rust crate that contains the implementation of TIG's core protocol logic.
* [tig-structs](./tig-structs/README.md) - A Rust crate that contains the definitions of structs used throughout TIG
* [tig-token](./tig-token/README.md) - Solidity contract for TIG's ERC20 token that is deployed on Ethereum L2 Base chain
* [tig-utils](./tig-utils/README.md) - A Rust crate that contains utility functions used throughout TIG
* [tig-wasm](./tig-benchmarker/README.md) - A Rust crate for wrapping algorithm submissions for compilation into WASM with an exported `entry_point`
* [tig-worker](./tig-worker/README.md) - A Rust crate for verifying and computing solutions

## License

Expand Down
Binary file added assets/circuit.jfif
Binary file not shown.
Binary file added assets/gene_clustering.jfif
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

[The area of Knapsack problems is one of the most active research areas of combinatorial optimization](https://en.wikipedia.org/wiki/Knapsack_problem). The problem is to maximise the value of items placed in a knapsack given the constraint that the total weight of items cannot exceed some limit.

Figure 1 (below) illustrates a simple example. In this case, the weight of all items in the knapsack cannot exceed 15kg. Here, the solution is that the maximum value of items in the knapsack is $15, which is attained by including all items apart from the green item.

<img src="../assets/knapsack_example.png" alt="Knapsack Example" width="100%"/>
<figcaption>Figure 1. Example of Knapsack Problem</figcaption>
<br/>

# Example

For our challenge, we use a version of the knapsack problem with configurable difficulty, where the following two parameters can be adjusted in order to vary the difficulty of the challenge:
Expand Down Expand Up @@ -53,7 +47,7 @@ The Knapsack problems have a wide variety of practical applications. The [use of

Although originally studied in the context of logistics, Knapsack problems appear regularly in diverse areas of science and technology. For example, in gene expression data, there are usually thousands of genes, but only a subset of them are informative for a specific problem. The Knapsack Problem can be used to select a subset of genes (items) that maximises the total information (value) without exceeding the limit of the number of genes that can be included in the analysis (weight limit).

<img src="../assets/gene_clustering.jfif" alt="Gene Clustering" width="100%"/>
<img src="../../assets/gene_clustering.jfif" alt="Gene Clustering" width="100%"/>


<figcaption>Figure 2: <a href="https://openres.ersjournals.com/content/4/4/00031-2018" target="_blank">Microarray clustering of differentially expressed genes in blood</a>. Genes are clustered in rows, with red indicating high expression, yellow intermediate expression and blue low expression. The Knapsack problem is <a href="https://www.sciencedirect.com/science/article/abs/pii/S0305054821003877" target="_blank">used to analyse</a> gene expression clustering.</figcaption>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ SAT has a vast range of applications in science and industry in fields including
SAT is used in computational biology to solve the "cell formation problem" of [organising a plant into cells](https://www.sciencedirect.com/science/article/abs/pii/S0957417412006173).
SAT is also heavily utilised in [electronic circuit design](https://dl.acm.org/doi/abs/10.1145/337292.337611).

<img src="../assets/circuit.jfif" alt="Application of SAT" width="100%"/>
<img src="../../assets/circuit.jfif" alt="Application of SAT" width="100%"/>

<figcaption>Figure 1: Chips made possible by electronic circuit design.</figcaption>
<br/>
File renamed without changes.
114 changes: 114 additions & 0 deletions docs/guides/innovating.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Getting Started with Innovating

## Setting up Private Fork

Innovators will want to create a private fork so that they can test that their algorithm can be successfully compiled into WASM by the CI.

1. Create private repository on GitHub
2. Create empty git repository on your local machine
```
mkdir tig-monorepo
cd tig-monorepo
git init
```
3. Setup remotes with origin pointed to your private repository
```
git remote add origin <your private repo>
git remote add public https://github.com/tig-foundation/tig-monorepo.git
```
4. Pulling `blank_slate` from TIG public repository (branch with no algorithms)
```
git fetch public
git checkout -b blank_slate
git pull public blank_slate
```
5. Push to your private repository
```
git push origin blank_slate
```
## Checking out Existing Algorithms
Every algorithm has its own `<branch>` with name `<challenge_name>/<algorithm_name>`.
Only algorithms that are successfully compiled into WASM have their branch pushed to this public repository.
Each algorithm branch will have 2 files:
1. Rust code @ `tig-algorithms/src/<branch>.rs`
2. Wasm blob @ `tig-algorithms/wasm/<branch>.wasm`
To pull an existing algorithm from TIG public repository, run the following command:
```
git fetch public
git pull public <branch>
```
## Developing Your Algorithm
1. Pick a challenge (`<challenge_name>`) to develop an algorithm for
2. Make a copy of an existing algorithm's rust code or `tig-algorithms/<challenge_name>/template.rs`
3. Rename the file with your own `<algorithm_name>`
4. Edit `tig-algorithms/<challenge_name>/mod.rs` to export your algorithm and test it:
```
pub mod <algorithm_name>;
#[cfg(test)]
mod tests {
use super::*;
use tig_challenges::{<challenge_name>::*, *};
#[test]
fn test_<algorithm_name>() {
let difficulty = Difficulty {
// Uncomment the relevant fields.
// -- satisfiability --
// num_variables: 50,
// clauses_to_variables_percent: 300,
// -- vehicle_routing --
// num_nodes: 40,
// better_than_baseline: 250,
// -- knapsack --
// num_items: 50,
// better_than_baseline: 10,
};
let seed = 0;
let challenge = Challenge::generate_instance(seed, &difficulty).unwrap();
<algorithm_name>::solve_challenge(&challenge).unwrap();
}
}
```
5. Check that your algorithm compiles & runs:
```
cargo test -p tig-algorithms
```
Notes:
* Do not include tests in your algorithm file. TIG will reject your algorithm submission.
* Only your algorithm's rust code gets submitted. You should not be adding dependencies to `tig-algorithms` as they will not be available when TIG compiles your algorithm
## Locally Compiling Your Algorithm into WASM
See the [README](../../tig-wasm/README.md) for `tig-wasm`
## Testing Performance of Algorithms
See the [README](../../tig-worker/README.md) for `tig-worker`
## Checking CI Successfully Compiles Your Algorithm
TIG pushes all algorithms to their own branch which triggers the CI (`.github/workflows/build_algorithm.yml`).
To trigger the CI on your private repo, your branch just needs to have a particular name:
```
git checkout -b <challenge_name>/<algorithm_name>
git push origin <challenge_name>/<algorithm_name>
```
## Making Your Submission
You will need to burn 0.001 ETH to make a submission. Visit https://play.tig.foundation/innovator and follow the instructions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tig-algorithms/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "tig-algorithms"
version = "0.1.0"
readme = "README.md"
authors.workspace = true
repository.workspace = true
edition.workspace = true
Expand Down
Loading

0 comments on commit 15d567b

Please sign in to comment.