-
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.
functions for sampling pellets over diamond area.
- Loading branch information
1 parent
ed03123
commit 12b0e60
Showing
10 changed files
with
256 additions
and
44 deletions.
There are no files selected for viewing
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/pellet/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 "../../../transactions/admin/pellets/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,7 @@ | ||
import { | ||
getDiamondAreaSample, | ||
writePelletsCSV, | ||
} from "../../../transactions/admin/pellets/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
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,98 @@ | ||
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); | ||
} | ||
|
||
function getDiamondCoordinates(radius: bigint) { | ||
const cs = []; | ||
for (let i = 0n; i < radius; i++) { | ||
cs.push({ | ||
pos_x: radius - i, | ||
pos_y: i, | ||
}); | ||
cs.push({ | ||
pos_x: -radius + i, | ||
pos_y: -i, | ||
}); | ||
cs.push({ | ||
pos_x: -i, | ||
pos_y: radius - i, | ||
}); | ||
cs.push({ | ||
pos_x: i, | ||
pos_y: -radius + i, | ||
}); | ||
} | ||
return cs; | ||
} | ||
|
||
function getDiamondSample(radius: bigint, density: number) { | ||
if (density > 1 || density < 0) { | ||
throw Error("Density must be a number between 0 and 1."); | ||
} | ||
const cs = getDiamondCoordinates(radius); | ||
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), | ||
pos_x: c.pos_x, | ||
pos_y: c.pos_y, | ||
})); | ||
return pellets; | ||
} | ||
|
||
function getDiamondAreaSample( | ||
inner_r: bigint, | ||
outer_r: bigint, | ||
density: number | ||
) { | ||
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 }; |