1.パラメタ設定:人気度、拠点利用コスト、コスト制限
2.Knapsackで拠点算出->DBに登録
登録用テーブル
knapsacks
elements
3.最適化計算->結果をテーブルに格納
価値:人気
重さ:拠点使用コスト
$ sudo vi Knapsack02.java
$ sudo javac Knapsack02.java -cp /opt/localsolver_5_5/bin/localsolver.jar
$ sudo java -cp /Users/mizuno/Desktop/work/mysql-connector-java-5.1.21-bin.jar:/opt/localsolver_5_5/bin/localsolver.jar:. -Djava.library.path=/opt/localsolver_5_5/bin/ Knapsack02
[Knapsack02.java]
********** Knapsack02.java **********/
import java.sql.*;
import java.util.*;
import java.io.*;
import localsolver.*;
public class Knapsack02 {
/* Number of items. */
int nbItems;
/* Items properties. */
int[] weights;
int[] values;
public int[] getValues(){ return this.values;}
public int[] getWeights(){ return this.weights;}
/* Knapsack bound */
int knapsackBound;
/* Solver. */
LocalSolver localsolver;
/* LS Program variables. */
LSExpression[] x;
/* Solutions (classes at each position). */
List solutions;
public Knapsack02(int nbItems, int knapsackBound, MySQL mysql){
ResultSet rs = mysql.selectFacilities();
this.nbItems = nbItems;
weights = new int[nbItems];
values = new int[nbItems];
this.knapsackBound = knapsackBound;
int index = 0;
try {
while(rs.next()){
values[index] = rs.getInt("popularity");
weights[index] = rs.getInt("cost");
index++;
}
}catch (SQLException e) {
e.printStackTrace();
}
}
/* Reads instance data. */
void readInstance(String fileName) {
try {
Scanner input = new Scanner(new File(fileName));
nbItems = input.nextInt();
weights = new int[nbItems];
for (int i = 0; i < nbItems; i++) {
weights[i] = input.nextInt();
}
values = new int[nbItems];
for (int i = 0; i < nbItems; i++) {
values[i] = input.nextInt();
}
knapsackBound = input.nextInt();
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
void solve(int limit) {
try {
/* Declares the optimization model. */
localsolver = new LocalSolver();
LSModel model = localsolver.getModel();
x = new LSExpression[nbItems];
System.out.println("nbItems="+nbItems);
System.out.println("knapsackBound="+knapsackBound);
// boolean variables x[i]
for (int i = 0; i < nbItems; i++) {
x[i] = model.boolVar();
x[i].setName("x[" + i + "]");
}
// weight constraint
LSExpression weightSum = model.sum();
for (int i = 0; i < nbItems; i++) {
LSExpression itemWeight = model.prod(x[i],weights[i]);
weightSum.addOperand(itemWeight);
}
model.constraint(model.leq(weightSum,knapsackBound));
// maximize value
LSExpression valueSum = model.sum();
for (int i = 0; i < nbItems; i++) {
LSExpression itemValue = model.prod(x[i],values[i]);
valueSum.addOperand(itemValue);
}
model.maximize(valueSum);
model.close();
/* Parameterizes the solver. */
LSPhase phase = localsolver.createPhase();
phase.setTimeLimit(limit);
localsolver.solve();
solutions = new ArrayList();
for (int i = 0; i < nbItems; i++)
if (x[i].getValue() == 1)
solutions.add(i);
} catch (LSException e) {
System.out.println("LSException:" + e.getMessage());
System.exit(1);
}
}
/* Writes the solution in a file */
void writeSolution(String fileName, MySQL mysql) {
try {
BufferedWriter output = new BufferedWriter(new FileWriter(fileName));
int idx[] = new int[solutions.size()];
double obj = 0;
for (int i = 0; i < solutions.size(); ++i){
output.write(solutions.get(i) + " ");
idx[i] = solutions.get(i);
obj += values[solutions.get(i)];
}
mysql.insertKnapsack(solutions.size(),obj);
mysql.insertElement(idx);
output.write("\n");
output.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
int id = 1001;
String outputFile = "result.txt";
MySQL mysql = new MySQL(id);
Knapsack02 model = new Knapsack02(42, 2000, mysql);
//model.readInstance(instanceFile);
String strTimeLimit = "10";
int values[] = model.getValues();
int weights[] = model.getWeights();
for(int i = 0; i < 42; i++) System.out.print(weights[i]+" ");
System.out.println("");
for(int i = 0; i < 42; i++) System.out.print(values[i]+" ");
System.out.println("");
model.solve(Integer.parseInt(strTimeLimit));
if(outputFile != null) {
model.writeSolution(outputFile, mysql);
}
}
}
0 件のコメント:
コメントを投稿