/// <summary> /// Private constructor /// </summary> private Inequality(Label id, IneqType type, LinearFunction lhs, LpNumber rhs, bool negFlag) { this.Id = id; this.type = type; this.lhs = lhs; this.rhs = rhs; this.NegFlag = negFlag; }
/// <summary> /// Parses an inequality /// </summary> /// <param name="scanner"></param> /// <returns></returns> public static Inequality ParseInequality(Scanner scanner) { Token t; // Identifier Label id = Label.ParseLabel(scanner); // : t = scanner.nextToken(); if (t.Type != TokenType.Colon) { throw new Exception(": expected: " + t); } // lhs LinearFunction lhs = LinearFunction.ParseFunction(scanner); // type t = scanner.nextToken(); IneqType type; switch (t.Type) { case TokenType.Le: type = IneqType.Le; break; case TokenType.Ge: type = IneqType.Ge; break; case TokenType.Eq: type = IneqType.Eq; break; default: throw new Exception("<=, =>, or = expected: " + t); } // rhs t = scanner.nextToken(); if (t.Type != TokenType.Integer && t.Type != TokenType.Double) { throw new Exception("A number is expected: " + t); } LpNumber rhs = new LpNumber(decimal.Parse(t.StringValue)); return(new Inequality(id, type, lhs, rhs, false)); }
/// <summary> /// Multiplication /// </summary> /// <param name="ineq"></param> /// <param name="n"></param> /// <returns></returns> public static Inequality operator *(Inequality ineq, decimal n) { IneqType type = ineq.type; bool newNegFlag = ineq.NegFlag; if (n < 0) { newNegFlag = !newNegFlag; if (type == IneqType.Le) { type = IneqType.Ge; } else if (type == IneqType.Ge) { type = IneqType.Le; } } return(new Inequality(ineq.Id, type, ineq.lhs * n, ineq.rhs * n, newNegFlag)); }
/// <summary> /// Multiplication /// </summary> /// <param name="ineq"></param> /// <param name="n"></param> /// <returns></returns> public static Inequality operator *(Inequality ineq, decimal n) { IneqType type = ineq.type; bool newNegFlag = ineq.NegFlag; if (n < 0) { newNegFlag = !newNegFlag; if (type == IneqType.Le) type = IneqType.Ge; else if (type == IneqType.Ge) type = IneqType.Le; } return new Inequality(ineq.Id, type, ineq.lhs * n, ineq.rhs * n, newNegFlag); }
/// <summary> /// Parses an inequality /// </summary> /// <param name="scanner"></param> /// <returns></returns> public static Inequality ParseInequality(Scanner scanner) { Token t; // Identifier Label id = Label.ParseLabel(scanner); // : t = scanner.nextToken(); if (t.Type != TokenType.Colon) throw new Exception(": expected: " + t); // lhs LinearFunction lhs = LinearFunction.ParseFunction(scanner); // type t = scanner.nextToken(); IneqType type; switch (t.Type) { case TokenType.Le: type = IneqType.Le; break; case TokenType.Ge: type = IneqType.Ge; break; case TokenType.Eq: type = IneqType.Eq; break; default: throw new Exception("<=, =>, or = expected: " + t); } // rhs t = scanner.nextToken(); if (t.Type != TokenType.Integer && t.Type != TokenType.Double) throw new Exception("A number is expected: " + t); LpNumber rhs = new LpNumber(decimal.Parse(t.StringValue)); return new Inequality(id, type, lhs, rhs, false); }