Skip to content

Commit

Permalink
59 add script for generating random pellet distributions (#60)
Browse files Browse the repository at this point in the history
* functions for sampling pellets over diamond area.

* moved pellets utils file.

* added comments.

* created asteria dir.
  • Loading branch information
franciscojoray committed May 13, 2024
1 parent ed03123 commit 1ac5910
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { admin_token } from "../../constants.ts";
import { consumeAsteria } from "../../transactions/admin/consume-asteria.ts";
import { printTxURL } from "../../utils.ts";
import { admin_token } from "../../../constants.ts";
import { consumeAsteria } from "../../../transactions/admin/asteria/consume-asteria.ts";
import { printTxURL } from "../../../utils.ts";

const asteria_tx_hash =
"b661437c389a4bd8db770145980030af22729f9c03be442329c60389e4168c81";
Expand Down
7 changes: 7 additions & 0 deletions offchain/tests/admin/asteria/create-asteria.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { admin_token } from "../../../constants.ts";
import { createAsteria } from "../../../transactions/admin/asteria/create-asteria.ts";
import { printTxURL } from "../../../utils.ts";

const txHash = await createAsteria(admin_token);

printTxURL(txHash);
14 changes: 0 additions & 14 deletions offchain/tests/admin/consume-pellets.ts

This file was deleted.

7 changes: 0 additions & 7 deletions offchain/tests/admin/create-asteria.ts

This file was deleted.

20 changes: 0 additions & 20 deletions offchain/tests/admin/create-pellets.ts

This file was deleted.

6 changes: 0 additions & 6 deletions offchain/tests/admin/pellets.csv

This file was deleted.

15 changes: 15 additions & 0 deletions offchain/tests/admin/pellets/consume-pellets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { admin_token } from "../../../constants.ts";
import { consumePellets } from "../../../transactions/admin/pellets/consume-pellets.ts";
import { printTxURL } from "../../../utils.ts";

const pellets_tx_hash =
"46e84d0411ae0244781f8b135636dadaf8eb129782891888fe8ca8715a272433";
const pellets_tx_indexes = Array.from({ length: 10 }, (_, i) => i + 90);
// const pellets_tx_indexes = [0, 1, 2, 3, 4];

const txHash = await consumePellets(
admin_token,
pellets_tx_hash,
pellets_tx_indexes
);
printTxURL(txHash);
8 changes: 8 additions & 0 deletions offchain/tests/admin/pellets/create-pellets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { admin_token } from "../../../constants.ts";
import { createPellets } from "../../../transactions/admin/pellets/create-pellets.ts";
import { readPelletsCSV } from "./utils.ts";
import { printTxURL } from "../../../utils.ts";

const params = await readPelletsCSV("tests/admin/pellets.csv");
const txHash = await createPellets(admin_token, params);
printTxURL(txHash);
124 changes: 124 additions & 0 deletions offchain/tests/admin/pellets/pellets.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
fuel,x,y
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
36,-4,-17
68,19,-10
90,12,-35
40,-8,3
25,20,16
4 changes: 4 additions & 0 deletions offchain/tests/admin/pellets/sample-pellets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { getDiamondAreaSample, writePelletsCSV } from "./utils.ts";

const pellets = getDiamondAreaSample(20n, 30n, 0.15);
writePelletsCSV(pellets, "tests/admin/pellets/sample1.csv");
122 changes: 122 additions & 0 deletions offchain/tests/admin/pellets/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { parse, stringify } from "jsr:@std/csv";

function getRandomSubarray<T>(arr: Array<T>, size: number) {
const shuffled = arr.slice(0);
let i = arr.length,
temp,
index;
while (i--) {
index = Math.floor((i + 1) * Math.random());
temp = shuffled[index];
shuffled[index] = shuffled[i];
shuffled[i] = temp;
}
return shuffled.slice(0, size);
}

/**
* Returns an array with the coordinates of a diamond (rhombus) with diagonal r.
* Since we measure distances using the "manhattan distance", diamonds are the
* actual "circumferences" (set of points equidistant from the center) in our geometry.
* @param r Diamond diagonal.
*/
function getDiamondCoordinates(
r: bigint
): Array<{ pos_x: bigint; pos_y: bigint }> {
const cs = [];
for (let i = 0n; i < r; i++) {
cs.push({
pos_x: r - i,
pos_y: i,
});
cs.push({
pos_x: -r + i,
pos_y: -i,
});
cs.push({
pos_x: -i,
pos_y: r - i,
});
cs.push({
pos_x: i,
pos_y: -r + i,
});
}
return cs;
}

/**
* Returns an array with a random sample of pellet parameters on the perimeter
* of a diamond with diagonal r.
* @param r Diamond diagonal.
* @param density Density of the sample: equals 1 if every diamond point is taken. Must be in the range 0 - 1, inclusive.
*/
function getDiamondSample(
r: bigint,
density: number
): Array<{ fuel: number; pos_x: bigint; pos_y: bigint }> {
if (density > 1 || density < 0) {
throw Error("Density must be a number between 0 and 1.");
}
const cs = getDiamondCoordinates(r);
const sample_size = Math.floor(cs.length * density);
const sample_cs = getRandomSubarray(cs, sample_size);
const pellets = sample_cs.map((c) => ({
fuel: Math.floor(Math.random() * 60 + 30), //random amount between 30 and 90.
pos_x: c.pos_x,
pos_y: c.pos_y,
}));
return pellets;
}

/**
* Returns an array with a random sample of pellet parameters over the area
* between two diamonds with diagonals inner_r and outer_r respectively.
* @param inner_r Inner diamond diagonal. Must be greater than or equal to 0.
* @param Outer_r Outer diamond diagonal. Must be greater than or equal to inner_r.
* @param density Density of the sample: equals 1 if every diamond point is taken. Must be in the range 0 - 1, inclusive.
*/
function getDiamondAreaSample(
inner_r: bigint,
outer_r: bigint,
density: number
): Array<{ fuel: number; pos_x: bigint; pos_y: bigint }> {
if (inner_r < 0 || inner_r > outer_r) {
throw Error(
"inner_r must be a positive number less than or equal to outer_r"
);
}
const pellets = [];
for (let r = inner_r; r <= outer_r; r++) {
pellets.push(getDiamondSample(r, density));
}
return pellets.flat();
}

function writePelletsCSV(
pellets: Array<{ fuel: number; pos_x: bigint; pos_y: bigint }>,
path: string
) {
const csv = stringify(pellets, {
columns: ["fuel", "pos_x", "pos_y"],
});
Deno.writeTextFileSync(path, csv);
}

async function readPelletsCSV(path: string) {
const text = await Deno.readTextFile(path);
const data = parse(text, {
skipFirstRow: true,
columns: ["fuel", "pos_x", "pos_y"],
});
const params: { fuel: bigint; pos_x: bigint; pos_y: bigint }[] = data.map(
(p) => ({
fuel: BigInt(p.fuel),
pos_x: BigInt(p.pos_x),
pos_y: BigInt(p.pos_y),
})
);
return params;
}

export { getDiamondAreaSample, writePelletsCSV, readPelletsCSV };
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
Constr,
UTxO,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fetchReferenceScript, lucidBase } from "../../utils.ts";
import { AssetClassT } from "../../types.ts";
import { fetchReferenceScript, lucidBase } from "../../../utils.ts";
import { AssetClassT } from "../../../types.ts";

