public code v1
This commit is contained in:
+178
@@ -0,0 +1,178 @@
|
||||
package flintstones.method.promethee.phase.result;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author Alex
|
||||
*/
|
||||
|
||||
public class Promethee {
|
||||
|
||||
//----------------- DATOS INICIALES DEL PROBLEMA ----------------------
|
||||
float[][] initMatrix;
|
||||
int[] MinMax; //0 --> Min || 1 --> Max
|
||||
float[] weights;
|
||||
int[] typeFunc;
|
||||
|
||||
String[] alternatives;
|
||||
String[] criteria;
|
||||
//--------------------- VARIABLES INTERNAS ----------------------------
|
||||
ArrayList<float[][]> preferencesDegrees;
|
||||
ArrayList<float[][]> flowMatrix;
|
||||
float[][] globalFlowMatrix;
|
||||
float[] p;
|
||||
float[] q;
|
||||
float[] s;
|
||||
|
||||
public Promethee(String[] alternatives, String[] criterions) {
|
||||
this.alternatives = alternatives;
|
||||
this.criteria = criterions;
|
||||
}
|
||||
|
||||
//-------------------- GETTERS AND SETTERS ---------------------------
|
||||
public float[][] getMAtrixFlow(int indexCriterion){
|
||||
return flowMatrix.get(indexCriterion);
|
||||
}
|
||||
|
||||
public float[][] getGlobalFLow(){
|
||||
return globalFlowMatrix;
|
||||
}
|
||||
|
||||
//-------------------- PROMETHEE FUNCTIONS ---------------------------
|
||||
|
||||
public void init() {
|
||||
preferencesDegrees = new ArrayList<float[][]>();
|
||||
flowMatrix = new ArrayList<float[][]>();
|
||||
globalFlowMatrix = new float[alternatives.length][3];
|
||||
|
||||
//Inicializamos el vector de matrices para cada criterio enfrentando alternativas con alternativas.
|
||||
for (int i = 0; i < criteria.length; ++i) {
|
||||
preferencesDegrees.add(new float[alternatives.length][alternatives.length]);
|
||||
flowMatrix.add(new float[alternatives.length][3]);
|
||||
}
|
||||
}
|
||||
|
||||
public void Execute(float[][] initMatrix, float[] weights, int[] MinMax, int[] typeFunc, float[] p, float[] q, float[] s) {
|
||||
//Iniciamos los valores
|
||||
this.initMatrix = initMatrix;
|
||||
this.weights = weights;
|
||||
this.MinMax = MinMax;
|
||||
this.typeFunc = typeFunc;
|
||||
this.p = p;
|
||||
this.q = q;
|
||||
this.s = s;
|
||||
|
||||
init();
|
||||
|
||||
//Calculamos las matrices.
|
||||
calculatePreferences();
|
||||
|
||||
//Calculamos el flujo
|
||||
calculateFlow();
|
||||
|
||||
//Calculamos el flujo global según sus pesos.
|
||||
calculateGlobalsFlow();
|
||||
}
|
||||
|
||||
//Calcula las matrices de preferencia según la función especificada.
|
||||
private void calculatePreferences() {
|
||||
float d = 0.0f;
|
||||
//Nos recorremos los criteros para calcular cada matriz
|
||||
for (int iCriterion = 0; iCriterion < criteria.length; iCriterion++) {
|
||||
|
||||
//Enfrentamos las alternativas según el criterio (iCriterion) que estamos calculando.
|
||||
for (int iAlternative = 0; iAlternative < alternatives.length; iAlternative++) {
|
||||
|
||||
for (int jAlternative = 0; jAlternative < alternatives.length; jAlternative++) {
|
||||
|
||||
if (iAlternative != jAlternative) {
|
||||
//Comprobamos si es de beneficio o de coste.
|
||||
d = (MinMax[iCriterion] == 0)
|
||||
? -(initMatrix[iAlternative][iCriterion] - initMatrix[jAlternative][iCriterion])
|
||||
: (initMatrix[iAlternative][iCriterion] - initMatrix[jAlternative][iCriterion]);
|
||||
|
||||
preferencesDegrees.get(iCriterion)[iAlternative][jAlternative] = getResultFunction(d, iCriterion);
|
||||
|
||||
} else {
|
||||
preferencesDegrees.get(iCriterion)[iAlternative][jAlternative] = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Calculamos el flujo positivo y negativo de cada matriz según sus criterios.
|
||||
private void calculateFlow() {
|
||||
float positive = 0, negative = 0;
|
||||
for (int i = 0; i < preferencesDegrees.size(); i++) {
|
||||
|
||||
//Caculamos el flujo positivo, negativo y neto.
|
||||
for (int iAlt = 0; iAlt < alternatives.length; iAlt++) {
|
||||
for (int jAlternative = 0; jAlternative < alternatives.length; jAlternative++) {
|
||||
positive += preferencesDegrees.get(i)[iAlt][jAlternative];
|
||||
negative += preferencesDegrees.get(i)[jAlternative][iAlt];
|
||||
}
|
||||
flowMatrix.get(i)[iAlt][0] = positive / (alternatives.length - 1);
|
||||
flowMatrix.get(i)[iAlt][1] = negative / (alternatives.length - 1);
|
||||
flowMatrix.get(i)[iAlt][2] = flowMatrix.get(i)[iAlt][0] - flowMatrix.get(i)[iAlt][1];
|
||||
positive = 0;
|
||||
negative = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Calculamos el flujo positivo y negativo de cada matriz según sus criterios.
|
||||
private void calculateGlobalsFlow() {
|
||||
float positive = 0, negative = 0;
|
||||
|
||||
//Caculamos el flujo global positivo, negativo y neto.
|
||||
for (int iAlt = 0; iAlt < alternatives.length; iAlt++) {
|
||||
|
||||
for (int j = 0; j < flowMatrix.size(); j++) {
|
||||
positive += flowMatrix.get(j)[iAlt][0] * weights[j];
|
||||
negative += flowMatrix.get(j)[iAlt][1] * weights[j];
|
||||
}
|
||||
globalFlowMatrix[iAlt][0] = positive;
|
||||
globalFlowMatrix[iAlt][1] = negative;
|
||||
globalFlowMatrix[iAlt][2] = positive - negative;
|
||||
positive = 0;
|
||||
negative = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//Según la función especificada, te devuelve el valor P(d) correspondiente
|
||||
private float getResultFunction(float d, int iCriterion) {
|
||||
switch (typeFunc[iCriterion]) {
|
||||
case 1:
|
||||
if(d > 0 ) return 1;
|
||||
else return 0;
|
||||
case 2:
|
||||
if(d > q[iCriterion] ) return 1;
|
||||
else return 0;
|
||||
case 3:
|
||||
if(d > p[iCriterion] ) return 1;
|
||||
else if (d <= 0) return 0;
|
||||
else return d / p[iCriterion];
|
||||
case 4:
|
||||
if(d > p[iCriterion] ) return 1;
|
||||
else if (d <= q[iCriterion]) return 0;
|
||||
else return 1/2;
|
||||
case 5:
|
||||
if (d <= q[iCriterion]) {
|
||||
return 0.0f;
|
||||
} else if (d > p[iCriterion]) {
|
||||
return 1.0f;
|
||||
} else {
|
||||
return ((d - q[iCriterion]) / (p[iCriterion] - q[iCriterion]));
|
||||
}
|
||||
case 6:
|
||||
if(d <= 0 ) return 0;
|
||||
else {
|
||||
float exponentialFunc = (float) java.lang.Math.exp((-1) * (Math.pow(d, 2) / (2 * Math.pow(s[iCriterion], 2))));
|
||||
return 1 - exponentialFunc;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package flintstones.method.promethee.phase.result;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import flintstones.entity.method.phase.PhaseMethod;
|
||||
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.model.problemelement.service.IProblemElementService;
|
||||
import flintstones.model.valuation.service.IValuationService;
|
||||
import flintstones.valuation.numeric.NumericValuation;
|
||||
|
||||
public class PrometheeResult extends PhaseMethod {
|
||||
|
||||
@Inject
|
||||
IProblemElementService problemService;
|
||||
|
||||
@Inject
|
||||
IValuationService valuationService;
|
||||
|
||||
// Atributos
|
||||
HashMap<ProblemElementKey, Valuation> mapVAluations;
|
||||
Alternative[] allAlternatives;
|
||||
Criterion[] allCriterion;
|
||||
Expert[] Allexperts;
|
||||
String[] rowHeaderPromethee;
|
||||
|
||||
Promethee promethee;
|
||||
float[] weights;
|
||||
float[][] values;
|
||||
int[] MinMax;
|
||||
|
||||
public PrometheeResult() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Phase Promethee Result";
|
||||
}
|
||||
|
||||
//------------------------------- GETS AND SETTERS ---------------------------------
|
||||
public void cargarDatos() {
|
||||
rowHeaderPromethee = new String[3];
|
||||
rowHeaderPromethee[0] = "Positive Flow";
|
||||
rowHeaderPromethee[1] = "Negative Flow";
|
||||
rowHeaderPromethee[2] = "Neto Flow";
|
||||
|
||||
allAlternatives = ProblemElementHelper.asAlternatives(problemService.getAll(Alternative.Type));
|
||||
allCriterion = ProblemElementHelper.asCriterions(problemService.getAll(Criterion.Type));
|
||||
Allexperts = ProblemElementHelper.asExperts(problemService.getAll(Expert.Type));
|
||||
|
||||
promethee = new Promethee(getAlternativesNames(), getCriterionNames());
|
||||
|
||||
values = this.getValuations(Allexperts[0]);
|
||||
|
||||
float[] p = (float[]) this.importData("arrayP");
|
||||
float[] q = (float[]) this.importData("arrayQ");
|
||||
float[] s = (float[]) this.importData("arrayS");
|
||||
float[] w = (float[]) this.importData("arrayWeights");
|
||||
int[] MinMax = (int[]) this.importData("arrayMinMax");
|
||||
int[] typeFunc = (int[]) this.importData("arrayTypeFunc");
|
||||
|
||||
this.promethee.Execute(this.values, w, MinMax, typeFunc, p, q, s);
|
||||
}
|
||||
|
||||
public float[][] getValuations(Expert e) {
|
||||
String[] alternatives = getAlternativesNames();
|
||||
String[] criterions = getCriterionNames();
|
||||
float[][] valuation = new float[allAlternatives.length][allCriterion.length];
|
||||
|
||||
//Obtenemos el hashmap
|
||||
ProblemElement pE = problemService.getByCanonicalName(Expert.Type, e.getCanonicalName());
|
||||
mapVAluations = valuationService.getAllValuationsKVWith(pE);
|
||||
|
||||
//Para cada elemento, introducirlo ordenado en el array
|
||||
int a = 0, b = 0;
|
||||
for(Entry<ProblemElementKey, Valuation> entry : mapVAluations.entrySet()){
|
||||
NumericValuation nV = (NumericValuation) entry.getValue();
|
||||
|
||||
for(int i = 0; i < allAlternatives.length; i++) {
|
||||
if(entry.getKey().getAlternative().getName().equals( alternatives[i] )) {
|
||||
a = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(int i = 0; i < allCriterion.length; i++) {
|
||||
if(entry.getKey().getCriterion().getName().equals( criterions[i])) {
|
||||
b = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
double number = Double.valueOf(nV.getValue());
|
||||
valuation[a][b] = (float) number;
|
||||
a = 0;
|
||||
b = 0;
|
||||
}
|
||||
|
||||
return valuation;
|
||||
}
|
||||
|
||||
public float[][] getResult() {
|
||||
return promethee.getGlobalFLow();
|
||||
}
|
||||
|
||||
public float[][] getCriterionMatrixFlow(int indexCriterion){
|
||||
return promethee.getMAtrixFlow(indexCriterion);
|
||||
}
|
||||
|
||||
public void executePromethee() {
|
||||
|
||||
}
|
||||
|
||||
public String[] getRowHeaderPromethee() {
|
||||
return rowHeaderPromethee;
|
||||
}
|
||||
|
||||
public String[] getAlternativesNames() {
|
||||
String[] str = new String[allAlternatives.length];
|
||||
|
||||
for(int i = 0; i < allAlternatives.length; i++)
|
||||
str[i] = allAlternatives[i].getName();
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public String[] getCriterionNames() {
|
||||
String[] str = new String[allCriterion.length];
|
||||
|
||||
for(int i = 0; i < allCriterion.length; i++) {
|
||||
str[i] = allCriterion[i].getName();
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public int numberCriterions() {
|
||||
return allCriterion.length;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user