Esempio n. 1
0
        /// <remarks>
        /// Default to strength ClStrength#Weak.
        /// </remarks>
        public ClSimplexSolver AddStay(ClVariable v)
        /* throws ExClRequiredFailure, ExClInternalError */
        {
            AddStay(v, ClStrength.Weak, 1.0);

            return(this);
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        /// <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));
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 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);
 }
Esempio n. 7
0
 public ClEditOrStayConstraint(ClVariable var,
                               ClStrength strength,
                               double weight) : base(strength, weight)
 {
     _variable   = var;
     _expression = new ClLinearExpression(_variable, -1.0, _variable.Value);
 }
Esempio n. 8
0
 /// <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");
     }
 }
Esempio n. 9
0
        /// <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);
        }
Esempio n. 10
0
        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);
        }
Esempio n. 11
0
 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");
   }
 }
Esempio n. 12
0
        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");
            }
        }
Esempio n. 13
0
 public ClSimplexSolver AddPointStay(ClVariable vx,
                                     ClVariable vy,
                                     double weight)
   /* throws ExClRequiredFailure, ExClInternalError */
 {
   AddStay(vx, ClStrength.Weak, weight);
   AddStay(vy, ClStrength.Weak, weight);
   
   return this;
 }
Esempio n. 14
0
 /// <remarks>
 /// Add an edit constraint with strength ClStrength#Strong.
 /// </remarks>
 public ClSimplexSolver AddEditVar(ClVariable v)
 {
   /* throws ExClInternalError */
   return AddEditVar(v, ClStrength.Strong);
 }
Esempio n. 15
0
		public static ClLinearExpression Plus(ClVariable e1, double e2)
		{
		  return (new ClLinearExpression(e1)).Plus(new ClLinearExpression(e2));
		}
Esempio n. 16
0
		static bool Approx(double a, ClVariable clv)
		{
		  return Approx(a, clv.Value);
		}
Esempio n. 17
0
        public static ClSimplexSolver AddPointStay(this ClSimplexSolver solver, ClVariable vx, ClVariable vy)
        {
            solver.AddPointStay(vx, vy, 1.0);

            return(solver);
        }
Esempio n. 18
0
 public bool ContainsVariable(ClVariable v)
   /* throws ExClInternalError */
 {
   return ColumnsHasKey(v) || (RowExpression(v) != null);
 }
Esempio n. 19
0
 public ClPoint(double x, double y)
 {
     _clv_x = new ClVariable(x);
     _clv_y = new ClVariable(y);
 }
Esempio n. 20
0
 public ClEditConstraint(ClVariable clv) : base(clv)
 {}
Esempio n. 21
0
 public ClEditConstraint(ClVariable clv, 
                         ClStrength strength) : base(clv, strength)
 {}
Esempio n. 22
0
 public ClEditConstraint(ClVariable clv,
                         ClStrength strength,
                         double weight) : base(clv, strength, weight)
 {}
Esempio n. 23
0
 public static ClLinearExpression Plus(ClVariable e1, double e2)
 {
     return((new ClLinearExpression(e1)).Plus(new ClLinearExpression(e2)));
 }
Esempio n. 24
0
 static bool Approx(double a, ClVariable clv)
 {
     return(Approx(a, clv.Value));
 }
Esempio n. 25
0
 public static bool Approx(ClVariable clv, double b)
 {
     return(Approx(clv.Value, b));
 }
Esempio n. 26
0
 /// <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);
 }
Esempio n. 27
0
    /// <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;
    }
Esempio n. 28
0
 public ClPoint(ClVariable clv_x, ClVariable clv_y)
 {
     _clv_x = clv_x;
     _clv_y = clv_y;
 }
Esempio n. 29
0
 public ClLinearInequality(ClVariable clv,
                           byte op_enum,
                           double val) : this(clv, op_enum, val, ClStrength.Required, 1.0)
     /* throws ExClInternalError */
 {
 }
Esempio n. 30
0
 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)
 {}
Esempio n. 32
0
 public ClEditConstraint(ClVariable clv) : base(clv)
 {
 }
