public code v1

This commit is contained in:
2026-05-22 11:14:29 +02:00
parent 427197ec5a
commit b8141736eb
28859 changed files with 575079 additions and 0 deletions
@@ -0,0 +1,221 @@
package afryca.twotuple;
import afryca.domain.fuzzyset.FuzzySet;
import afryca.domain.fuzzyset.function.types.TrapezoidalFunction;
import afryca.domain.fuzzyset.labelset.Label;
import afryca.twotuple.i18n.Messages;
import afryca.valueforcer.ValueForcer;
public class TwoTuple {
private Label label;
private float alpha;
private FuzzySet domain;
public TwoTuple(FuzzySet domain) {
setDomain(domain);
alpha = 0;
}
public TwoTuple(FuzzySet domain, Label label) {
setDomain(domain);
setLabel(label);
}
public TwoTuple(FuzzySet domain, Label label, float alpha) {
setDomain(domain);
setLabel(label);
setAlpha(alpha);
}
public void setDomain(FuzzySet domain) {
ValueForcer.notNull(domain);
this.domain = domain;
}
public void setLabel(String name) {
Label label = domain.getLabelSet().getLabel(name);
ValueForcer.notNull(label);
this.label = label;
}
public void setLabel(Label label) {
ValueForcer.notNull(label);
this.label = label;
}
public void setLabel(int pos) {
Label label = domain.getLabelSet().getLabel(pos);
ValueForcer.notNull(label);
this.label = label;
}
public void setAlpha(float alpha) {
ValueForcer.inRange(alpha, -0.5, 0.5); //$NON-NLS-1$
int pos = domain.getLabelSet().getPos(label);
if((pos == 0) && (alpha < 0)) {
throw new IllegalArgumentException(Messages.TwoTuple_Invalid_alpha_value);
}
if((pos == domain.getLabelSet().getCardinality() - 1) && (alpha > 0)) {
throw new IllegalArgumentException(Messages.TwoTuple_Invalid_alpha_value);
}
this.alpha = alpha;
}
public Label getLabel() {
return label;
}
public FuzzySet getDomain() {
return domain;
}
public float getAlpha() {
return alpha;
}
public void calculateDelta(float beta) {
int labelIndex = (int) Math.round(beta);
setLabel(labelIndex);
float alpha = beta - labelIndex;
if(alpha == 0.5) {
labelIndex++;
setLabel(labelIndex);
alpha = -0.5f;
}
setAlpha(alpha);
}
public float calculateInverseDelta() {
return alpha + domain.getLabelSet().getPos(label);
}
public TwoTuple negateValuation() {
TwoTuple result = (TwoTuple) clone();
if(domain.getLabelSet().getCardinality() > 1) {
result.calculateDelta((domain.getLabelSet().getCardinality() - 1) - calculateInverseDelta());
}
return result;
}
public TrapezoidalFunction getFuzzyNumber() {
double distance = alpha / (domain.getLabelSet().getCardinality() - 1);
double a;
if(((TrapezoidalFunction) label.getSemantic()).getA() + distance < 0)
a = ((TrapezoidalFunction) label.getSemantic()).getA();
else
a = ((TrapezoidalFunction) label.getSemantic()).getA() + distance;
double d;
if(((TrapezoidalFunction) label.getSemantic()).getA() + distance > 1)
d = ((TrapezoidalFunction) label.getSemantic()).getD();
else
d = ((TrapezoidalFunction) label.getSemantic()).getD() + distance;
TrapezoidalFunction fuzzyNumber = new TrapezoidalFunction();
fuzzyNumber.setLimits(new double[] {a, ((TrapezoidalFunction) label.getSemantic()).getB() + distance,
((TrapezoidalFunction) label.getSemantic()).getC() + distance, d});
return fuzzyNumber;
}
public TwoTuple transform(FuzzySet fuzzySet) {
TwoTuple result = null;
ValueForcer.notNull(fuzzySet);
if (!fuzzySet.isBLTS()) {
throw new IllegalArgumentException(Messages.TwoTuple_Not_BLTS_fuzzy_set);
}
int thisCardinality = domain.getLabelSet().getCardinality();
int otherCardinality = fuzzySet.getLabelSet().getCardinality();
float numerator = calculateInverseDelta() * ((float) (otherCardinality - 1));
float denominator = (float) (thisCardinality - 1);
float beta = numerator / denominator;
result = new TwoTuple(fuzzySet);
result.calculateDelta(beta);
return result;
}
public double similarityMeasure(TwoTuple twoTuple) {
return 1d - distance(twoTuple);
}
public double distance(TwoTuple twoTuple) {
return Math.abs(this.calculateInverseDelta() - twoTuple.calculateInverseDelta()) / (domain.getLabelSet().getCardinality() - 1);
}
@Override
public String toString() {
return ("[" + label + ", " + Math.round(alpha * 10000d) / 10000d + "]" + Messages.TwoTuple_in + domain); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
public String prettyFormat() {
return ("(" + label + ", " + Math.round(alpha * 10000d) / 10000d + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TwoTuple other = (TwoTuple) obj;
if (domain != other.domain)
return false;
if (label != other.label)
return false;
if (alpha != other.alpha)
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + domain.hashCode();
result = prime * result + label.hashCode();
result = prime * result + Double.hashCode(alpha);
return result;
}
public int compareTo(TwoTuple other) {
ValueForcer.notNull(other);
ValueForcer.notIllegalElementType(other, new String[] { TwoTuple.class.toString()});
if(domain.equals(other.getDomain())) {
Label otherLabel = other.label;
double otherAlpha = other.alpha;
int aux;
if((aux = label.compareTo(otherLabel)) == 0) {
return Double.compare(alpha, otherAlpha);
} else {
return aux;
}
} else {
return 1;
}
}
@Override
public Object clone() {
return new TwoTuple(this.domain, this.label, this.alpha);
}
}
@@ -0,0 +1,20 @@
package afryca.twotuple.i18n;
import org.eclipse.osgi.util.NLS;
public class Messages extends NLS {
private static final String BUNDLE_NAME = "afryca.twotuple.i18n.messages"; //$NON-NLS-1$
public static String TwoTuple_Different_domains;
public static String TwoTuple_in;
public static String TwoTuple_Invalid_alpha_value;
public static String TwoTuple_Not_BLTS_fuzzy_set;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}
private Messages() {
}
}
@@ -0,0 +1,4 @@
TwoTuple_Different_domains=Different domains
TwoTuple_in=in
TwoTuple_Invalid_alpha_value=Invalid alpha value
TwoTuple_Not_BLTS_fuzzy_set=Not BLTS fuzzy set