Skip to content

Commit e903d4b

Browse files
adding some stuff
1 parent d98ad54 commit e903d4b

File tree

6 files changed

+140
-14
lines changed

6 files changed

+140
-14
lines changed

build.gradle

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ plugins {
1313
id 'org.spongepowered.mixin' version '0.7.+'
1414
}
1515

16+
import java.nio.file.Files
17+
import java.nio.file.Paths
18+
19+
def loadEnv() {
20+
def envFile = rootProject.file(".env")
21+
if (envFile.exists()) {
22+
envFile.eachLine { line ->
23+
def matcher = line =~ /^\s*([\w\.]+)\s*=\s*(.*)\s*$/
24+
if (matcher.matches()) {
25+
def key = matcher[0][1]
26+
def value = matcher[0][2]
27+
System.setProperty(key, value)
28+
}
29+
}
30+
}
31+
}
32+
loadEnv()
1633
version = mod_version
1734
group = mod_group_id
1835

@@ -65,7 +82,6 @@ minecraft {
6582
gameTestServer {
6683
property 'forge.enabledGameTestNamespaces', mod_id
6784
}
68-
6985
data {
7086
// example of overriding the workingDirectory set in configureEach above
7187
workingDirectory project.file('run-data')
@@ -136,10 +152,8 @@ dependencies {
136152
annotationProcessor "org.spongepowered:mixin:${mixin_version}:processor"
137153

138154
//worldgen tool
139-
//worldgen-devtools:4rikEHY9
140-
//runtimeOnly fg.deobf("maven.modrinth:worldgen-devtools:1.0.0-bata.2+1.20.5") -> dependent on minecraft too high use it for 1.21.1
141-
//runtimeOnly fg.deobf("maven.modrinth:connector:1.0.0-beta.46+1.20.1")
142-
//runtimeOnly fg.deobf("maven.modrinth:forgified-fabric-api:0.92.2+1.11.12+1.20.1")
155+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
156+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
143157

144158

145159
}
@@ -188,6 +202,9 @@ tasks.named('jar', Jar).configure {
188202
finalizedBy 'reobfJar'
189203
}
190204

205+
tasks.test {
206+
useJUnitPlatform()
207+
}
191208
// However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing:
192209
// tasks.named('publish').configure {
193210
// dependsOn 'reobfJar'
@@ -209,8 +226,8 @@ publishing {
209226
maven {
210227
url = uri("https://maven.pkg.github.com/RealAntEngineer/Formic_API")
211228
credentials {
212-
username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
213-
password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
229+
username = System.getProperty("USERNAME")
230+
password = System.getProperty("TOKEN")
214231
}
215232
}
216233
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod_id=formicapi
2020
mod_name=Formic API
2121
mod_license=MIT
2222

23-
mod_version=1.0
23+
mod_version=1.1
2424

2525
create_version = 6.0.2-50
2626
flywheel_version = 1.0.1
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.rae.formicapi.math;
2+
3+
import com.rae.formicapi.FormicAPI;
4+
5+
import java.util.function.Function;
6+
7+
import static java.lang.Math.abs;
8+
9+
public class Solvers {
10+
/**
11+
*
12+
* @param function the equation you want to solve
13+
* @param a first boundaries
14+
* @param b second boundaries
15+
* @param epsilon tolerance
16+
* @return the Solution if there is one or 0.
17+
*/
18+
public static float dichotomy(Function<Float, Float> function, float a, float b, float epsilon) {
19+
try {
20+
if (function.apply(a) * function.apply(b) > 0) { //Verification of the boundaries
21+
throw new RuntimeException("Wrong boundaries in dichotomy solver : a=" + a + "f(a)=" + function.apply(a) + "| b=" + b + " f(b)=" + function.apply(b));
22+
} else {
23+
float m = (float) ((a + b) / 2.);
24+
while (abs(a - b) > epsilon) {
25+
if (function.apply(m) == 0.0) {
26+
return m;
27+
} else if (function.apply(a) * function.apply(m) > 0) {
28+
a = m;
29+
} else {
30+
b = m;
31+
}
32+
m = (a + b) / 2;
33+
}
34+
return m;
35+
}
36+
} catch (RuntimeException e) {
37+
FormicAPI.LOGGER.error(e.toString());
38+
return 0;
39+
}
40+
}
41+
//TODO complete a naive approach first
42+
43+
/**
44+
* Uses gradient descent to find the minimum of a given function, with adaptive step size.
45+
* (generated in part with chatgpt)
46+
* @param function a function that has a minimum
47+
* @param start starting point
48+
* @param step initial learning rate
49+
* @param dx small delta used to estimate the derivative
50+
* @return the estimated x value at which the function has a minimum, return NaN if there is no solution found
51+
*/
52+
public static float gradientDecent(Function<Float, Float> function, float start, float step, float dx) {
53+
float x = start;
54+
float learningRate = step;
55+
int maxIterations = 10000;
56+
float tolerance = 1e-6f;
57+
float decay = 0.9f; // How fast the step shrinks when progress slows
58+
float minStep = 1e-6f;
59+
60+
for (int i = 0; i < maxIterations; i++) {
61+
float derivative = (function.apply(x + dx) - function.apply(x - dx)) / (2 * dx);
62+
float newX = x - learningRate * derivative;
63+
64+
float currentValue = function.apply(x);
65+
float newValue = function.apply(newX);
66+
67+
if (newValue < currentValue) {
68+
// Improvement, keep going
69+
x = newX;
70+
// Optionally increase the step a little
71+
learningRate *= 1.05f;
72+
} else {
73+
// No improvement, reduce step size
74+
learningRate *= decay;
75+
if (learningRate < minStep) {
76+
return x;
77+
}
78+
}
79+
80+
if (Math.abs(derivative) < tolerance) {
81+
return x;
82+
}
83+
}
84+
85+
return Float.NaN;
86+
}
87+
}

src/main/java/com/rae/formicapi/multiblock/MBShape.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
public class MBShape {
1717
//make functions for every sizes
18-
public static MBShape make3x1x1(DirectionalBlock structure){
18+
public static MBShape make3x1x1(MBStructureBlock structure){
1919
return new MBShape(structure, new Vec3i(3, 1,1), new Vec3i(1,0,0),
2020
new HashMap<>(Map.of(
2121
Direction.UP, List.of(List.of(List.of(Direction.UP),List.of(),List.of(Direction.DOWN))),
@@ -27,7 +27,7 @@ public static MBShape make3x1x1(DirectionalBlock structure){
2727
)
2828
));
2929
}
30-
public static MBShape make2x1x1(DirectionalBlock structure){
30+
public static MBShape make2x1x1(MBStructureBlock structure){
3131
return new MBShape(structure, new Vec3i(2, 1,1), new Vec3i(1,0,0),
3232
new HashMap<>(Map.of(
3333
Direction.UP, List.of(List.of(List.of(Direction.UP))),
@@ -39,12 +39,12 @@ public static MBShape make2x1x1(DirectionalBlock structure){
3939
));
4040
}
4141
//default is north
42-
private final DirectionalBlock structure;
42+
private final MBStructureBlock structure;
4343
private final HashMap<Direction, List<List<List<Direction>>>> shapes;// X, Y, Z
4444
private final Vec3i defaultOffset;
4545
private final Vec3i defaultSize;
4646

47-
public MBShape(DirectionalBlock structure, Vec3i defaultSize, Vec3i defaultOffset,HashMap<Direction, List<List<List<Direction>>>> shapes) {
47+
public MBShape(MBStructureBlock structure, Vec3i defaultSize, Vec3i defaultOffset,HashMap<Direction, List<List<List<Direction>>>> shapes) {
4848
this.structure = structure;
4949
this.defaultOffset = defaultOffset;
5050
this.defaultSize = defaultSize;
@@ -96,7 +96,7 @@ public void repairStructure(Level level, BlockPos controlPos, Direction facing){
9696
negative?-y:y,
9797
negative?-z:z),
9898

99-
Blocks.DISPENSER.defaultBlockState().setValue(DirectionalBlock.FACING, shape.get(x + off.getX()).get(y + off.getY()).get(z + off.getZ()))
99+
structure.defaultBlockState().setValue(DirectionalBlock.FACING, shape.get(x + off.getX()).get(y + off.getY()).get(z + off.getZ()))
100100
);
101101
}
102102
}

src/main/java/com/rae/formicapi/multiblock/MBStructureBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static boolean stillValid(BlockGetter level, BlockPos pos, BlockState sta
143143
Direction direction = state.getValue(FACING);
144144
BlockPos targetedPos = pos.relative(direction);
145145
BlockState targetedState = level.getBlockState(targetedPos);
146-
return targetedState.getBlock() instanceof MBStructureBlock ||
146+
return //targetedState.getBlock() instanceof MBStructureBlock ||
147147
(targetedState.getBlock() instanceof MBController mb && state.is(mb.getStructure()));
148148
}
149149

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.rae.formicapi;
2+
3+
import com.rae.formicapi.math.Solvers;
4+
import org.junit.jupiter.api.Test;
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class TestSolvers {
8+
9+
@Test
10+
public void testGradientDescent1() {
11+
float result = Solvers.gradientDecent((x)-> x*x,-1, 0.1f,0.01f);
12+
float tolerance = 1e-6f;
13+
assertTrue(result > 0- tolerance && result < 0 + tolerance);
14+
}
15+
@Test
16+
public void testGradientDescent2() {
17+
float result = Solvers.gradientDecent((x)-> (float) Math.abs(Math.cos(x)),0, 0.1f,0.01f);
18+
float tolerance = 1e-6f;
19+
double expected = Math.PI/2f;
20+
assertTrue(result > expected- tolerance && result < expected + tolerance,() -> String.valueOf(result));
21+
}
22+
}

0 commit comments

Comments
 (0)