Skip to content

Commit e99111d

Browse files
author
khashab2
committed
fixing a couple of bugs; tests should pass
1 parent 1455ea3 commit e99111d

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/infer/OJalgoHook.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ public void setMaximize(boolean d) {
108108

109109
public int addBooleanVariable(double c) {
110110
numvars ++;
111-
Variable var = Variable.makeBinary(Integer.toString(numvars));
111+
Variable var = Variable.makeBinary(Integer.toString(numvars)).weight(c);
112112
model.addVariable(var);
113-
objectiveFunction.set(var, c);
114113
return numvars-1;
115114
}
116115

@@ -120,10 +119,10 @@ public int[] addDiscreteVariable(double[] c) {
120119
while (ind < c.length) {
121120
double coef = c[ind];
122121
numvars ++;
123-
Variable var = Variable.make(Integer.toString(numvars));
122+
Variable var = Variable.make(Integer.toString(numvars)).weight(coef);
124123
var.isInteger();
125124
model.addVariable(var);
126-
objectiveFunction.set(var, coef);
125+
// objectiveFunction.set(var, coef);
127126
varIndices[ind] = numvars-1;
128127
ind++;
129128
}
@@ -136,10 +135,10 @@ public int[] addDiscreteVariable(Score[] c) {
136135
while (ind < c.length) {
137136
double coef = c[ind].score;
138137
numvars ++;
139-
Variable var = Variable.make(Integer.toString(numvars));
138+
Variable var = Variable.make(Integer.toString(numvars)).weight(coef);
140139
var.isInteger();
141140
model.addVariable(var);
142-
objectiveFunction.set(var, coef);
141+
// objectiveFunction.set(var, coef);
143142
varIndices[ind] = numvars -1;
144143
ind++;
145144
}
@@ -204,13 +203,7 @@ public boolean getBooleanValue(int index) {
204203
}
205204

206205
public double objectiveValue() {
207-
/** TODO(khashab2): the method `result.getValue();` doesn't return the right value. Make sure you're doing
208-
* the right thing.
209-
*/
210-
double obj = 0;
211-
for( Variable v : model.getVariables() )
212-
obj += objectiveFunction.get(v).doubleValue() * v.getValue().doubleValue();
213-
return obj;
206+
return result.getValue();
214207
}
215208

216209
public void reset() {
@@ -233,6 +226,8 @@ public void setTimeout(int limit) {
233226
}
234227

235228
public void printModelInfo() {
229+
236230
System.out.println(model.toString());
231+
System.out.println("objective: " + result.getValue());
237232
}
238233
}

lbjava/src/test/java/edu/illinois/cs/cogcomp/lbjava/JalgoHookTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public void testProgram1() throws Exception {
3030
e.printStackTrace();
3131
}
3232

33+
ojaHook.printModelInfo();
34+
3335
assertTrue(ojaHook.objectiveValue() == -2.0);
3436
assertTrue(ojaHook.getBooleanValue(0));
3537
assertTrue(ojaHook.getBooleanValue(1));
@@ -134,9 +136,10 @@ public void testProgram5() throws Exception {
134136
OJalgoHook ojaHook = new OJalgoHook();
135137
int[] varInds = new int[2];
136138

139+
double[] objCoefs = {1.5, 2.5};
137140
int i = 0;
138141
while (i< 2) {
139-
int x = ojaHook.addBooleanVariable(1.5);
142+
int x = ojaHook.addBooleanVariable(objCoefs[i]);
140143
varInds[i] = x;
141144
i++;
142145
}
@@ -155,7 +158,7 @@ public void testProgram5() throws Exception {
155158

156159
ojaHook.printModelInfo();
157160

158-
assertTrue(ojaHook.objectiveValue() == 3);
161+
assertTrue(ojaHook.objectiveValue() == 4);
159162
assertTrue(ojaHook.getBooleanValue(0));
160163
assertTrue(ojaHook.getBooleanValue(1));
161164
}
@@ -165,9 +168,10 @@ public void testProgram6() throws Exception {
165168
OJalgoHook ojaHook = new OJalgoHook();
166169
int[] varInds = new int[2];
167170

171+
double[] objCoefs = {1.5, 2.5};
168172
int i = 0;
169173
while (i< 2) {
170-
int x = ojaHook.addBooleanVariable(1.5);
174+
int x = ojaHook.addBooleanVariable(objCoefs[i]);
171175
varInds[i] = x;
172176
i++;
173177
}
@@ -186,7 +190,7 @@ public void testProgram6() throws Exception {
186190

187191
ojaHook.printModelInfo();
188192

189-
assertTrue(ojaHook.objectiveValue() == 1); // fails: should be 1
193+
assertTrue(ojaHook.objectiveValue() == 1.5); // fails: should be 1
190194
assertTrue(ojaHook.getBooleanValue(0)); // fails; should be true
191195
assertTrue(!ojaHook.getBooleanValue(1)); // fails; should be false
192196
}
@@ -196,9 +200,10 @@ public void testProgram7() throws Exception {
196200
OJalgoHook ojaHook = new OJalgoHook();
197201
int[] varInds = new int[2];
198202

203+
double[] objCoefs = {1.5, 2.5};
199204
int i = 0;
200205
while (i< 2) {
201-
int x = ojaHook.addBooleanVariable(1.5);
206+
int x = ojaHook.addBooleanVariable(objCoefs[i]);
202207
varInds[i] = x;
203208
i++;
204209
}
@@ -217,7 +222,7 @@ public void testProgram7() throws Exception {
217222

218223
ojaHook.printModelInfo();
219224

220-
assertTrue(ojaHook.objectiveValue() == 2); // fails
225+
assertTrue(ojaHook.objectiveValue() == 2.5); // fails
221226
assertTrue(!ojaHook.getBooleanValue(0)); // fails
222227
assertTrue(ojaHook.getBooleanValue(1)); // fails
223228
}

0 commit comments

Comments
 (0)