Skip to content

Commit

Permalink
feat: added new curves, added reflection based ui controls
Browse files Browse the repository at this point in the history
  • Loading branch information
Scoppio committed Dec 30, 2024
1 parent b4f5231 commit 82be31d
Show file tree
Hide file tree
Showing 27 changed files with 857 additions and 200 deletions.
4 changes: 1 addition & 3 deletions megamek/data/ai/tw/considerations/proto_considerations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ name: "MyUnitArmor"
curve: !<LinearCurve>
m: 0.5
b: 0.3
parameters:
minValue: 0
maxValue: 10
parameters: {}
--- !<TargetWithinOptimalRange>
name: "TargetWithinOptimalRange"
curve: !<LogisticCurve>
Expand Down
4 changes: 1 addition & 3 deletions megamek/data/ai/tw/decisions/proto_decisions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ decisionScoreEvaluator: !<TWDecisionScoreEvaluator>
curve: !<LinearCurve>
m: 0.5
b: 0.3
parameters:
minValue: 0
maxValue: 10
parameters: {}
- !<TargetWithinOptimalRange>
name: "TargetWithinOptimalRange"
curve: !<LogisticCurve>
Expand Down
8 changes: 2 additions & 6 deletions megamek/data/ai/tw/evaluators/proto_dse.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ considerations:
curve: !<LinearCurve>
m: 0.5
b: 0.3
parameters:
minValue: 0
maxValue: 10
parameters: {}
- !<TargetWithinOptimalRange>
name: "TargetWithinOptimalRange"
curve: !<LogisticCurve>
Expand All @@ -33,9 +31,7 @@ considerations:
curveType: "LinearCurve"
m: 0.5
b: 0.3
parameters:
minValue: 0
maxValue: 10
parameters: {}
- !<TargetWithinOptimalRange>
name: "TargetWithinOptimalRange"
curve:
Expand Down
106 changes: 106 additions & 0 deletions megamek/src/megamek/ai/utility/BandFilterCurve.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
*/

package megamek.ai.utility;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;

import java.util.StringJoiner;

import static megamek.codeUtilities.MathUtility.clamp01;

@JsonTypeName("BandFilterCurve")
public class BandFilterCurve implements Curve {
private double m;
private double b;
private double k;
private double c;

@JsonCreator
public BandFilterCurve(
@JsonProperty("m") double m,
@JsonProperty("b") double b,
@JsonProperty("k") double k,
@JsonProperty("c") double c) {
this.m = m;
this.b = b;
this.k = k;
this.c = c;
}

@Override
public BandFilterCurve copy() {
return new BandFilterCurve(m, b, k, c);
}

public double evaluate(double x) {

Check notice

Code scanning / CodeQL

Missing Override annotation Note

This method overrides
Curve.evaluate
; it is advisable to add an Override annotation.
var bandStart = m - b / 2;
var bandEnd = m + b / 2;

return clamp01(x < bandStart ? 1d + c : x > bandEnd ? 1d + c : 0d + k);
}

@Override
public double getC() {
return c;
}

@Override
public void setC(double c) {
this.c = c;
}

@Override
public double getM() {
return m;
}

@Override
public double getB() {
return b;
}

@Override
public double getK() {
return k;
}

@Override
public void setK(double k) {
this.k = k;
}

@Override
public void setM(double m) {
this.m = m;
}

@Override
public void setB(double b) {
this.b = b;
}

@Override
public String toString() {
return new StringJoiner(", ", BandFilterCurve.class.getSimpleName() + " [", "]")
.add("m=" + m)
.add("b=" + b)
.add("k=" + k)
.add("c=" + c)
.toString();
}
}
106 changes: 106 additions & 0 deletions megamek/src/megamek/ai/utility/BandPassCurve.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
*/

package megamek.ai.utility;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;

import java.util.StringJoiner;

import static megamek.codeUtilities.MathUtility.clamp01;

@JsonTypeName("BandPassCurve")
public class BandPassCurve implements Curve {
private double m;
private double b;
private double k;
private double c;

@JsonCreator
public BandPassCurve(
@JsonProperty("m") double m,
@JsonProperty("b") double b,
@JsonProperty("k") double k,
@JsonProperty("c") double c) {
this.m = m;
this.b = b;
this.k = k;
this.c = c;
}

@Override
public BandPassCurve copy() {
return new BandPassCurve(m, b, k, c);
}

public double evaluate(double x) {

Check notice

Code scanning / CodeQL

Missing Override annotation Note

This method overrides
Curve.evaluate
; it is advisable to add an Override annotation.
var bandStart = m - b / 2;
var bandEnd = m + b / 2;

return clamp01(x < bandStart ? 0d + k : x > bandEnd ? 0d + k : 1d + c);
}

@Override
public double getK() {
return k;
}

@Override
public void setK(double k) {
this.k = k;
}

@Override
public double getC() {
return c;
}

@Override
public void setC(double c) {
this.c = c;
}

@Override
public double getM() {
return m;
}

@Override
public double getB() {
return b;
}

@Override
public void setM(double m) {
this.m = m;
}

@Override
public void setB(double b) {
this.b = b;
}

@Override
public String toString() {
return new StringJoiner(", ", BandPassCurve.class.getSimpleName() + " [", "]")
.add("m=" + m)
.add("b=" + b)
.add("k=" + k)
.add("c=" + c)
.toString();
}
}
51 changes: 38 additions & 13 deletions megamek/src/megamek/ai/utility/Consideration.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public abstract class Consideration<IN_GAME_OBJECT,TARGETABLE> implements Named
@JsonProperty("parameters")
protected Map<String, Object> parameters = Collections.emptyMap();

