-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
59 add script for generating random pellet distributions (#60)
* functions for sampling pellets over diamond area. * moved pellets utils file. * added comments. * created asteria dir.
- Loading branch information
1 parent
ed03123
commit 1ac5910
Showing
15 changed files
with
291 additions
and
58 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
offchain/tests/admin/consume-asteria.ts → ...in/tests/admin/asteria/consume-asteria.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters