Skip to content

Commit

Permalink
[EXPERTS] Allow exact expert weighting (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
demid5111 committed May 9, 2022
1 parent 6954478 commit cfd07e8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 21 deletions.
21 changes: 5 additions & 16 deletions src/main/java/dss/lingvo/samples/TT2HFLTSCoordinator.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void processNumericSample() {

private void processWeightsGenerationSample() {
float[] expDistr = {0.8f, 0.2f};
float[] resVector = TTUtils.calculateWeightsVector(expDistr, 4);
float[] resVector = TTUtils.calculateWeightsVectorFromDistribution(expDistr, 4);
System.out.println(Arrays.toString(resVector));

float sum = 0f;
Expand Down Expand Up @@ -126,21 +126,11 @@ private void processMultiLevelAdvancedSample(File inputFile, File outputDirector
tt2HFLTSMHTWOWAMultiLevelOperator.transposeByAbstractionLevel(model.getAbstractionLevels().size(),
model.getAlternatives().size(), model.getExperts().size(), allByLevel);

int numExperts = model.getExpertWeightsRule().values().size();
float[] a = new float[numExperts];
float curMax = 0f;
for (Map.Entry<String, Float> e : model.getExpertWeightsRule().entrySet()) {
if (e.getKey().equals("1")) {
curMax = e.getValue();
break;
}
}
a[0] = curMax;
if (numExperts > 1) {
a[1] = 1 - curMax;
}
float[] weights = TTUtils.calculateWeights(model);
System.out.println("\n\n\n[MULTILEVEL] Weights: " + Arrays.toString(weights));
List<ArrayList<TT2HFLTS>> altToLevel = tt2HFLTSMHTWOWAMultiLevelOperator.aggregateByExpert(
model.getAbstractionLevels().size(), model.getAlternatives().size(), targetScaleSize, allByExpert, a);
model.getAbstractionLevels().size(), model.getAlternatives().size(), targetScaleSize, allByExpert,
weights);

List<TT2HFLTS> altVec = tt2HFLTSMHTWOWAMultiLevelOperator.aggregateFinalAltEst(targetScaleSize, altToLevel);

Expand All @@ -151,7 +141,6 @@ private void processMultiLevelAdvancedSample(File inputFile, File outputDirector
Path outputJSONFilePath = Paths.get(outputDirectory.toString(), "result.json");
TTJSONUtils.getInstance().writeResultToJSON(outputJSONFilePath, res);


// printing to console
List<Pair<String, TT2HFLTS>> resZippedVec = IntStream.range(0, altVec.size()).mapToObj(i -> new Pair<>(model.getAlternatives().get(i).getAlternativeID(), altVec.get(i))).collect(Collectors.toList());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public List<ArrayList<TT2HFLTS>> aggregateByExpert(int levelsSize,
int altSize,
int targetScaleSize,
List<ArrayList<ArrayList<TT2HFLTS>>> all,
float[] distribution) {
float[] weights) {
TT2HFLTSMHTWAOperator tt2HFLTSMHTWAOperator = new TT2HFLTSMHTWAOperator();
List<ArrayList<TT2HFLTS>> levelEstimates = new ArrayList<>();

Expand All @@ -82,7 +82,6 @@ public List<ArrayList<TT2HFLTS>> aggregateByExpert(int levelsSize,
// fill matrix
for (int levelIndex = 0; levelIndex < levelsSize; levelIndex++) {
for (int altIndex = 0; altIndex < altSize; altIndex++) {
float[] weights = TTUtils.calculateWeightsVector(distribution, all.get(levelIndex).get(altIndex).size());
List<TT2HFLTS> newSet = TTUtils.sortTT2HFLTS(all.get(levelIndex).get(altIndex), true);
TT2HFLTS aggRes = tt2HFLTSMHTWAOperator.calculate(newSet, weights, targetScaleSize);
levelEstimates.get(altIndex).set(levelIndex, aggRes);
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/dss/lingvo/utils/TTUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,37 @@ public static TTJSONOutputModel prepareAllResultsForJSON(List<TT2HFLTS> altOvera
return ttjsonOutputModel;
}


public static float[] calculateWeights(TTCommonInputModel model) {
float[] weights = new float[model.getExperts().size()];
if (model.getExpertWeights() != null) {
// if particular weights are defined in the task description there is no need to
// automatically assign weights
int index = 0;
for (TTExpertModel expert: model.getExperts()) {
weights[index++] = model.getExpertWeights().get(expert.getExpertID());
}

return weights;
}

float curMax = 0f;
for (Map.Entry<String, Float> e : model.getExpertWeightsRule().entrySet()) {
if (e.getKey().equals("1")) {
curMax = e.getValue();
break;
}
}

int numExperts = model.getExperts().size();
float[] distribution = new float[numExperts];
distribution[0] = curMax;
if (numExperts > 1) {
distribution[1] = 1 - curMax;
}
return calculateWeightsVectorFromDistribution(distribution, numExperts);
}

/**
* When calculating weights for the experts, we introduce a new formula
* E.g. distribution is [0.8, 0.2] and we have 3 experts.
Expand All @@ -218,7 +249,7 @@ public static TTJSONOutputModel prepareAllResultsForJSON(List<TT2HFLTS> altOvera
* @param numExperts - number of experts
* @return vector of weights
*/
public static float[] calculateWeightsVector(float[] distribution, int numExperts) {
public static float[] calculateWeightsVectorFromDistribution(float[] distribution, int numExperts) {
float[] res = new float[numExperts];
for (int i = 0; i < numExperts; i++) {
res[i] = calculateWeight(distribution[0], i, numExperts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class TTCommonInputModel {
private Map<String, List<TTExpertEstimationsModel>> estimations;
@JsonProperty("abstractionLevelWeights")
private Map<String, Float> abstractionLevelWeights;
@JsonProperty("expertWeights")
private Map<String, Float> expertWeights;
@JsonProperty("expertWeightsRule")
private Map<String, Float> expertWeightsRule;
@JsonProperty("criteriaWeightsPerGroup")
Expand Down Expand Up @@ -76,6 +78,14 @@ public void setExpertWeightsRule(Map<String, Float> expertWeightsRule) {
this.expertWeightsRule = expertWeightsRule;
}

public Map<String, Float> getExpertWeights() {
return expertWeights;
}

public void setExpertWeights(Map<String, Float> expertWeights) {
this.expertWeights = expertWeights;
}

public Map<String, List<Float>> getCriteriaWeightsPerGroup() {
return criteriaWeightsPerGroup;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void runOnceBeforeClass() throws IOException {
model.getExperts().size(),
allByLevel);

float []a = {0.8f, 0.2f};
float[] a = TTUtils.calculateWeights(model);
altToLevel = tt2HFLTSMHTWOWAMultiLevelOperator
.aggregateByExpert(model.getAbstractionLevels().size(),
model.getAlternatives().size(),
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/dss/lingvo/utils/TTUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void testSortTT2HFLTS3() throws Exception {
@Test
public void testWeightsDistribution() throws Exception {
float[] expDistr = {0.8f, 0.2f};
float[] resVector = TTUtils.calculateWeightsVector(expDistr, 4);
float[] resVector = TTUtils.calculateWeightsVectorFromDistribution(expDistr, 4);
float[] expV = {0.8f, 0.16f, 0.03199997f, 0.008000016f};
float sum = 0f;
for (int i = 0; i < resVector.length; i++) {
Expand Down

0 comments on commit cfd07e8

Please sign in to comment.