-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 Intitial creation of AssignmentProblem class
- Loading branch information
Stefan Katzensteiner
committed
Jul 7, 2024
1 parent
783ee3c
commit 5332c2e
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.gocklkatz.m12s; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class AssignmentProblem { | ||
|
||
List<List<Integer>> matrix; | ||
|
||
public AssignmentProblem(List<List<Integer>> matrix) { | ||
this.matrix = matrix; | ||
} | ||
|
||
public List<Integer> solveAP() { | ||
List<Integer> sequence = Arrays.asList(0, 1, 2, 3); | ||
List<List<Integer>> permutations = Combinatorics.permutations(sequence); | ||
|
||
List<Integer> bestSolution = new ArrayList<>(); | ||
int zfBest = Integer.MAX_VALUE; | ||
for(List<Integer> solution : permutations) { | ||
int zfVal = calcZf(solution); | ||
if(zfVal < zfBest) { | ||
zfBest = zfVal; | ||
bestSolution = solution; | ||
} | ||
} | ||
|
||
return bestSolution; | ||
} | ||
|
||
private int calcZf(List<Integer> solution) { | ||
int total = 0; | ||
for(int i=0; i<4; i++) { | ||
total += matrix.get(i).get(solution.get(i)); | ||
} | ||
return total; | ||
} | ||
|
||
public static void main(String[] args) { | ||
List<List<Integer>> matrix = new ArrayList<>(); | ||
matrix.add(List.of(9, 2, 7, 8)); | ||
matrix.add(List.of(6, 4, 3, 7)); | ||
matrix.add(List.of(5, 8, 1, 8)); | ||
matrix.add(List.of(7, 6, 9, 4)); | ||
|
||
AssignmentProblem ap = new AssignmentProblem(matrix); | ||
List<Integer> bestSolution = ap.solveAP(); | ||
System.out.println(bestSolution); | ||
} | ||
} |