Esempio n. 33
0
 public static ClLinearExpression Times(ClVariable clv, double n)
 /*throws ExCLNonlinearExpression*/
 {
     return(new ClLinearExpression(clv, n));
 }
Esempio n. 34
0
 public ClEditConstraint(ClVariable clv,
                         ClStrength strength) : base(clv, strength)
 {
 }
Esempio n. 35
0
		public static ClLinearExpression Plus(ClLinearExpression e1, ClVariable e2)
		{
		  return e1.Plus(new ClLinearExpression(e2));
		}
Esempio n. 36
0
 public ClEditConstraint(ClVariable clv,
                         ClStrength strength,
                         double weight) : base(clv, strength, weight)
 {
 }
Esempio n. 37
0
 /// <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");
   }
 }
Esempio n. 38
0
 public ClLinearInequality(ClVariable clv,
                           byte op_enum,
                           double val,
                           ClStrength strength) : this(clv, op_enum, val, strength, 1.0)
                           /* throws ExClInternalError */
 {}
Esempio n. 39
0
 /// <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;
 }
Esempio n. 40
0
        /// <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);
        }
Esempio n. 41
0
    public ClSimplexSolver AddPointStay(ClVariable vx,
                                        ClVariable vy)
      /* throws ExClRequiredFailure, ExClInternalError */
    {
      AddPointStay(vx, vy, 1.0);

      return this;
    }
Esempio n. 42
0
 public bool ContainsVariable(ClVariable v)
 /* throws ExClInternalError */
 {
     return(ColumnsHasKey(v) || (RowExpression(v) != null));
 }
Esempio n. 43
0
    /// <remarks>
    /// Default to strength ClStrength#Weak.
    /// </remarks>
    public ClSimplexSolver AddStay(ClVariable v)
      /* throws ExClRequiredFailure, ExClInternalError */
    {
      AddStay(v, ClStrength.Weak, 1.0);

      return this;
    }
Esempio n. 44
0
 public ClPoint(double x, double y, int a)
 {
     _clvX = new ClVariable("x" + a, x);
     _clvY = new ClVariable("y" + a, y);
 }
Esempio n. 45
0
    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;
    }
Esempio n. 46
0
 public ClLinearInequality(ClVariable clv1,
                           byte op_enum,
                           ClVariable clv2) : this(clv1, op_enum, clv2, ClStrength.Required, 1.0)
                           /* throws ExClInternalError */
 {}
Esempio n. 47
0
    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;
    }
Esempio n. 48
0
 public void SetXY(ClVariable clv_x, ClVariable clv_y)
 {
   _clv_x = clv_x;
   _clv_y = clv_y;
 }
Esempio n. 49
0
 public /*sealed*/ ClLinearExpression Minus(ClVariable var) 
   /*throws ExCLNonlinearExpression*/
 { 
   return ((ClLinearExpression) Clone()).AddVariable(var, -1.0);
 }
Esempio n. 50
0
		public static ClLinearExpression Times(ClVariable e1, ClLinearExpression e2) 
		  /*throws ExCLNonlinearExpression*/
		{ 
			return (new ClLinearExpression(e1)).Times(e2); 
		}
Esempio n. 51
0
	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;
		                      }
		                    
	}
Esempio n. 52
0
		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;
 }
Esempio n. 54
0
		public static bool Approx(ClVariable clv, double b)
		{
		  return Approx(clv.Value, b);
		}
Esempio n. 55
0
 public ClPoint(double x, double y)
 {
     _clvX = new ClVariable(x);
     _clvY = new ClVariable(y);
 }
Esempio n. 56
0
 public static ClLinearExpression Times(ClVariable e1, ClLinearExpression e2)
 /*throws ExCLNonlinearExpression*/
 {
     return((new ClLinearExpression(e1)).Times(e2));
 }
Esempio n. 57
0
 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));
 }
Esempio n. 59
0
 public ClLinearExpression Minus(ClVariable var)
 /*throws ExCLNonlinearExpression*/
 {
     return((Clone()).AddVariable(var, -1.0));
 }
Esempio n. 60
0
		public void InitializeVariable(double val)
		{
			m_var = new ClVariable(HashValue, val);
		}