Skip to content

Commit

Permalink
Improve error messages for LinearProgramming
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Mar 3, 2024
1 parent 8c36a7c commit d214088
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public static void initGeneralMessages() {
"listrp", "List or SparseArray or structured array expected at position `1` in `2`.", //
"locked", "Symbol `1` is locked.", //
"lowlen", "Required length `1` is smaller than maximum `2` of support of `3`.", //
"lpsnf", "No solution can be found that satisfies the constraints.", //
"lslc", "Coefficient matrix and target vector or matrix do not have the same dimensions.", //
"lstpat", "List or pattern matching a list expected at position `1` in `2`.", //
"lvlist", "Local variable specification `1` is not a List.", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import java.util.ArrayList;
import java.util.Collection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hipparchus.exception.MathIllegalArgumentException;
import org.hipparchus.exception.MathIllegalStateException;
import org.hipparchus.exception.MathRuntimeException;
import org.hipparchus.optim.PointValuePair;
import org.hipparchus.optim.linear.LinearConstraint;
Expand All @@ -15,6 +14,7 @@
import org.hipparchus.optim.linear.Relationship;
import org.hipparchus.optim.linear.SimplexSolver;
import org.hipparchus.optim.nonlinear.scalar.GoalType;
import org.matheclipse.core.basic.Config;
import org.matheclipse.core.eval.Errors;
import org.matheclipse.core.eval.EvalEngine;
import org.matheclipse.core.eval.interfaces.AbstractFunctionEvaluator;
Expand Down Expand Up @@ -77,7 +77,6 @@
* </pre>
*/
public class LinearProgramming extends AbstractFunctionEvaluator {
private static final Logger LOGGER = LogManager.getLogger();

public LinearProgramming() {
super();
Expand Down Expand Up @@ -138,13 +137,15 @@ public IExpr numericEval(final IAST ast, EvalEngine engine) {
if (sn != null) {
constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, sn.doubleValue()));
} else {
LOGGER.log(engine.getLogLevel(), "Numeric vector or number expected!");
return F.NIL;
// `1`.
return Errors.printMessage(ast.topHead(), "error",
F.list(F.$str("Numeric vector or number expected.")), engine);
}
}
} else {
LOGGER.log(engine.getLogLevel(), "Numeric vector expected!");
return F.NIL;
// `1`.
return Errors.printMessage(ast.topHead(), "error",
F.list(F.$str("Numeric vector expected.")), engine);
}
}
SimplexSolver solver = new SimplexSolver();
Expand All @@ -156,11 +157,24 @@ public IExpr numericEval(final IAST ast, EvalEngine engine) {
}
}
} catch (MathIllegalArgumentException miae) {
if (Config.DEBUG) {
miae.printStackTrace();
}
// `1`.
return Errors.printMessage(ast.topHead(), "error", F.list(F.$str(miae.getMessage())),
engine);
return Errors.printMessage(ast.topHead(), "error", F.list(F.$str(miae.getMessage())), engine);
} catch (MathIllegalStateException mise) {
if (Config.DEBUG) {
mise.printStackTrace();
}
// No solution can be found that satisfies the constraints.
return Errors.printMessage(ast.topHead(), "lpsnf", F.CEmptyList, engine);
} catch (MathRuntimeException mre) {
LOGGER.log(engine.getLogLevel(), ast.topHead(), mre);
if (Config.DEBUG) {
mre.printStackTrace();
}
// `1`.
return Errors.printMessage(ast.topHead(), "error", F.list(F.$str(mre.getMessage())), engine);

}
return F.NIL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ public void testLinearProgramming() {
"{0.6,0.0}");
}

@Test
public void testLinearProgrammingNoFeasibleSolution() {
// message: LinearProgramming: No solution can be found that satisfies the constraints.
check("LinearProgramming({-2, -1,-2}, {{1, 2,0},{3,2,1},{0,1,0}}, {{5, -1},{4,-1},{3,1}})", //
"LinearProgramming({-2,-1,-2},{{1,2,0},{3,2,1},{0,1,0}},{{5,-1},{4,-1},{3,1}})");
}

@Test
public void testSystem803() {
// see
// http://google-opensource.blogspot.com/2009/06/introducing-apache-commons-math.html
check(
"LinearProgramming({-2, 1, -5}, {{1, 2, 0},{3, 2, 0},{0,1,0},{0,0,1}}, {{6,-1},{12,-1},{0,1},{1,0}})",
"{4.0,0.0,1.0}");
// message: LinearProgramming: inconsistent dimensions: 2 != 3.
check("LinearProgramming({-2, 1, -5}, {{1, 2},{3, 2},{0,1}},{{6,-1},{12,-1},{0,1}})", //
"LinearProgramming({-2,1,-5},{{1,2},{3,2},{0,1}},{{6,-1},{12,-1},{0,1}})");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3717,19 +3717,6 @@ public void testSystem422() {
// check("Plot(Sin(x),{x,0,10})", "");
// };

@Test
public void testSystem803() {
// see
// http://google-opensource.blogspot.com/2009/06/introducing-apache-commons-math.html
check(
"LinearProgramming({-2, 1, -5}, {{1, 2, 0},{3, 2, 0},{0,1,0},{0,0,1}}, {{6,-1},{12,-1},{0,1},{1,0}})",
"{4.0,0.0,1.0}");
// creates unbounded error
// check("LinearProgramming({-2, 1, -5}, {{1, 2},{3, 2},{0,1}},
// {{6,-1},{12,-1},{0,1}})",
// "{4.0,0.0,1.0}");
}

@Test
public void testSystem804() {
// check("Simplify(x*(x^2.00))", "x^3.0");
Expand Down

0 comments on commit d214088

Please sign in to comment.