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
| import java.util.*;
import java.io.*;
/** Simulates Manning & Schützes "Crazy Soft Drink Machine" -
Generates an output sequence of length t */
public class soda {
/** initial state probabilities */
private int pi_min[];
private int pi_max[];
/** transition probabilities */
private int a_min[][];
private int a_max[][];
/** output probabilities */
private int b_min[][];
private int b_max[][];
/** output symbols */
private static final int cola = 0;
private static final int ice_t = 1;
private static final int lem = 2;
/** states */
private static final int cola_pref = 0;
private static final int ice_t_pref = 1;
/** initialization of the HMM as in M&S, page 321 */
private void init() {
pi_min = new int[2];
pi_max = new int[2];
pi_min[cola_pref] = 1;
pi_max[cola_pref] = 1000;
pi_min[ice_t_pref] = 0;
pi_max[ice_t_pref] = 0;
a_min = new int[2][2];
a_max = new int[2][2];
a_min[cola_pref][cola_pref] = 1;
a_max[cola_pref][cola_pref] = 700;
a_min[cola_pref][ice_t_pref] = 701;
a_max[cola_pref][ice_t_pref] = 1000;
a_min[ice_t_pref][cola_pref] = 1;
a_max[ice_t_pref][cola_pref] = 500;
a_min[ice_t_pref][ice_t_pref] = 501;
a_max[ice_t_pref][ice_t_pref] = 1000;
b_min = new int[2][3];
b_max = new int[2][3];
b_min[cola_pref][cola] = 1;
b_max[cola_pref][cola] = 600;
b_min[cola_pref][ice_t] = 601;
b_max[cola_pref][ice_t] = 700;
b_min[cola_pref][lem] = 701;
b_max[cola_pref][lem] = 1000;
b_min[ice_t_pref][cola] = 1;
b_max[ice_t_pref][cola] = 100;
b_min[ice_t_pref][ice_t] = 101;
b_max[ice_t_pref][ice_t] = 800;
b_min[ice_t_pref][lem] = 801;
b_max[ice_t_pref][lem] = 1000;
}
/** generation of output sequence */
private void gen_output(int t) {
try {
PrintWriter pw = new PrintWriter(new FileWriter("crazysoda.seq"));
pw.println(t);
int rnd_number = ((int) (Math.random() * 1000)) + 1;
int state;
for (state = 0; state < 2; state++) {
if ((pi_min[state] <= rnd_number) && (pi_max[state] >= rnd_number))
break;
}
for (int i = 0; i < t; i++) {
rnd_number = ((int) (Math.random() * 1000)) + 1;
for (int symb = 0; symb < 3; symb++) {
if ((b_min[state][symb] <= rnd_number) && (b_max[state][symb]) >= rnd_number) {
printSymbol(symb, pw);
break;
}
}
rnd_number = ((int) (Math.random() * 1000)) + 1;
for (int newState = 0; newState < 2; newState++) {
if ((a_min[state][newState] <= rnd_number) && (a_max[state][newState] >= rnd_number)) {
state = newState;
break;
}
}
}
pw.println();
pw.close();
}
catch (IOException e) {
System.out.println("crazysoda.seq cannot be loaded.");
System.exit(0);
}
}
/** output of a Symbol */
private void printSymbol(int s, PrintWriter pw) {
pw.print(s);
}
/** Main program. Invoke with java soda <t>, where t is the length of
the sequence to be generated. */
public static void main(String argv[]) {
soda s = new soda();
s.init();
s.gen_output(Integer.parseInt(argv[0]));
}
} |
Partager