async function consumeAsteria(
admin_token: AssetClassT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
toUnit,
TxHash,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fetchReferenceScript, lucidBase } from "../../utils.ts";
import { AssetClassT, AsteriaDatum, AsteriaDatumT } from "../../types.ts";
import { fetchReferenceScript, lucidBase } from "../../../utils.ts";
import { AssetClassT, AsteriaDatum, AsteriaDatumT } from "../../../types.ts";

async function createAsteria(admin_token: AssetClassT): Promise<TxHash> {
const lucid = await lucidBase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
Constr,
UTxO,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fetchReferenceScript, lucidBase } from "../../utils.ts";
import { AssetClassT } from "../../types.ts";
import { fetchReferenceScript, lucidBase } from "../../../utils.ts";
import { AssetClassT } from "../../../types.ts";

async function consumePellets(
admin_token: AssetClassT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
toUnit,
TxHash,
} from "https://deno.land/x/lucid@0.10.7/mod.ts";
import { fetchReferenceScript, lucidBase } from "../../utils.ts";
import { AssetClassT, PelletDatum, PelletDatumT } from "../../types.ts";
import { fetchReferenceScript, lucidBase } from "../../../utils.ts";
import { AssetClassT, PelletDatum, PelletDatumT } from "../../../types.ts";

async function createPellets(
admin_token: AssetClassT,
Expand Down

0 comments on commit 1ac5910

Please sign in to comment.