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,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>flintstones.group</groupId>
<artifactId>flintstones.bundles</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>flintstones.entity.ahp</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<name>[bundle] Ahp</name>
</project>
+45
View File
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>flintstones.entity.ahp</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1779484362553</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
@@ -0,0 +1,7 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
@@ -0,0 +1,24 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Ahp
Bundle-SymbolicName: flintstones.entity.ahp
Bundle-Version: 1.0.0.qualifier
Automatic-Module-Name: flintstones.entity.ahp
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: flintstones.valuation.ahp,
flintstones.entity.problemelement,
flintstones.entity.valuation,
flintstones.model.valuation.service,
javax.inject,
flintstones.operator,
flintstones.valuation.numeric.real,
flintstones.operator.aggregation.geometricMean,
org.apache.commons.math3,
flintstones.helper.data,
flintstones.entity.valuation,
flintstones.model.domain.service,
flintstones.domain.numeric.real,
flintstones.entity.domain,
flintstones.helper.ahp,
flintstones.entity.operator
Export-Package: flintstones.entity.ahp
@@ -0,0 +1,4 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.
@@ -0,0 +1,107 @@
package flintstones.entity.ahp;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import flintstones.domain.numeric.real.NumericRealDomain;
import flintstones.entity.operator.AggregationOperator;
import flintstones.entity.problemelement.entities.ProblemElement;
import flintstones.entity.valuation.Valuation;
import flintstones.model.domain.service.IDomainService;
import flintstones.model.valuation.service.IValuationService;
import flintstones.operator.aggregation.geometricMean.GeometricMean;
import flintstones.operator.service.IOperatorService;
import flintstones.valuation.numeric.real.RealValuation;
/**
* The Class AHPAggregatedMatrix.
*/
public class AHPAggregatedMatrix {
/** The rows of the matrox. */
private ProblemElement[] rows;
/** The cols of the matrix. */
private ProblemElement[] cols;
/** The result matrix. */
private double[][] resultMatrix;
/** The valuation service. */
@Inject
IValuationService valuationService;
@Inject
IDomainService domainService;
/** The operator service. */
@Inject
IOperatorService operatorService;
/**
* Instantiates a new AHP aggregated matrix.
*/
public AHPAggregatedMatrix() {
}
/**
* Aggregate the given preferences using GeometricMean.
*
* @param preferences the preferences
*/
public void aggregate(AHPMatrix[] preferences) {
cols = preferences[0].getCols();
rows = preferences[0].getRows();
resultMatrix = new double[cols.length][rows.length];
// Aggregate the values of every of each matrix to retrive a single matrix. C1,A1,A2,E1 + C1,A1,A2,E2 + + C1,A1,A2,E3... -> C1,A1,A2
for (int i = 0; i < cols.length; i++) {
ProblemElement a = cols[i];
for (int j = 0; j < rows.length; j++) {
ProblemElement b = rows[j];
List<Valuation> valuations = new ArrayList<>();
for (AHPMatrix mat : preferences) {
double x = mat.getValue(a, b);
NumericRealDomain newDomain = (NumericRealDomain) domainService.create(NumericRealDomain.ID);
newDomain.setInRange(false);
RealValuation realValuation = (RealValuation) valuationService.create(newDomain);
realValuation.setValue(x);
valuations.add(realValuation);
}
AggregationOperator operator = operatorService.getAggregationOperator(GeometricMean.class.getPackage().getName().toString(), valuations.get(0).getId());
RealValuation result = (RealValuation) operator.computeAggregation(valuations, null);
resultMatrix[i][j] = result.getValue();
}
}
}
/**
* Gets the result matrix.
*
* @return the result matrix
*/
public double[][] getResultMatrix() {
return resultMatrix;
}
/**
* Gets the row.
*
* @return the row
*/
public ProblemElement[] getRow() {
return rows;
}
}
@@ -0,0 +1,99 @@
package flintstones.entity.ahp;
import java.util.stream.DoubleStream;
import org.apache.commons.math3.linear.EigenDecomposition;
import org.apache.commons.math3.linear.MatrixUtils;
import org.apache.commons.math3.linear.RealMatrix;
import flintstones.entity.ahp.exception.UncheckableConsistencyException;
import flintstones.helper.DoubleHelper;
/**
* The Class AHPConsistency.
*/
public class AHPConsistency {
/** The consistency random indexes. */
double[] consistencyRandomIndexes = {-1.0,-1.0,-1.0,0.58,0.9,1.12,1.24,1.32,1.41,1.45,1.49};
/**
* Instantiates a new AHP consistency checker.
*/
public AHPConsistency() {
}
/**
* Gets the consistency ratio.
*
* @param maxEigenValue the max eigen value
* @param numberOfItems the number of items
* @return the consistency ratio
*/
private double getConsistencyRatio(double maxEigenValue, int numberOfItems) {
if(numberOfItems == 2)
return 0;
else
return getConsistencyIndex(maxEigenValue, numberOfItems) / getRandomIndex(numberOfItems);
}
/**
* Gets the consistency index.
*
* @param maxEigenValue the max eigen value
* @param numberOfItems the number of items
* @return the consistency index
*/
private double getConsistencyIndex( double maxEigenValue, int numberOfItems ) {
return (maxEigenValue - numberOfItems)/(numberOfItems-1);
}
/**
* Gets the random index.
*
* @param numberOfItems the number of items
* @return the random index
*/
private double getRandomIndex(int numberOfItems) {
double val = consistencyRandomIndexes[numberOfItems];
if(numberOfItems > 10 || val == -1.0 )
throw new UncheckableConsistencyException(numberOfItems);
return val;
}
/**
* Gets the consistency ratio.
*
* @param mat the mat
* @return the consistency ratio
*/
public double getConsistencyRatio(AHPMatrix mat) {
double[][] values = mat.getValues();
RealMatrix realMatrix = MatrixUtils.createRealMatrix(values);
EigenDecomposition descomposition = null;
try {
descomposition = new EigenDecomposition(realMatrix);
} catch( org.apache.commons.math3.exception.MaxCountExceededException e ) {
System.err.println(e.getMessage());
}
if(descomposition == null)
return 1.0;
double[] eigenValues = descomposition.getRealEigenvalues();
double max = DoubleStream.of(eigenValues).max().getAsDouble();
max = DoubleHelper.Round(max,2);
double result = this.getConsistencyRatio(max,eigenValues.length);
return result;
}
}
@@ -0,0 +1,255 @@
package flintstones.entity.ahp;
import java.util.LinkedHashMap;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import flintstones.entity.problemelement.entities.ProblemElement;
import flintstones.helper.DoubleHelper;
import flintstones.helper.MatrixHelper;
import flintstones.helper.StringHelper;
import flintstones.helper.ahp.AHPHelper;
import flintstones.valuation.ahp.AHPValuation;
/**
* The Class AHPMatrix.
*/
public class AHPMatrix {
/** The content. */
private LinkedHashMap<ProblemElement, LinkedHashMap<ProblemElement, AHPValuation>> content = new LinkedHashMap<>();
/*
* El cuestionario:
*
* A2 -> 8 <- A1 A2 -> 1/6 <- A3 A1 -> 1/8 <- A2 (Repe) A1 -> 1 <- A3 A3 -> 6 <-
* A2 (Repe) A3 -> 1 <- A1 (Repe)
*
* Genera la matrix: A2 A1 A3 A2 1 8 1/6 A1 1/8 1 1 A3 6 1 1
*
* Genera la matrix de índices: A2 A1 A3 A2 8 15 3 A1 1 8 8 A3 13 8 8
*
*
*/
/**
* Instantiates a new AHP matrix.
*
* @param domain the domain
* @param alternatives the alternatives
*/
public AHPMatrix(ProblemElement[] alternatives) {
for (int i = 0; i < alternatives.length; i++) {
ProblemElement alternative = alternatives[i];
LinkedHashMap<ProblemElement, AHPValuation> map = new LinkedHashMap<>();
for (int j = i + 1; j < alternatives.length; j++) {
ProblemElement b = alternatives[j];
AHPValuation v = new AHPValuation().buildAHPValuation(alternative, b);
map.put(b, v);
}
content.put(alternative, map);
}
}
/**
* Sets the cell to a value by it index.
*
* @param a the first PE
* @param b the second PE
* @param index the index of the value in the domain
*/
public void set(ProblemElement a, ProblemElement b, int index) {
if (a.equals(b))
return;
LinkedHashMap<ProblemElement, AHPValuation> map = content.get(a);
AHPValuation val = map.get(b);
if (val == null) {
map = content.get(b);
val = map.get(a);
int reverseIndex = AHPHelper.getInverseIndex(index);
val.setIndex(reverseIndex);
} else {
val.setIndex(index);
}
}
// API for debbugging only
public void setValue(ProblemElement a, ProblemElement b, String valueLabel) {
String[] labels = AHPHelper.getLabels();
int index = StringHelper.IndexOf(labels, valueLabel);
set(a, b, index);
}
/**
* Gets the index.
*
* @param a the first PE
* @param b the second PE
* @return the index of the value in the domain
*/
public int getIndex(ProblemElement a, ProblemElement b) {
if (a.equals(b))
return (int) AHPHelper.getMidPoint();
LinkedHashMap<ProblemElement, AHPValuation> map = content.get(a);
AHPValuation val = map.get(b);
int index;
if (val == null) {
map = content.get(b);
val = map.get(a);
index = AHPHelper.getInverseIndex(val.getIndex());
} else {
index = val.getIndex();
}
return index;
}
/**
* Gets the label.
*
* @param a the first PE
* @param b the second PE
* @return the human readable label
*/
public String getLabel(ProblemElement a, ProblemElement b) {
int index = this.getIndex(a, b);
return AHPHelper.getLabels()[index];
}
/**
* Gets the value.
*
* @param a the first PE
* @param b the second PE
* @return the value
*/
public Double getValue(ProblemElement a, ProblemElement b) {
int index = this.getIndex(a, b);
Double val = DoubleHelper.ParseFraction(AHPHelper.getLabels()[index]);
return val;
}
/**
* Gets the values.
*
* @return the values
*/
public double[][] getValues() {
int size = content.size();
double[][] result = new double[size][size];
ProblemElement[] arr = content.keySet().stream().toArray(ProblemElement[]::new);
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
result[i][j] = this.getValue(arr[i], arr[j]);
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
boolean toStringIndex = true;
String[] indexValueMapping = AHPHelper.getLabels();
StringBuilder sb = new StringBuilder();
String[] headers = content.keySet().stream().map(k -> k.getName()).toArray(String[]::new);
String[][] m = new String[headers.length][headers.length];
ProblemElement[] pes = content.keySet().toArray(new ProblemElement[0]);
int i = 0;
for (ProblemElement pe1 : pes) {
int j = 0;
for (ProblemElement pe2 : pes) {
m[i][j] = toStringIndex ? indexValueMapping[getIndex(pe1, pe2)] : this.getIndex(pe1, pe2) + "";
j++;
}
i++;
}
// for(Entry<ProblemElement, LinkedHashMap<ProblemElement, AHPValuation>> entry1 : content.entrySet()) {
// int j = 0;
// for( Entry<ProblemElement, AHPValuation> entry2 : entry1.getValue().entrySet()) {
// m[i][j] = entry2.getValue().getIndex()+"";
// j++;
// }
// i++;
// }
String[][] m2 = MatrixHelper.addHeaders(m, headers, headers);
sb.append(StringHelper.PrintTable(m2));
sb.append("\n");
sb.append("RAW: \n");
sb.append(content.toString());
sb.append("\n");
return sb.toString();
}
/**
* Gets the cols.
*
* @return the cols
*/
public ProblemElement[] getCols() {
return this.content.keySet().toArray(new ProblemElement[0]);
}
/**
* Gets the rows.
*
* @return the rows
*/
public ProblemElement[] getRows() {
return this.content.keySet().toArray(new ProblemElement[0]);
}
/**
* Write.
*
* @param writer the writer
* @throws XMLStreamException the XML stream exception
*/
public void write(XMLStreamWriter writer) throws XMLStreamException {
ProblemElement[] arr = content.keySet().toArray(new ProblemElement[0]);
for (int i = 0; i < arr.length; i++) {
ProblemElement pe1 = arr[i];
for (int j = i; j < arr.length; j++) {
ProblemElement pe2 = arr[j];
if (!pe1.equals(pe2)) {
writer.writeStartElement("AHPValuation");
content.get(pe1).get(pe2).write(writer);
writer.writeEndElement();
}
}
}
}
}
@@ -0,0 +1,21 @@
package flintstones.entity.ahp.exception;
/**
* @author igmunoz
*
*/
public class UncheckableConsistencyException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = -1452084500458108325L;
/**
* @param numberOfItems the number of recibed items (n items -> matrix n*n)
*/
public UncheckableConsistencyException(int numberOfItems) {
super("Consistency can be checked between 3 and 10 only. ( numberOfItems: " + numberOfItems + ")");
}
}