#14 -- Add model

This commit is contained in:
Anton Romanov 2025-03-19 11:22:22 +04:00
parent 78f56d814b
commit 77158d0be8
4 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package ru.ulstu.method.fuzzy;
import ru.ulstu.datamodel.Model;
import ru.ulstu.datamodel.exception.ModelingException;
import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.datamodel.ts.TimeSeriesValue;
import ru.ulstu.method.Method;
import ru.ulstu.method.MethodParamValue;
import ru.ulstu.method.MethodParameter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class PlainFuzzy extends Method {
@Override
public List<MethodParameter> getAvailableParameters() {
return PlainFuzzyModel.getAvailableParameters();
}
@Override
protected Model getModelOfValidTimeSeries(TimeSeries timeSeries, List<MethodParamValue> parameters) {
PlainFuzzyModel model = new PlainFuzzyModel(timeSeries, parameters);
List<Triangle> fuzzySets = generateFuzzySets(timeSeries, model.getNumberOfFuzzyTerms().getIntValue());
return null;
}
private List<Triangle> generateFuzzySets(TimeSeries timeSeries, Integer numberOfFuzzyTerms) {
List<Triangle> fuzzySets = new ArrayList<Triangle>();
double min = timeSeries.getValues()
.stream()
.min(Comparator.comparing(TimeSeriesValue::getValue))
.map(TimeSeriesValue::getValue)
.orElseThrow(() -> new RuntimeException("Минимальное значение не найдено"));
double max = timeSeries.getValues()
.stream()
.max(Comparator.comparing(TimeSeriesValue::getValue))
.map(TimeSeriesValue::getValue)
.orElseThrow(() -> new RuntimeException("Максимальное значение не найдено"));
double delta = (max - min) / numberOfFuzzyTerms;
for (int i = 0; i < numberOfFuzzyTerms; i++) {
fuzzySets.add(new Triangle(min + i * numberOfFuzzyTerms,
min + i * numberOfFuzzyTerms + delta / 2,
min + i * numberOfFuzzyTerms + delta));
}
return fuzzySets;
}
@Override
protected TimeSeries getForecastWithValidParams(Model model, TimeSeries forecast) throws ModelingException {
return null;
}
@Override
public String getName() {
return "Нечеткая модель";
}
}

View File

@ -0,0 +1,52 @@
package ru.ulstu.method.fuzzy;
import ru.ulstu.datamodel.Model;
import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.method.MethodParamValue;
import ru.ulstu.method.MethodParameter;
import ru.ulstu.method.fuzzy.parameter.NumberOfFuzzyTerms;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlainFuzzyModel extends Model {
private final static String PREFIX_OF_FUZZY_LABEL = "X";
private final MethodParamValue numberOfFuzzyTerms = new MethodParamValue(NumberOfFuzzyTerms.getInstance(), 2);
private List<String> fuzzyTimeSeries = new ArrayList<>();
private TimeSeries defuzzyfiedTimeSeries;
protected PlainFuzzyModel(TimeSeries ts, List<MethodParamValue> parameters) {
super(ts);
defuzzyfiedTimeSeries = new TimeSeries("Defuzzified time series of ", ts.getKey());
for (MethodParamValue parameter : parameters) {
if (parameter.getParameter() instanceof NumberOfFuzzyTerms) {
numberOfFuzzyTerms.setValue(parameter.getValue());
}
}
}
public static List<MethodParameter> getAvailableParameters() {
return Collections.singletonList(NumberOfFuzzyTerms.getInstance());
}
public MethodParamValue getNumberOfFuzzyTerms() {
return numberOfFuzzyTerms;
}
public List<String> getFuzzyTimeSeries() {
return fuzzyTimeSeries;
}
public void setFuzzyTimeSeries(List<String> fuzzyTimeSeries) {
this.fuzzyTimeSeries = fuzzyTimeSeries;
}
public TimeSeries getDefuzzyfiedTimeSeries() {
return defuzzyfiedTimeSeries;
}
public void setDefuzzifiedTimeSeries(TimeSeries defuzzifiedTimeSeries) {
this.defuzzyfiedTimeSeries = defuzzifiedTimeSeries;
}
}

View File

@ -0,0 +1,51 @@
package ru.ulstu.method.fuzzy;
public class Triangle {
private double start; // левая граница треугольника
private double end; // правая граница треугольника
private double top; // вершина треугольника
public double getStart() {
return start;
}
public Triangle(double start, double top, double end) {
this.start = start;
this.top = top;
this.end = end;
}
public void setStart(int start) {
this.start = start;
}
public double getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public double getTop() {
return top;
}
public void setTop(int top) {
this.top = top;
}
public double getValueAtPoint(int pointIndex) {
if (pointIndex == this.getTop()) {
return 1;
} else if ((pointIndex >= this.getEnd()) || (pointIndex <= this.getStart())) {
return 0;
} else if (pointIndex < this.getTop()) {
return (pointIndex - this.getStart()) / (this.getTop() - this.getStart());
} else if (pointIndex > this.getTop()) {
return -(pointIndex - this.getEnd()) / (this.getEnd() - this.getTop());
}
return 0;
}
}

View File

@ -0,0 +1,32 @@
package ru.ulstu.method.fuzzy.parameter;
import ru.ulstu.datamodel.ts.TimeSeries;
import ru.ulstu.method.MethodParameter;
import java.util.ArrayList;
import java.util.List;
public class NumberOfFuzzyTerms extends MethodParameter {
private final static int MIN_NUMBER_OF_FUZZY_TERMS = 2;
private final static int MIN_INCREASING_STEP_OF_NUMBER_OF_FUZZY_TERMS = 1;
private final static int MAX_NUMBER_OF_FUZZY_TERMS = 7;
public NumberOfFuzzyTerms() {
super("Number of fuzzy terms");
}
public static NumberOfFuzzyTerms getInstance() {
return new NumberOfFuzzyTerms();
}
@Override
public List<Number> getAvailableValues(TimeSeries timeSeries) {
List<Number> values = new ArrayList<>();
for (double i = MIN_NUMBER_OF_FUZZY_TERMS;
i <= MAX_NUMBER_OF_FUZZY_TERMS;
i += MIN_INCREASING_STEP_OF_NUMBER_OF_FUZZY_TERMS) {
values.add(i);
}
return values;
}
}