protected transient Map<String, Class<?>> parameterTypes = Collections.emptyMap();

public Consideration() {
}

Expand Down Expand Up @@ -78,35 +80,58 @@ public Map<String, Object> getParameters() {
return Map.copyOf(parameters);
}

public Map<String, Class<?>> getParameterTypes() {
return Map.copyOf(parameterTypes);
}

public Class<?> getParameterType(String key) {
return parameterTypes.get(key);
}

public void setParameters(Map<String, Object> parameters) {
var params = new HashMap<String, Object>();

Check failure

Code scanning / CodeQL

Container contents are never accessed Error

The contents of this container are never accessed.
for (var entry : parameters.entrySet()) {
var clazz = parameterTypes.get(entry.getKey());
if (clazz == null) {
throw new IllegalArgumentException("Unknown parameter: " + entry.getKey());
}
if (clazz.isAssignableFrom(entry.getValue().getClass())) {
throw new IllegalArgumentException("Invalid parameter type for " + entry.getKey() + ": " + entry.getValue().getClass());
}
params.put(entry.getKey(), entry.getValue());
}
this.parameters = Map.copyOf(parameters);
}

protected double getDoubleParameter(String key) {
return (double) parameters.get(key);
public double getDoubleParameter(String key) {
return (double) getParameter(key);
}

public int getIntParameter(String key) {
return (int) getParameter(key);
}

protected int getIntParameter(String key) {
return (int) parameters.get(key);
public boolean getBooleanParameter(String key) {
return (boolean) getParameter(key);
}

protected boolean getBooleanParameter(String key) {
return (boolean) parameters.get(key);
public String getStringParameter(String key) {
return (String) getParameter(key);
}

protected String getStringParameter(String key) {
return (String) parameters.get(key);
public float getFloatParameter(String key) {
return (float) getParameter(key);
}

protected float getFloatParameter(String key) {
return (float) parameters.get(key);
public long getLongParameter(String key) {
return (long) getParameter(key);
}

protected long getLongParameter(String key) {
return (long) parameters.get(key);
public Object getParameter(String key) {
return parameters.get(key);
}

protected boolean hasParameter(String key) {
public boolean hasParameter(String key) {
return parameters.containsKey(key);
}

Expand Down
20 changes: 19 additions & 1 deletion megamek/src/megamek/ai/utility/Curve.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
@JsonSubTypes.Type(value = LinearCurve.class, name = "LinearCurve"),
@JsonSubTypes.Type(value = LogisticCurve.class, name = "LogisticCurve"),
@JsonSubTypes.Type(value = LogitCurve.class, name = "LogitCurve"),
@JsonSubTypes.Type(value = ParabolicCurve.class, name = "ParabolicCurve")
@JsonSubTypes.Type(value = ParabolicCurve.class, name = "ParabolicCurve"),
@JsonSubTypes.Type(value = BandPassCurve.class, name = "BandPassCurve"),
@JsonSubTypes.Type(value = BandFilterCurve.class, name = "BandFilterCurve"),
})
public interface Curve {
double evaluate(double x);
Expand Down Expand Up @@ -127,4 +129,20 @@ default void setK(double k) {
default void setC(double c) {
//
}

default double getM() {
return 0.0;
}

default double getB() {
return 0.0;
}

default double getK() {
return 0.0;
}

default double getC() {
return 0.0;
}
}
9 changes: 8 additions & 1 deletion megamek/src/megamek/ai/utility/DefaultCurve.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public enum DefaultCurve {
LogisticDecreasing(new LogisticCurve(1.0, 0.5, -10.0, 0.0)),

Logit(new LogitCurve(1.0, 0.5, -15.0, 0.0)),
LogitDecreasing(new LogitCurve(1.0, 0.5, 15.0, 0.0));
LogitDecreasing(new LogitCurve(1.0, 0.5, 15.0, 0.0)),

BandPass(new BandPassCurve(0.5, 0.2, 0, 0)),
BandFilter(new BandFilterCurve(0.5, 0.2, 0, 0));

private final Curve curve;

Expand All @@ -44,6 +47,10 @@ public static DefaultCurve fromCurve(Curve curve) {
return Logistic;
} else if (curve instanceof LogitCurve) {
return Logit;
} else if (curve instanceof BandPassCurve) {
return BandPass;
} else if (curve instanceof BandFilterCurve) {
return BandFilter;
}
// Return Linear as default
return Linear;
Expand Down
Loading

0 comments on commit 82be31d

Please sign in to comment.