Skip to content
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
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);


}




}
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);
}

}
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��
Copy link
Member

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.

*
*
* */

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;
}



}
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;
}

}
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;

}


}
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);
}

}
Loading