/// <remarks> /// Default to strength ClStrength#Weak. /// </remarks> public ClSimplexSolver AddStay(ClVariable v) /* throws ExClRequiredFailure, ExClInternalError */ { AddStay(v, ClStrength.Weak, 1.0); return(this); }
/// <summary> /// Remove the edit constraint previously added. /// <param name="v">Variable to which the edit constraint was added before.</param> /// </summary> private void RemoveEditVar(ClVariable v) { ClEditInfo cei = _editVarMap[v]; ClConstraint cn = cei.Constraint; RemoveConstraint(cn); }
/// <summary> /// Add a stay of the given strength (default to ClStrength#Weak) /// of a variable to the tableau.. /// <param name="v"> /// Variable to add the stay constraint to. /// </param> /// </summary> public ClSimplexSolver AddStay(ClVariable v, ClStrength strength, double weight) /* throws ExClRequiredFailure, ExClInternalError */ { ClStayConstraint cn = new ClStayConstraint(v, strength, weight); return(AddConstraint(cn)); }
public static ClSimplexSolver AddPointStay(this ClSimplexSolver solver, ClVariable vx, ClVariable vy, double weight) { solver.AddStay(vx, ClStrength.Weak, weight); solver.AddStay(vy, ClStrength.Weak, weight); return(solver); }
public ClLinearInequality(ClVariable clv1, byte op_enum, ClVariable clv2, ClStrength strength) : this(clv1, op_enum, clv2, strength, 1.0) /* throws ExClInternalError */ { }
public ClEditOrStayConstraint(ClVariable var, ClStrength strength, double weight) : base(strength, weight) { _variable = var; _expression = new ClLinearExpression(_variable, -1.0, _variable.Value); }
/// <summary> /// Add an edit constraint for a variable with a given strength. /// <param name="v">Variable to add an edit constraint to.</param> /// <param name="strength">Strength of the edit constraint.</param> /// </summary> private void AddEditVar(ClVariable v, ClStrength strength) { try { AddConstraint(new ClEditConstraint(v, strength)); } catch (CassowaryRequiredFailureException) { // should not get this throw new CassowaryInternalException("Required failure when adding an edit variable"); } }
/// <summary> /// Suggest a new value for an edit variable. /// </summary> /// <remarks> /// The variable needs to be added as an edit variable and /// BeginEdit() needs to be called before this is called. /// The tableau will not be solved completely until after Resolve() /// has been called. /// </remarks> IEditContext IEditContext.SuggestValue(ClVariable v, double x) { ClEditInfo cei = _editVarMap[v]; if (cei == null) { throw new CassowaryException("SuggestValue for variable " + v + ", but var is not an edit variable\n"); } ClSlackVariable clvEditPlus = cei.ClvEditPlus; ClSlackVariable clvEditMinus = cei.ClvEditMinus; double delta = x - cei.PrevEditConstant; cei.PrevEditConstant = x; DeltaEditConstant(delta, clvEditPlus, clvEditMinus); return(this); }
public ClSimplexSolver AddVar(ClVariable v) /* throws ExClInternalError */ { if (!ContainsVariable(v)) { try { AddStay(v); } catch (CassowaryRequiredFailureException) { // cannot have a required failure, since we add w/ weak throw new CassowaryInternalException("Error in AddVar -- required failure is impossible"); } } return(this); }
public ClLinearInequality(ClVariable clv, byte op_enum, double val, ClStrength strength, double weight) : base(new ClLinearExpression(val), strength, weight) /* throws ExClInternalError */ { switch (op_enum) { case Cl.GEQ: _expression.MultiplyMe(-1.0); _expression.AddVariable(clv); break; case Cl.LEQ: _expression.AddVariable(clv, -1.0); break; default: // invalid operator throw new ExClInternalError("Invalid operator in ClLinearInequality constructor"); } }
public ClSimplexSolver AddPointStay(ClVariable vx, ClVariable vy, double weight) /* throws ExClRequiredFailure, ExClInternalError */ { AddStay(vx, ClStrength.Weak, weight); AddStay(vy, ClStrength.Weak, weight); return this; }
/// <remarks> /// Add an edit constraint with strength ClStrength#Strong. /// </remarks> public ClSimplexSolver AddEditVar(ClVariable v) { /* throws ExClInternalError */ return AddEditVar(v, ClStrength.Strong); }
public static ClLinearExpression Plus(ClVariable e1, double e2) { return (new ClLinearExpression(e1)).Plus(new ClLinearExpression(e2)); }
static bool Approx(double a, ClVariable clv) { return Approx(a, clv.Value); }
public static ClSimplexSolver AddPointStay(this ClSimplexSolver solver, ClVariable vx, ClVariable vy) { solver.AddPointStay(vx, vy, 1.0); return(solver); }
public bool ContainsVariable(ClVariable v) /* throws ExClInternalError */ { return ColumnsHasKey(v) || (RowExpression(v) != null); }
public ClPoint(double x, double y) { _clv_x = new ClVariable(x); _clv_y = new ClVariable(y); }
public ClEditConstraint(ClVariable clv) : base(clv) {}
public ClEditConstraint(ClVariable clv, ClStrength strength) : base(clv, strength) {}
public ClEditConstraint(ClVariable clv, ClStrength strength, double weight) : base(clv, strength, weight) {}
public static ClLinearExpression Plus(ClVariable e1, double e2) { return((new ClLinearExpression(e1)).Plus(new ClLinearExpression(e2))); }
static bool Approx(double a, ClVariable clv) { return(Approx(a, clv.Value)); }
public static bool Approx(ClVariable clv, double b) { return(Approx(clv.Value, b)); }
/// <summary> /// Add a stay of the given strength (default to ClStrength#Weak) /// of a variable to the tableau.. /// <param name="v"> /// Variable to add the stay constraint to. /// </param> /// </summary> public ClSimplexSolver AddStay(ClVariable v, ClStrength strength, double weight) /* throws ExClRequiredFailure, ExClInternalError */ { ClStayConstraint cn = new ClStayConstraint(v, strength, weight); return AddConstraint(cn); }
/// <summary> /// Suggest a new value for an edit variable. /// </summary> /// <remarks> /// The variable needs to be added as an edit variable and /// BeginEdit() needs to be called before this is called. /// The tableau will not be solved completely until after Resolve() /// has been called. /// </remarks> public ClSimplexSolver SuggestValue(ClVariable v, double x) /* throws ExClError */ { if (Trace) FnEnterPrint("SuggestValue(" + v + ", " + x + ")"); ClEditInfo cei = (ClEditInfo) _editVarMap[v]; if (cei == null) { Console.Error.WriteLine("SuggestValue for variable " + v + ", but var is not an edit variable\n"); throw new ExClError(); } int i = cei.Index; ClSlackVariable clvEditPlus = cei.ClvEditPlus; ClSlackVariable clvEditMinus = cei.ClvEditMinus; double delta = x - cei.PrevEditConstant; cei.PrevEditConstant = x; DeltaEditConstant(delta, clvEditPlus, clvEditMinus); return this; }
public ClPoint(ClVariable clv_x, ClVariable clv_y) { _clv_x = clv_x; _clv_y = clv_y; }
public ClLinearInequality(ClVariable clv, byte op_enum, double val) : this(clv, op_enum, val, ClStrength.Required, 1.0) /* throws ExClInternalError */ { }
public void SetXY(ClVariable clv_x, ClVariable clv_y) { _clv_x = clv_x; _clv_y = clv_y; }
public ClEditOrStayConstraint(ClVariable var, ClStrength strength) : this(var, strength, 1.0) {}
public ClEditConstraint(ClVariable clv) : base(clv) { }
public static ClLinearExpression Times(ClVariable clv, double n) /*throws ExCLNonlinearExpression*/ { return(new ClLinearExpression(clv, n)); }
public ClEditConstraint(ClVariable clv, ClStrength strength) : base(clv, strength) { }
public static ClLinearExpression Plus(ClLinearExpression e1, ClVariable e2) { return e1.Plus(new ClLinearExpression(e2)); }
public ClEditConstraint(ClVariable clv, ClStrength strength, double weight) : base(clv, strength, weight) { }
/// <summary> /// Add an edit constraint for a variable with a given strength. /// <param name="v">Variable to add an edit constraint to.</param> /// <param name="strength">Strength of the edit constraint.</param> /// </summary> public ClSimplexSolver AddEditVar(ClVariable v, ClStrength strength) /* throws ExClInternalError */ { try { ClEditConstraint cnEdit = new ClEditConstraint(v, strength); return AddConstraint(cnEdit); } catch (ExClRequiredFailure) { // should not get this throw new ExClInternalError("Required failure when adding an edit variable"); } }
public ClLinearInequality(ClVariable clv, byte op_enum, double val, ClStrength strength) : this(clv, op_enum, val, strength, 1.0) /* throws ExClInternalError */ {}
/// <summary> /// Remove the edit constraint previously added. /// <param name="v">Variable to which the edit constraint was added before.</param> /// </summary> public ClSimplexSolver RemoveEditVar(ClVariable v) /* throws ExClInternalError, ExClConstraintNotFound */ { ClEditInfo cei = (ClEditInfo) _editVarMap[v]; ClConstraint cn = cei.Constraint; RemoveConstraint(cn); return this; }
/// <summary> /// Remove a constraint from the tableau. /// Also remove any error variable associated with it. /// </summary> public ClSimplexSolver RemoveConstraint(ClConstraint cn) /* throws ExClRequiredFailure, ExClInternalError */ { _cNeedsSolving = true; ResetStayConstants(); ClLinearExpression zRow = RowExpression(_objective); HashSet <ClAbstractVariable> eVars; if (_errorVars.TryGetValue(cn, out eVars)) { foreach (ClAbstractVariable clv in eVars) { ClLinearExpression expr = RowExpression(clv); if (expr == null) { zRow.AddVariable(clv, -cn.Weight * cn.Strength.SymbolicWeight.AsDouble(), _objective, this); } else // the error variable was in the basis { zRow.AddExpression(expr, -cn.Weight * cn.Strength.SymbolicWeight.AsDouble(), _objective, this); } } } ClAbstractVariable marker; if (!_markerVars.TryGetValue(cn, out marker)) { throw new CassowaryConstraintNotFoundException(); } _markerVars.Remove(cn); if (RowExpression(marker) == null) { // not in the basis, so need to do some more work var col = Columns[marker]; ClAbstractVariable exitVar = null; double minRatio = 0.0; foreach (ClAbstractVariable v in col) { if (v.IsRestricted) { ClLinearExpression expr = RowExpression(v); double coeff = expr.CoefficientFor(marker); if (coeff < 0.0) { double r = -expr.Constant / coeff; if (exitVar == null || r < minRatio) { minRatio = r; exitVar = v; } } } } if (exitVar == null) { foreach (ClAbstractVariable v in col) { if (v.IsRestricted) { ClLinearExpression expr = RowExpression(v); double coeff = expr.CoefficientFor(marker); double r = expr.Constant / coeff; if (exitVar == null || r < minRatio) { minRatio = r; exitVar = v; } } } } if (exitVar == null) { // exitVar is still null if (col.Count == 0) { RemoveColumn(marker); } else { // put first element in exitVar var colEnum = col.GetEnumerator(); colEnum.MoveNext(); exitVar = colEnum.Current; } } if (exitVar != null) { Pivot(marker, exitVar); } } if (RowExpression(marker) != null) { RemoveRow(marker); } if (eVars != null) { foreach (ClAbstractVariable v in eVars.Where(a => a != marker)) { RemoveColumn(v); } } if (cn.IsStayConstraint) { if (eVars != null) { for (int i = 0; i < _stayPlusErrorVars.Count; i++) { eVars.Remove(_stayPlusErrorVars[i]); eVars.Remove(_stayMinusErrorVars[i]); } } } else if (cn.IsEditConstraint) { Assert(eVars != null, "eVars != null"); ClEditConstraint cnEdit = (ClEditConstraint)cn; ClVariable clv = cnEdit.Variable; ClEditInfo cei = _editVarMap[clv]; ClSlackVariable clvEditMinus = cei.ClvEditMinus; RemoveColumn(clvEditMinus); _editVarMap.Remove(clv); } // FIXME: do the remove at top if (eVars != null) { //_errorVars.Remove(eVars); _errorVars.Remove(cn); } if (_cOptimizeAutomatically) { Optimize(_objective); SetExternalVariables(); } return(this); }
public ClSimplexSolver AddPointStay(ClVariable vx, ClVariable vy) /* throws ExClRequiredFailure, ExClInternalError */ { AddPointStay(vx, vy, 1.0); return this; }
public bool ContainsVariable(ClVariable v) /* throws ExClInternalError */ { return(ColumnsHasKey(v) || (RowExpression(v) != null)); }
/// <remarks> /// Default to strength ClStrength#Weak. /// </remarks> public ClSimplexSolver AddStay(ClVariable v) /* throws ExClRequiredFailure, ExClInternalError */ { AddStay(v, ClStrength.Weak, 1.0); return this; }
public ClPoint(double x, double y, int a) { _clvX = new ClVariable("x" + a, x); _clvY = new ClVariable("y" + a, y); }
public ClSimplexSolver SetEditedValue(ClVariable v, double n) /* throws ExClInternalError */ { if (!ContainsVariable(v)) { v.ChangeValue(n); return this; } if (!Cl.Approx(n, v.Value)) { AddEditVar(v); BeginEdit(); try { SuggestValue(v, n); } catch(ExClError) { // just added it above, so we shouldn't get an error throw new ExClInternalError("Error in SetEditedValue"); } EndEdit(); } return this; }
public ClLinearInequality(ClVariable clv1, byte op_enum, ClVariable clv2) : this(clv1, op_enum, clv2, ClStrength.Required, 1.0) /* throws ExClInternalError */ {}
public ClSimplexSolver AddVar(ClVariable v) /* throws ExClInternalError */ { if (!ContainsVariable(v)) { try { AddStay(v); } catch(ExClRequiredFailure) { // cannot have a required failure, since we add w/ weak throw new ExClInternalError("Error in AddVar -- required failure is impossible"); } if (Trace) TracePrint("added initial stay on " + v); } return this; }
public /*sealed*/ ClLinearExpression Minus(ClVariable var) /*throws ExCLNonlinearExpression*/ { return ((ClLinearExpression) Clone()).AddVariable(var, -1.0); }
public static ClLinearExpression Times(ClVariable e1, ClLinearExpression e2) /*throws ExCLNonlinearExpression*/ { return (new ClLinearExpression(e1)).Times(e2); }
void Variable(out ClVariable v) { Expect(12); if (Context.ContainsKey(t.val)) { v = (ClVariable) Context[t.val]; } else { SemErr("Undefined variable: " + t.val); v = null; } }
public static ClLinearExpression Times( ClVariable clv, double n) /*throws ExCLNonlinearExpression*/ { return new ClLinearExpression(clv,n); }
public ClEditOrStayConstraint(ClVariable var) : this(var, ClStrength.Required, 1.0) { _variable = var; }
public static bool Approx(ClVariable clv, double b) { return Approx(clv.Value, b); }
public ClPoint(double x, double y) { _clvX = new ClVariable(x); _clvY = new ClVariable(y); }
public static ClLinearExpression Times(ClVariable e1, ClLinearExpression e2) /*throws ExCLNonlinearExpression*/ { return((new ClLinearExpression(e1)).Times(e2)); }
public ClPoint(ClVariable clvX, ClVariable clvY) { _clvX = clvX; _clvY = clvY; }
public /*sealed*/ ClLinearExpression Plus(ClVariable var) /*throws ExCLNonlinearExpression*/ { return(((ClLinearExpression)Clone()).AddVariable(var, 1.0)); }
public ClLinearExpression Minus(ClVariable var) /*throws ExCLNonlinearExpression*/ { return((Clone()).AddVariable(var, -1.0)); }
public void InitializeVariable(double val) { m_var = new ClVariable(HashValue, val); }