1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| import java.io.*;
import java.util.ArrayList;
//import org.gnu.glpk;
public class Read_and_write {
public static void Read() {
BufferedReader reader = null;
int compteur = 0;
final int nbLignesASauter = 15;
final int nbMax = 24;
try {
reader = new BufferedReader(new FileReader("C:\\herve.doc"));
String ligne;
while ((ligne = reader.readLine()) != null) {
compteur++;
if ((compteur > nbLignesASauter) && (compteur < nbMax)) {
// else {
String[] colonnes = ligne.split("\\s+");
for (int i = 0; i < colonnes.length; i++)
System.out.print(colonnes[i] + "\t ");
System.out.println("");
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
private static void tourner() {
String cmd[] = { "C:\\glpsol", "--math", "antichaine.mod", "--output", "antichaine.txt" };
try {
Process process = Runtime.getRuntime().exec(cmd);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ArrayList<String> tableau = new ArrayList<String>();
// String tab[] = { "01111011","111","011", "111", "101", "110111",
// "15", "21", "22", "23",
// "24", "25", "31", "32", "33", "34", "41", "42", "43", "44",
// "45", "51", "52", "53", " 54", " 55" };
String[] tab = { "00001111111010110001"};
int p = 4;
for (int i = 0; i < tab.length; i++) {
tableau.add(tab[i]);
}
for (String elt : tableau) {
String chemin;
chemin = "C:" + File.separator + "antichaine.mod";
File fichier = new File(chemin);
try {
FileWriter writer = new FileWriter(fichier);
BufferedWriter output = new BufferedWriter(writer);
try {
String vec = elt;
output.write("# Decision Variables\n");
output.write("var lambda, >=0;\n");
output.write("var w{1.." + p + "}, >=0;\n\n");
output.write("#Objective function\n");
//output.write("printf\"Fmin: %d\", " + vec + "; \n");
output.write("minimize obj: lambda;\n\n");
output.write("#Constraints\n");
int compt = 0;
int cas = 0;
int po = 0;
int nb = (vec.length() / p);
while (nb != 0) {
String result = "init";
int deb = (po * p);
po++;
int fin = (po * p);
String v = vec.substring(deb, fin);
int tailleV = v.length();
int tail = tailleV;
char charV;
compt = 0;
output.write("s.t. const" + cas + ": ");
cas++;
while (tailleV != 0) {
charV = v.charAt(--tailleV);
String sV = Character.toString(charV);
int iElt = Integer.parseInt(sV);
compt++;
String t = new String("w[");
String a = "+";
String b = "]";
if (compt <= tail) {
if ((iElt == 1) && (result == "init")) {
result = t + compt + b;
output.write(result);
} else if ((iElt == 1) && (result != "init")) {
output.write(a);
result = t + compt + b;
output.write(result);
}
}
}
if (result != "init") {
output.write(" >= lambda;\n");
} else {
output.write("lambda >= 0;\n");
}
nb--;
}
output.write("s.t. const" + 19 + ":\tsum{i in 1.." + p
+ "} w[i]= 1;\n\n");
output.write("#Solving this model\n");
output.write("solve;\n\n");
output.write("#Printing the optimal values:\n");
output.write("printf\"lambda: %f\", lambda; \n");
output.write("for {i in 1.." + p + "}\n");
output.write("{\n");
output.write("printf\"\t w[%d]: %f\", i, w[i]; \n");
output.write("}\n");
output.write("end;\n");
} finally {
output.close();
writer.close();
}
} catch (Exception e) {
System.out.println("Impossible de creer le fichier");
e.printStackTrace();
}
tourner();
}
}
} |
Partager