public code v1

This commit is contained in:
Francisco Jesús Martínez Mimbrera
2026-05-23 00:32:57 +02:00
commit 759a8968a2
4357 changed files with 163763 additions and 0 deletions
@@ -0,0 +1,334 @@
package flintstones.method.numeric.common.aggregation;
import org.eclipse.e4.core.services.nls.Translation;
import flintstones.domain.fuzzyset.FuzzySet;
import flintstones.domain.numeric.NumericDomain;
import flintstones.domain.numeric.real.NumericRealDomain;
import flintstones.entity.method.phase.ImportedDataNotFoundException;
import flintstones.entity.method.phase.PhaseMethod;
import flintstones.entity.operator.AggregationOperator;
import flintstones.entity.problemelement.ProblemElementKey;
import flintstones.entity.problemelement.entities.Alternative;
import flintstones.entity.problemelement.entities.Criterion;
import flintstones.entity.problemelement.entities.Expert;
import flintstones.entity.problemelement.entities.ProblemElement;
import flintstones.entity.problemelement.entities.ProblemElementHelper;
import flintstones.entity.valuation.Valuation;
import flintstones.helper.data.HashMatrix;
import flintstones.method.numeric.common.aggregation.messages.Messages;
import flintstones.model.problemelement.service.IProblemElementService;
import flintstones.model.valuation.service.IValuationService;
import flintstones.operator.service.IOperatorService;
import flintstones.valuation.linguistic.LinguisticValuation;
import flintstones.valuation.numeric.NumericValuation;
import flintstones.valuation.numeric.real.RealValuation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import javax.inject.Inject;
public class NumericAggregation extends PhaseMethod {
@Inject
IProblemElementService problemService;
@Inject
IOperatorService operatorService;
@Inject
IValuationService valuationService;
@Inject
@Translation
Messages msg;
/**
* Problem alternatives
*/
Alternative[] allAlternatives;
/**
* Problem criteria
*/
Criterion[] allCriteria;
/**
* Problem experts
*/
Expert[] allExperts;
/**
* Collected valuations for [expert][alternative][criterion]
*/
NumericValuation[][][] initialValuations;
/**
* Expert given criteria weights
*/
HashMatrix<Expert, Criterion, Double> initialCritWeights;
/**
* Experts weights
*/
HashMap<ProblemElement, Double> expertsWeights;
/**
* Aggregated valuations
*/
HashMatrix<Alternative, Criterion, Valuation> aggregatedValuations;
/**
* Aggregated criteria weights
*/
HashMap<ProblemElement, Double> aggregatedCriteriaWeights;
/**
* @return Phase name
*/
@Override
public String getName() {
return msg.phase_name;
}
/**
* Get data from previous phases and initial valuations
*/
@SuppressWarnings("unchecked")
public void loadData() {
allAlternatives = ProblemElementHelper.asAlternatives(problemService.getAll(Alternative.Type));
allCriteria = ProblemElementHelper.asCriterions(problemService.getSubElements(Criterion.Type));
allExperts = ProblemElementHelper.asExperts(problemService.getSubElements(Expert.Type));
try {
initialCritWeights = (HashMatrix<Expert, Criterion, Double>) this.getPreviousPhase().importData("criterionWeights");
expertsWeights = (HashMap<ProblemElement, Double>) importData("expertWeights");
} catch (ImportedDataNotFoundException e) {
initialCritWeights = (HashMatrix<Expert, Criterion, Double>) importData("criterionWeights");
expertsWeights = (HashMap<ProblemElement, Double>) this.getPreviousPhase().importData("expertWeights");
}
if(allExperts.length == 1)
aggregatedCriteriaWeights = new LinkedHashMap<>(initialCritWeights.get(allExperts[0]));
else
aggregatedCriteriaWeights = new LinkedHashMap<ProblemElement, Double>();
initialValuations = new NumericValuation[allExperts.length][allAlternatives.length][allCriteria.length];
aggregatedValuations = new HashMatrix<Alternative, Criterion, Valuation>();
loadValuations();
}
/**
* Gets initial valuations casted to NumericValuation
*/
private void loadValuations() {
ProblemElementKey key;
Valuation auxValuation;
ArrayList<String> labels;
NumericRealDomain auxDomain;
for (int e = 0; e < allExperts.length; e++) {
for (int a = 0; a < allAlternatives.length; a++) {
for (int c = 0; c < allCriteria.length; c++) {
// Find every valuation
key = new ProblemElementKey(allExperts[e], allAlternatives[a], allCriteria[c]);
auxValuation = valuationService.getValuationFor(key);
// if not real number valuation
if (!auxValuation.getDomain().getType().equals("flintstones.domain.numeric.real")) {
double value;
double dMax, dMin;
// if linguistic
if (auxValuation.getDomain().getType().equals("flintstones.domain.linguistic")) {
labels = ((FuzzySet) auxValuation.getDomain()).getLabelSet().getLabelsName();
// use label index as value
value = labels.indexOf(((LinguisticValuation) auxValuation).getLabel().getName());
dMin = 0;
dMax = labels.size();
} else { // other numeric type
value = ((NumericValuation) auxValuation).getValue();
NumericDomain temp = (NumericDomain) auxValuation.getDomain();
dMin = temp.getMin();
dMax = temp.getMax();
}
auxDomain = new NumericRealDomain();
auxDomain.setMinMax(dMin, dMax);
auxDomain.setInRange(true);
auxValuation = new RealValuation();
auxValuation.setDomain(auxDomain);
auxValuation.setValueFromString(Double.toString(value));
}
if (e == 0) {
aggregatedValuations.put(allAlternatives[a], allCriteria[c], auxValuation);
}
initialValuations[e][a][c] = (NumericValuation) auxValuation;
}
}
}
}
/**
* Calculate aggregated valuations
*
* @param operatorId ID for aggregation operator
*/
public void computeAggregatedMatrix(AggregationOperator operator) {
LinkedList<Valuation> valuations = new LinkedList<>();
for (int a = 0; a < allAlternatives.length; a++) {
for (int c = 0; c < allCriteria.length; c++) {
for (int e = 0; e < allExperts.length; e++)
valuations.add(initialValuations[e][a][c]);
Valuation aggregated = (NumericValuation) operator.computeAggregation(valuations, new ArrayList<Double>(expertsWeights.values()));
aggregatedValuations.put(allAlternatives[a], allCriteria[c], aggregated);
valuations.clear();
}
}
}
/**
* Calculate aggregated weights
*
* @param operatorId ID for aggregation operator
*/
public void computeAggregatedWeights(AggregationOperator operator) {
ArrayList<Valuation> wTemp = new ArrayList<>();
NumericValuation temp;
NumericRealDomain dTemp = new NumericRealDomain();
dTemp.setInRange(true);
dTemp.setMinMax(0.0, 1.0);
for (int c = 0; c < allCriteria.length; c++) {
for (int e = 0; e < allExperts.length; e++) {
temp = new RealValuation();
temp.setDomain(dTemp);
temp.setValue(initialCritWeights.get(allExperts[e], allCriteria[c]));
wTemp.add(temp);
}
temp = (NumericValuation) operator.computeAggregation(wTemp, new ArrayList<Double>(expertsWeights.values()));
aggregatedCriteriaWeights.put(allCriteria[c], Math.round(temp.getValue() * 1000d) / 1000d);
wTemp.clear();
}
}
/**
* Exports aggregated data for next phases
*/
public void exportData() {
this.exportData("criterionWeights", aggregatedCriteriaWeights, true);
this.exportData("decisionmatrix", aggregatedValuations, true);
}
/**
* @return Problem alternatives
*/
public Alternative[] getAlternatives() {
return allAlternatives;
}
/**
* @return Problem criteria
*/
public Criterion[] getCriteria() {
return allCriteria;
}
/**
* @return All alternatives names as array
*/
public String[] getAlternativesNames() {
String[] str = new String[allAlternatives.length];
for (int i = 0; i < allAlternatives.length; i++)
str[i] = problemService.getByName(Alternative.Type, allAlternatives[i].getName()).getName();
return str;
}
/**
* @return All criteria names as array
*/
public String[] getCriteriaNames() {
String[] str = new String[allCriteria.length];
for (int i = 0; i < allCriteria.length; i++)
str[i] = problemService.getByName(Criterion.Type, allCriteria[i].getName()).getName();
return str;
}
/**
* @return All expert names as array
*/
public String[] getExpertsNames() {
String[] str = new String[allExperts.length];
for (int i = 0; i < allExperts.length; i++)
str[i] = problemService.getByName(Expert.Type, allExperts[i].getName()).getName();
return str;
}
/**
* @return Number of criteria
*/
public int numberCriteria() {
return allCriteria.length;
}
/**
* @return Number of experts
*/
public int numberExperts() {
return allExperts.length;
}
/**
* @param expert Expert index
* @param alternative Alternative index
* @param criterion Criterion index
* @return Collected valuation value
*/
public double getInitialData(int expert, int alternative, int criterion) {
return initialValuations[expert][alternative][criterion].getValue();
}
/**
* @param alternative Alternative index
* @param criterion Criterion index
* @return Aggregated valuation value
*/
public double getAggregatedData(int alternative, int criterion) {
return Math.round(
((NumericValuation) aggregatedValuations.get(allAlternatives[alternative], allCriteria[criterion]))
.getValue() * 1000d)
/ 1000d;
}
/**
* @param expertIndex Expert index
* @return Criteria weights for the expert
*/
public HashMap<Criterion, Double> getValuationWeights(int expertIndex) {
return initialCritWeights.get(allExperts[expertIndex]);
}
/**
* @param c Criterion index
* @return Aggregated criterion weight
*/
public double getAggCritWeight(int c) {
return aggregatedCriteriaWeights.get(allCriteria[c]);
}
}
@@ -0,0 +1,8 @@
package flintstones.method.numeric.common.aggregation.messages;
import org.eclipse.e4.core.services.nls.Message;
@Message
public class Messages {
public String phase_name;
}