-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of QAP Problem with the use of Simulated Annealing Algorithm #17
Open
Meropi
wants to merge
3
commits into
MH4J:develop
Choose a base branch
from
Meropi:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
MetaHeuristics4Java-Examples/src/main/java/de/mh4j/examples/qap/model/Facility.java
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,46 @@ | ||
package de.mh4j.examples.qap.model; | ||
|
||
import java.util.List; | ||
import java.util.ArrayList; | ||
|
||
|
||
|
||
|
||
|
||
public class Facility { | ||
|
||
|
||
public final String name; | ||
|
||
List<Integer> facilitiesCosts=new ArrayList<Integer>(); | ||
|
||
List<String> facilitiesNames=new ArrayList<String>(); | ||
|
||
static List<Facility> facilities=new ArrayList<Facility>(); | ||
|
||
|
||
|
||
|
||
|
||
public Facility (String name, List<Integer> costs, List<String> facilities_names){ | ||
|
||
this.name=name; | ||
for (int i=0; i<costs.size(); i++){ | ||
|
||
this.facilitiesCosts.add(costs.get(i)); | ||
|
||
this.facilitiesNames.add(facilities_names.get(i)); | ||
|
||
|
||
|
||
} | ||
|
||
facilities.add(this); | ||
|
||
|
||
} | ||
|
||
|
||
|
||
|
||
} |
31 changes: 31 additions & 0 deletions
31
MetaHeuristics4Java-Examples/src/main/java/de/mh4j/examples/qap/model/Location.java
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,31 @@ | ||
package de.mh4j.examples.qap.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Location{ | ||
|
||
|
||
public final String locationName; | ||
|
||
List<Integer> distances=new ArrayList<Integer>(); | ||
List<String> distancesNames=new ArrayList<String>(); | ||
|
||
static List<Location> locations=new ArrayList<Location>(); | ||
|
||
public Location (String locationName, List<Integer> distances, List<String> distancesNames){ | ||
|
||
this.locationName=locationName; | ||
|
||
for (int i=0; i<distances.size(); i++){ | ||
|
||
this.distances.add(distances.get(i)); | ||
|
||
this.distancesNames.add(distancesNames.get(i)); | ||
|
||
} | ||
|
||
locations.add(this); | ||
} | ||
|
||
} |
112 changes: 112 additions & 0 deletions
112
MetaHeuristics4Java-Examples/src/main/java/de/mh4j/examples/qap/model/Qap.java
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,112 @@ | ||
|
||
|
||
package de.mh4j.examples.qap.model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
import de.mh4j.solver.Solution; | ||
|
||
|
||
/** | ||
* | ||
* The quadratic assignment problem (QAP) is one of fundamental | ||
* combinatorial optimization problems in the branch of optimization | ||
* or operations research in mathematics, from the category of | ||
* the facilities-location problems. | ||
* | ||
* | ||
* **/ | ||
|
||
|
||
public class Qap implements Solution<Qap>{ | ||
|
||
|
||
public List<String> solution= new ArrayList<>();; | ||
public int costs=0; | ||
|
||
public Qap (List<String> solution) { | ||
|
||
for (int i=0; i<solution.size(); i++){ | ||
|
||
this.solution.add(solution.get(i)); | ||
|
||
|
||
} | ||
|
||
calculateCosts(this.solution); | ||
} | ||
|
||
public Qap(Qap original) { | ||
|
||
this.solution = new ArrayList<>(original.solution); | ||
this.costs = original.costs; | ||
} | ||
|
||
/** | ||
* This method #calculateCosts calculates this mathematical expression | ||
* __n__ __n__ | ||
* \ \ | ||
* \ \ Fij*Dp(i)p(j) for minimizing or maximizing the costs | ||
* / / where F are the facilities' flows and D the | ||
* /____ /____ the distances | ||
* i=1 j=1 | ||
* | ||
* | ||
* for example in case of 3 facilities(1,2,3) and 3 locations(A,B,C) this method would calculate: | ||
* f12*dAB+f13*d��+f21*dBA+f23*dB�+ f31*d��+f32*d�� | ||
* | ||
* | ||
* */ | ||
|
||
public void calculateCosts (List<String> solution){ | ||
|
||
costs=0; | ||
|
||
int k=0; | ||
int m; | ||
|
||
for(int i=0; i<solution.size(); i++){ | ||
|
||
for(int j=0; j<Facility.facilities.size(); j++){ | ||
|
||
if (solution.get(i).equals(Facility.facilities.get(j).name)){ | ||
|
||
m=0; | ||
|
||
for(int l=0; l<solution.size(); l++ ){ | ||
|
||
for(int o=0; o<Facility.facilities.get(j).facilitiesNames.size(); o++){ | ||
|
||
if(solution.get(l).equals(Facility.facilities.get(j).facilitiesNames.get(o))){ | ||
|
||
|
||
k=Facility.facilities.get(j).facilitiesCosts.get(o); | ||
|
||
costs=costs+k*Location.locations.get(i).distances.get(m); | ||
m++; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
public int getCosts() { | ||
|
||
return costs; | ||
} | ||
|
||
@Override | ||
public boolean isBetterThan(Qap otherSolution) { | ||
|
||
return otherSolution.getCosts() < costs; | ||
} | ||
|
||
|
||
|
||
} |
23 changes: 23 additions & 0 deletions
23
MetaHeuristics4Java-Examples/src/main/java/de/mh4j/examples/qap/solver/QapCoolingScheme.java
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,23 @@ | ||
package de.mh4j.examples.qap.solver; | ||
|
||
import de.mh4j.examples.qap.model.Qap; | ||
import de.mh4j.solver.simulatedAnnealing.AbstractCoolingScheme; | ||
|
||
public class QapCoolingScheme extends AbstractCoolingScheme<Qap> { | ||
|
||
@Override | ||
protected double getInitialTemperature() { | ||
return 90; | ||
} | ||
|
||
@Override | ||
protected int getInitialEpochLength() { | ||
return 50; | ||
} | ||
|
||
@Override | ||
protected double decreaseTemperature(double currentTemperature) { | ||
return currentTemperature * 0.5; | ||
} | ||
|
||
} |
94 changes: 94 additions & 0 deletions
94
...4Java-Examples/src/main/java/de/mh4j/examples/qap/solver/SimulatedAnnealingQapSolver.java
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,94 @@ | ||
package de.mh4j.examples.qap.solver; | ||
|
||
|
||
|
||
|
||
|
||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import de.mh4j.examples.qap.model.Qap; | ||
import de.mh4j.solver.simulatedAnnealing.AbstractSimulatedAnnealingSolver; | ||
import de.mh4j.solver.termination.StagnationTermination; | ||
import de.mh4j.solver.termination.StepCountTermination; | ||
|
||
|
||
public class SimulatedAnnealingQapSolver extends AbstractSimulatedAnnealingSolver<Qap> { | ||
|
||
static List<String> locs=new ArrayList<String>(); | ||
|
||
public SimulatedAnnealingQapSolver() { | ||
super(new QapCoolingScheme()); | ||
|
||
|
||
|
||
addTerminationCondition(new StepCountTermination(this, 50)); | ||
addTerminationCondition(new StagnationTermination(this, 5)); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
@Override | ||
protected Qap createRandomNeighbor() { | ||
|
||
Qap neighbor=new Qap(currentSolution); | ||
|
||
return createNeighborFromSwap(neighbor); | ||
|
||
|
||
|
||
} | ||
|
||
|
||
private Qap createNeighborFromSwap(Qap neighbor) { | ||
|
||
int randomIndex1 = randomizer.nextInt(neighbor.solution.size()-1); | ||
int randomIndex2 = randomizer.nextInt(neighbor.solution.size()-1); | ||
|
||
while (randomIndex1==randomIndex2){ | ||
|
||
randomIndex2 = randomizer.nextInt(neighbor.solution.size()-1); | ||
|
||
} | ||
|
||
String element1=neighbor.solution.get(randomIndex1); | ||
String element2=neighbor.solution.get(randomIndex2); | ||
|
||
neighbor.solution.remove(randomIndex1); | ||
neighbor.solution.add(randomIndex1,element2); | ||
|
||
neighbor.solution.remove(randomIndex2); | ||
|
||
|
||
neighbor.solution.add(randomIndex2,element1); | ||
|
||
log.trace("Created neighbor from SWAP :"); | ||
|
||
for (int i=0; i<neighbor.solution.size(); i++){ | ||
log.trace(neighbor.solution.get(i)); | ||
} | ||
|
||
neighbor.calculateCosts(neighbor.solution); | ||
|
||
return neighbor; | ||
|
||
} | ||
|
||
|
||
|
||
protected Qap createInitialSolution(){ | ||
|
||
|
||
|
||
Qap qap1=new Qap(locs); | ||
|
||
return qap1; | ||
|
||
} | ||
|
||
|
||
} |
71 changes: 71 additions & 0 deletions
71
MetaHeuristics4Java-Examples/src/test/java/de/mh4j/examples/qap/model/FacilityTest.java
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,71 @@ | ||
package de.mh4j.examples.qap.model; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
|
||
import java.util.List; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class FacilityTest { | ||
|
||
@Test | ||
public void testCreate() { | ||
|
||
String facilityName1="1"; | ||
String facilityName2="2"; | ||
String facilityName3="3"; | ||
String facilityName4="4"; | ||
|
||
List<Integer> cost_list1=new ArrayList<Integer>(); | ||
List<Integer> cost_list2=new ArrayList<Integer>(); | ||
List<Integer> cost_list3=new ArrayList<Integer>(); | ||
List<Integer> cost_list4=new ArrayList<Integer>(); | ||
|
||
cost_list1.add(8); | ||
cost_list1.add(1); | ||
cost_list1.add(5); | ||
|
||
cost_list2.add(8); | ||
cost_list2.add(1); | ||
cost_list2.add(1); | ||
|
||
cost_list3.add(1); | ||
cost_list3.add(1); | ||
cost_list3.add(2); | ||
|
||
cost_list4.add(5); | ||
cost_list4.add(1); | ||
cost_list4.add(2); | ||
|
||
List<String> names1=new ArrayList<String>(); | ||
List<String> names2=new ArrayList<String>(); | ||
List<String> names3=new ArrayList<String>(); | ||
List<String> names4=new ArrayList<String>(); | ||
|
||
names1.add("2"); | ||
names1.add("3"); | ||
names1.add("4"); | ||
|
||
names2.add("1"); | ||
names2.add("3"); | ||
names2.add("4"); | ||
|
||
names3.add("1"); | ||
names3.add("2"); | ||
names3.add("4"); | ||
|
||
names4.add("1"); | ||
names4.add("2"); | ||
names4.add("3"); | ||
|
||
|
||
|
||
|
||
new Facility(facilityName1, cost_list1, names1); | ||
new Facility(facilityName2, cost_list2, names2); | ||
new Facility(facilityName3, cost_list3, names3); | ||
new Facility(facilityName4, cost_list4, names4); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non UTF-8 characters. Travis fails to build because of this.