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.method.ahp.sort.phase.ranking</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<name>[bundle] Ranking</name>
</project>
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>flintstones.method.ahp.sort.phase.ranking</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>1779484362629</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,28 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Ranking
Bundle-SymbolicName: flintstones.method.ahp.sort.phase.ranking;singleton:=true
Bundle-Version: 1.0.0.qualifier
Automatic-Module-Name: flintstones.method.ahp.sort.phase.ranking
Bundle-RequiredExecutionEnvironment: JavaSE-11
Require-Bundle: flintstones.model.ahppreferences.service,
javax.inject,
flintstones.model.problemelement.service,
flintstones.entity.problemelement,
flintstones.entity.preferences.preferencecollection,
flintstones.entity.ahp,
org.apache.commons.math3,
flintstones.helper.data,
flintstones.entity.ahppreferences,
flintstones.model.ahpsort.profileassignment.service,
flintstones.entity.ahpsort.profileassignment,
flintstones.model.domain.service,
flintstones.entity.domain,
org.eclipse.e4.core.contexts,
org.eclipse.e4.core.di,
flintstones.entity.method.phase,
flintstones.entity.ahpsort.priority,
flintstones.helper.debug,
org.apache.commons.lang
Export-Package: flintstones.method.ahp.sort.phase.ranking,
flintstones.method.ahp.sort.phase.ranking.ahpcluster
@@ -0,0 +1,5 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
plugin.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="flintstones.phasemethod.extensionpoint">
<phase
uid="flintstones.method.ahp.sort.phase.ranking"
implementation="flintstones.method.ahp.sort.phase.ranking.AHPSortRankingModel">
</phase>
</extension>
</plugin>
@@ -0,0 +1,198 @@
package flintstones.method.ahp.sort.phase.ranking;
import java.util.Arrays;
import java.util.HashMap;
import javax.inject.Inject;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import flintstones.entity.ahp.AHPMatrix;
import flintstones.entity.method.phase.PhaseMethod;
import flintstones.entity.preferences.preferencecollection.PreferenceCollection;
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.SortingClass;
import flintstones.entity.problemelement.entities.SortingProfile;
import flintstones.helper.data.HashMatrix;
import flintstones.model.ahppreferences.service.IProblemPreferencesService;
import flintstones.model.ahpsort.profileassignment.service.IProfileAssignmentService;
import flintstones.model.domain.service.IDomainService;
import flintstones.model.problemelement.service.IProblemElementService;
/**
* The Class AHPSortRankingModel.
*/
public class AHPSortRankingModel extends PhaseMethod {
/** The pref service. */
@Inject
IProblemPreferencesService prefService;
/** The problem service. */
@Inject
IProblemElementService problemService;
/** The profile service. */
@Inject
IProfileAssignmentService profileService;
/** The domain service. */
@Inject
IDomainService domainService;
/** The context. */
@Inject
IEclipseContext context;
/** The db. */
HashMap<Expert, ExpertAHPSortModel> db = new HashMap<>();
/** The experts. */
ProblemElement[] experts;
/** The criteria. */
ProblemElement[] criteria;
/*
* (non-Javadoc)
*
* @see flintstones.phasemethod.PhaseMethod#getName()
*/
@Override
public String getName() {
return "AHPSort Ranking";
}
/**
* Execute.
*/
public void execute() {
exec();
export();
}
/**
* Export.
*/
private void export() {
exportData("expertModels", db);
}
/**
* Exec.
*/
private void exec() {
// Vars
experts = problemService.getAll(Expert.Type);
criteria = problemService.getAll(Criterion.Type);
for (ProblemElement e : experts)
execFor((Expert) e);
}
/**
* Exec the model for an expert.
*
* @param expert the e
*/
private void execFor(Expert expert) {
ExpertAHPSortModel emodel = new ExpertAHPSortModel(expert, profileService.getType().equals(SortingProfile.SortingProfileType.Central));
// Prepare
db.put(expert, emodel);
ContextInjectionFactory.inject(emodel, context);
// Weights
PreferenceCollection criterionCol = prefService.get(null, Expert.Type, Criterion.Type, Criterion.Type);
emodel.cacheCriterionWeightsFor(criterionCol);
// Priorities
PreferenceCollection altProfCol = prefService.get(Expert.Type, Criterion.Type, Alternative.Type,
SortingProfile.Type);
PreferenceCollection profProfCol = prefService.get(Expert.Type, Criterion.Type, SortingProfile.Type,
SortingProfile.Type);
for (ProblemElement c : criteria) {
AHPMatrix[] matrixesAltProf = altProfCol.getAHPMatrixesFor(expert, c);
AHPMatrix[] matrixesProfProf = profProfCol.getAHPMatrixesFor(expert, c);
AHPMatrix[] combinedMatrices = combineMatrices(matrixesAltProf, matrixesProfProf);
emodel.cacheLocalPriorities((Criterion) c, combinedMatrices);
}
emodel.cacheGlobalPriorities();
emodel.classify(getClasses());
}
private AHPMatrix[] combineMatrices(AHPMatrix[] matrixesAltProf, AHPMatrix[] matrixesProfProf) {
for (int i = 0; i < matrixesAltProf.length; i++) {
ProblemElement[] fil = matrixesProfProf[0].getRows();
ProblemElement[] col = matrixesProfProf[0].getCols();
for (ProblemElement problemElement : fil) {
for (ProblemElement problemElement2 : col) {
matrixesAltProf[i].set(problemElement, problemElement2,
matrixesProfProf[0].getIndex(problemElement, problemElement2));
}
}
}
return matrixesAltProf;
}
/**
* Gets the criterion weights for.
*
* @param currentExpert the current expert
* @return the criterion weights for
*/
public HashMap<ProblemElement, Double> getCriterionWeightsFor(Expert currentExpert) {
return db.get(currentExpert).getCriterionWeights();
}
/**
* Gets the alternative global priorities for.
*
* @param currentExpert the current expert
* @return the alternative global priorities for
*/
public HashMatrix<ProblemElement, Integer, Double> getAlternativeGlobalPrioritiesFor(Expert currentExpert) {
return db.get(currentExpert).getAlternativeGlobalPriorities();
}
/**
* Gets the profile global priorities for.
*
* @param currentExpert the current expert
* @return the profile global priorities for
*/
public HashMatrix<Integer, Alternative, Double> getProfileGlobalPrioritiesFor(Expert currentExpert) {
return db.get(currentExpert).getProfileGlobalPriorities();
}
/**
* Gets the classification for.
*
* @param currentExpert the current expert
* @return the classification for
*/
public HashMap<ProblemElement, SortingClass> getClassificationFor(Expert currentExpert) {
return db.get(currentExpert).getClassification();
}
public SortingClass[] getClasses() {
SortingClass[] classes = Arrays //
.stream(problemService.getAll(SortingClass.Type)) //
.map(k -> (SortingClass) k).toArray(SortingClass[]::new);
return classes;
}
}
@@ -0,0 +1,353 @@
/*
*
*/
package flintstones.method.ahp.sort.phase.ranking;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import flintstones.entity.ahp.AHPMatrix;
import flintstones.entity.ahpsort.priority.AHPSortPriority;
import flintstones.entity.ahpsort.priority.AHPSortPriorityManager;
import flintstones.entity.preferences.preferencecollection.PreferenceCollection;
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.SortingClass;
import flintstones.entity.problemelement.entities.SortingProfile;
import flintstones.helper.DoubleHelper;
import flintstones.helper.EigenHelper;
import flintstones.helper.data.HashMatrix;
import flintstones.helper.debug.DH;
/**
* The Class ExpertAHPSortModel.
*/
public class ExpertAHPSortModel {
// TODO REVISAR 06/06
boolean isOptimisticx = false;
/** The expert. */
Expert expert;
/** The criterions. */
Criterion[] criterions;
/** The profiles. */
SortingProfile[] profiles;
/** The alternatives. */
Alternative[] alternatives;
/** The criterion weights. */
HashMap<ProblemElement, Double> criterionWeights = new HashMap<>();
/** The local priorities. */
HashMap<Criterion, AHPSortPriorityManager> localPriorities = new HashMap<>();
/** The alternative global priorities. */
HashMatrix<ProblemElement, Integer, Double> alternativeG = new HashMatrix<>();
/** The profiles global priorities. */
HashMatrix<Integer, Alternative, Double> profileG = new HashMatrix<>();
/** The classification. */
HashMap<ProblemElement, SortingClass> classification = new HashMap<>();
boolean isCentralx = true;
/**
* Instantiates a new expert AHP sort model.
*
* @param expert the expert
*/
public ExpertAHPSortModel(Expert expert, boolean useCentralProfiles) {
this.expert = expert;
isCentralx = useCentralProfiles;
}
/**
* Cache criterion weights for the given Preference collection.
*
* @param col the col
*/
public void cacheCriterionWeightsFor(PreferenceCollection col) {
AHPMatrix matrix = col.getAHPMatrix(null, expert);
double[] values = EigenHelper.GetNormalizedEigenVector(matrix.getValues());
for (int i = 0; i < matrix.getCols().length; i++) {
ProblemElement criterion = matrix.getCols()[i];
Double value = values[i];
criterionWeights.put(criterion, value);
}
criterions = criterionWeights.keySet().stream().toArray(Criterion[]::new);
}
/**
* Cache local priorities.
*
* @param c the c
* @param matrixes the matrixes
*/
public void cacheLocalPriorities(Criterion c, AHPMatrix[] matrixes) {
AHPSortPriorityManager manager = new AHPSortPriorityManager();
for (AHPMatrix matrix : matrixes) {
double[] values = EigenHelper.GetNormalizedEigenVector(matrix.getValues());
ProblemElement[] col = matrix.getCols();
Alternative a = (Alternative) col[0];
SortingProfile[] profile = new SortingProfile[col.length - 1];
for (int i = 1; i < col.length; i++) {
profile[i - 1] = (SortingProfile) col[i];
}
double[] valoresPerfiles = new double[values.length - 1];
for (int i = 1; i < values.length; i++) {
valoresPerfiles[i - 1] = values[i];
}
AHPSortPriority prior = new AHPSortPriority(profile, a, values[0], valoresPerfiles);
manager.add(prior);
}
localPriorities.put(c, manager);
}
/**
* Cache global priorities.
*/
public void cacheGlobalPriorities() {
profiles = localPriorities.values().iterator().next().getProfiles();
ProblemElement[] pes = localPriorities.values().iterator().next().getLeftElements();
alternatives = Arrays.stream(pes).map(k -> (Alternative) k).toArray(Alternative[]::new);
cacheAlternativesGlobalPriorities();
cacheProfileGlobalPriorities();
}
/**
* Cache profile global priorities.
*/
private void cacheProfileGlobalPriorities() {
for (Alternative alternative : alternatives) {
for (Integer index = 0; index < profiles.length; index++) {
double wGlobalProf = 0.0;
for (Criterion c : criterions) {
AHPSortPriorityManager manager = localPriorities.get(c);
double criterionWeight = criterionWeights.get(c);
double localPro = manager.get(alternative, 0).getProfilePriority()[index];
double wLocalPro = localPro * criterionWeight;
wGlobalProf += wLocalPro;
profileG.put(index, alternative, wGlobalProf);
}
}
}
}
/**
* Cache alternatives global priorities.
*/
private void cacheAlternativesGlobalPriorities() {
for (Alternative alternative : alternatives) {
double wGlobalAlt = 0.0;
for (Criterion c : criterions) {
AHPSortPriorityManager manager = localPriorities.get(c);
double criterionWeight = criterionWeights.get(c);
double localAlt = manager.get(alternative, 0).getAlternativePriority();
double wLocalAlt = localAlt * criterionWeight;
wGlobalAlt += wLocalAlt;
}
alternativeG.put(alternative, 0, wGlobalAlt);
}
DH.out("Alternatives GLOBAL " + expert.getName());
DH.out(alternativeG.toString());
DH.l();
}
/**
* Gets the criterion weights.
*
* @return the criterion weights
*/
// G/S
public HashMap<ProblemElement, Double> getCriterionWeights() {
return this.criterionWeights;
}
/**
* Classify.
*
* @param domain the domain
*/
public void classify(SortingClass[] classes) {
if (!isCentralx) {
for (Alternative alternative : alternatives) {
int index = classifyByLimitingAlternativeV2(alternative);
SortingClass classs = classes[index];
classification.put(alternative, classs);
}
} else {
for (Alternative alternative : alternatives) {
int index = classifyByCentralAlternativeV2(alternative);
SortingClass classs = classes[index];
classification.put(alternative, classs);
}
}
DH.l(2);
}
private ArrayList<Double> getAlternativePrioritiesFor(Alternative a) {
Map<Integer, Double> altMap = alternativeG.get(a);
ArrayList<Double> alternativeHits = new ArrayList<>();
alternativeHits.addAll(altMap.values());
return alternativeHits;
}
private ArrayList<Double> getProfileLimitsFor(Alternative a) {
ArrayList<Double> profileLimits = (ArrayList<Double>) profileG.entrySet().stream().map(k -> k.getValue().get(a))
.collect(Collectors.toList());
return profileLimits;
}
private ArrayList<Double> getProfileCentersFor(Alternative a) {
ArrayList<Double> limits = getProfileLimitsFor(a);
limits.add(0, 0.0);
Double[] arr = limits.toArray(new Double[0]);
Double[] centrals = DoubleHelper.GetCentralPoints(arr);
return new ArrayList<Double>(Arrays.asList(centrals));
}
private int classifyByCentralAlternativeV2(Alternative alternative) {
ArrayList<Double> alternativeHits = getAlternativePrioritiesFor(alternative);
ArrayList<Double> profileCenters = getProfileCentersFor(alternative);
System.out.println(alternativeHits);
System.out.println(profileCenters);
double minDistance = Double.MAX_VALUE;
int minDistanceIndex = -1;
for (int i = 0; i < alternativeHits.size(); i++) {
double hit = alternativeHits.get(i);
for (int j = 0; j < profileCenters.size(); j++) {
double limit = profileCenters.get(j);
double diff = Math.abs(limit - hit);
if (diff < minDistance) { // WIN!!!
minDistance = diff;
minDistanceIndex = j;
} else if (diff == minDistance) { // TIE!!
if (!isOptimisticx) { // GET THE NEW ONE, ELSE KEEP THE OLD ONE
minDistanceIndex = j;
}
}
}
}
return minDistanceIndex;
}
private int classifyByLimitingAlternativeV2(Alternative alternative) {
ArrayList<Double> alternativeHits = getAlternativePrioritiesFor(alternative);
ArrayList<Double> profileLimits = getProfileLimitsFor(alternative);
System.out.println(profileLimits);
double hit = alternativeHits.get(0);
for (int i = 0; i < profileLimits.size(); i++) {
double limit = profileLimits.get(i);
if (hit > limit)
return i;
}
// No clasification, last one, bad.
return profileLimits.size();
}
/**
* Classify by limiting alternative.
*
* @param alternative the alternative
* @return the index
*/
/**
* Gets the alternative global priorities.
*
* @return the alternative global priorities
*/
public HashMatrix<ProblemElement, Integer, Double> getAlternativeGlobalPriorities() {
return alternativeG;
}
/**
* Gets the profile global priorities.
*
* @return the profile global priorities
*/
public HashMatrix<Integer, Alternative, Double> getProfileGlobalPriorities() {
return profileG;
}
/**
* Gets the classification.
*
* @return the classification
*/
public HashMap<ProblemElement, SortingClass> getClassification() {
return classification;
}
}
@@ -0,0 +1,88 @@
package flintstones.method.ahp.sort.phase.ranking.ahpcluster;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import flintstones.entity.problemelement.entities.ProblemElement;
public class AHPCluster implements Iterable<ProblemElement>{
LinkedList<ProblemElement> elements = new LinkedList<ProblemElement>();
LinkedList<Double> localPriorities = new LinkedList<>();
LinkedList<Double> joinedPriorities = new LinkedList<>();
public AHPCluster(ProblemElement[] pes) {
elements = new LinkedList<>(Arrays.asList(pes));
}
public void add(ProblemElement pe) {
elements.add(pe);
}
@Override
public Iterator<ProblemElement> iterator() {
return elements.iterator();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("<");
sb.append(elements.toString());
sb.append(">");
return sb.toString();
}
public ProblemElement[] getElements() {
return elements.toArray(new ProblemElement[0]);
}
public void setLocalPriorities(double[] arr) {
if(localPriorities.size() > 0)
throw new RuntimeException("No");
for(double item : arr)
localPriorities.add(item);
}
public Double[] getLocalPriorities() {
return localPriorities.toArray(new Double[0]);
}
public ProblemElement getFirstElement() {
return elements.get(0);
}
public Double getFirstPriority() {
return localPriorities.get(0);
}
public ProblemElement getLastElement() {
return elements.get(elements.size()-1);
}
public Double getLastPriority() {
return localPriorities.get(localPriorities.size()-1);
}
public void setJoinedPriorities(double[] arr) {
if(joinedPriorities.size() > 0)
throw new RuntimeException("No");
for(double item : arr)
joinedPriorities.add(item);
}
public Double[] getJoinedPriorities() {
if(joinedPriorities.size() == 0)
return localPriorities.toArray(new Double[0]);
return joinedPriorities.toArray(new Double[0]);
}
public int size() {
return elements.size();
}
}
@@ -0,0 +1,196 @@
package flintstones.method.ahp.sort.phase.ranking.ahpcluster;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import org.apache.commons.lang.ArrayUtils;
import flintstones.entity.problemelement.entities.ProblemElement;
import flintstones.helper.DoubleHelper;
public class AHPClusterer implements Iterable<AHPCluster> {
LinkedList<ProblemElement> originalElements = new LinkedList<>();
LinkedList<AHPCluster> clusters = new LinkedList<>();
LinkedHashMap<ProblemElement,Double> normalizedValues = new LinkedHashMap<>();
private int clusterSize = -1;
private int totalSize = -1;
public AHPClusterer(ProblemElement[] pes) {
originalElements = new LinkedList<>(Arrays.asList(pes));
int size = pes.length;
totalSize = size;
if (size <= 7) {
clusters.add(new AHPCluster(pes));
return;
}
int finalPos = -1;
int[] modules = getModules(size, 7);
int[] divisions = getDivisions(size, 7);
double[] modulesD = Arrays.stream(modules).asDoubleStream().toArray();
int[] minModules = DoubleHelper.FindMinIndexes(modulesD);
if (minModules.length > 1) {
int minDiv = Integer.MAX_VALUE;
int minDivPos = -1;
for (int i = 0; i < minModules.length; i++) {
int pos = minModules[i];
int div = divisions[pos];
if (div < minDiv) {
minDiv = div;
minDivPos = pos;
}
}
finalPos = minDivPos;
} else {
finalPos = minModules[0];
}
clusterSize = size / divisions[finalPos];
addClusters(clusterSize, pes);
}
private void addClusters(int size, ProblemElement[] pes) {
ArrayList<ProblemElement> elements = new ArrayList<>();
ProblemElement lastElement = null;
for (ProblemElement item : pes) {
if (lastElement != null && elements.size() == 0)
elements.add(lastElement);
elements.add(item);
if (elements.size() == size) {
clusters.add(new AHPCluster(elements.toArray(new ProblemElement[0])));
elements.clear();
}
lastElement = item;
}
// Add new cluster with remaining elements
if (elements.size() != 0)
clusters.add(new AHPCluster(elements.toArray(new ProblemElement[0])));
// Add the remaining elements to the last cluster. If any
// for(ProblemElement pe : elements)
// clusters.get(clusters.size()-1).add(pe);
}
private int[] getModules(int size, int maxSize) {
int[] values = new int[maxSize - 1];
for (int i = 2; i < maxSize + 1; i++)
values[i - 2] = size % i;
return values;
}
private int[] getDivisions(int size, int maxSize) {
int[] values = new int[maxSize - 1];
for (int i = 2; i < maxSize + 1; i++) {
values[i - 2] = size / i;
}
return values;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Clusters: \n clusterSize=" + clusterSize + " totalSize=" + totalSize);
for (AHPCluster c : clusters) {
sb.append("\n");
sb.append(c.toString());
}
return sb.toString();
}
@Override
public Iterator<AHPCluster> iterator() {
return clusters.iterator();
}
public LinkedHashMap<ProblemElement,Double> getNormalizedPriorities() {
if(normalizedValues.size() != 0)
return normalizedValues;
if(clusters.size() == 1) {
ProblemElement[] pes = clusters.get(0).getElements();
Double[] vals = clusters.get(0).getLocalPriorities();
LinkedHashMap<ProblemElement,Double> map = new LinkedHashMap<>();
for(int i = 0; i < pes.length; i++ ) {
map.put(pes[i], vals[i]);
}
return map;
}
for(int i = 0; i < clusters.size()-1; i+=1) {
AHPCluster a = clusters.get(i);
AHPCluster b = clusters.get(i+1);
join(a,b);
}
double[] weights = new double[originalElements.size()];
int w = 0;
AHPCluster firstCluster = clusters.get(0);
Double[] firstPrior = firstCluster.getJoinedPriorities();
for(int j = 0; j < firstPrior.length; j++) {
weights[w] = firstPrior[j];
w++;
}
for(int i = 1; i < clusters.size(); i++) {
Double[] prior = clusters.get(i).getJoinedPriorities();
for(int j = 1; j < prior.length; j++) {
weights[w] = prior[j];
w++;
}
}
Double[] nweights = ArrayUtils.toObject(DoubleHelper.Normalize(weights));
for(int i = 0; i < nweights.length; i++)
normalizedValues.put(originalElements.get(i), nweights[i]);
return normalizedValues;
}
public void join(AHPCluster top, AHPCluster bottom) {
double pA = top.getJoinedPriorities()[top.getJoinedPriorities().length - 1];
double pB = bottom.getFirstPriority();
double ratio = pA/pB;
double[] oldPriorities = ArrayUtils.toPrimitive(bottom.getLocalPriorities());
double[] newPriorities = new double[oldPriorities.length];
for(int i = 0; i < oldPriorities.length; i++)
newPriorities[i] = oldPriorities[i]*ratio;
bottom.setJoinedPriorities(newPriorities);
}
public int size() {
return clusterSize;
}
}