Beispiel #1
0
 /// <summary>
 /// Adds a function that needs to be.
 /// </summary>
 /// <param name="unknown">The unknown with a minimum value.</param>
 /// <param name="strict">The strict minimum value for the unknown.</param>
 public void AddMinimum(Unknown unknown, double strict)
 {
     if (unknown == null)
     {
         return;
     }
     _minimum.Add(unknown, strict);
 }
Beispiel #2
0
        /// <summary>
        /// Build a new solver from scratch using the current constraints and variables.
        /// </summary>
        /// <returns>The solver.</returns>
        private void BuildSolver()
        {
            _solver = new SparseRealSolver();
            _map.Clear();

            // Build our total function
            bool hasResolved = true;
            var  diffs       = new Dictionary <Unknown, Function>();

            while (hasResolved)
            {
                hasResolved = false;
                _lambdas.Clear();
                var f     = Minimize ?? 1.0;
                int index = 1;
                foreach (var c in _constraints.Keys)
                {
                    if (c.IsFixed)
                    {
                        if (!c.Value.IsZero())
                        {
                            Warn(this, new WarningEventArgs($"Could not {_constraints[c]}."));
                        }
                        continue;
                    }
                    var lambda = new Unknown($"lambda{index++}", UnknownTypes.Scalar);
                    f += lambda * c;
                    _lambdas.Add(lambda);
                }

                // Differentiate the Lagrangian function
                if (LogInfo)
                {
                    Console.WriteLine(f);
                }
                diffs.Clear();
                f.Differentiate(null, diffs);

                // Handle unknowns that need to be minimized
                foreach (var m in _minimum)
                {
                    if (m.Key.IsFixed)
                    {
                        if (m.Key.Value < m.Value)
                        {
                            Warn(this, new WarningEventArgs($"Minimum was violated for {m.Key}."));
                        }
                        continue;
                    }
                    if (diffs.TryGetValue(m.Key, out var eq))
                    {
                        diffs[m.Key] = eq - 0.01 / (m.Key - m.Value);
                    }
                }

                // First try to precompute as many constraints as possible
                var done = new HashSet <Unknown>();
                do
                {
                    done.Clear();
                    foreach (var pair in diffs)
                    {
                        var c = pair.Value;

                        // Skip the ones we already resolved
                        if (c.Resolve(0.0))
                        {
                            hasResolved = true;
                            if (LogInfo)
                            {
                                Console.WriteLine($"Resolved {c}");
                            }

                            // This has been fixed!
                            done.Add(pair.Key);
                        }
                        else if (c.IsFixed && !c.Value.IsZero())
                        {
                            if (_constraints.TryGetValue(c, out var description))
                            {
                                Warn(this, new WarningEventArgs($"Could not {description}."));
                            }
                            else
                            {
                                Warn(this, new WarningEventArgs($"Could not {c}."));
                            }

                            // This has been fixed!
                            done.Add(pair.Key);
                        }
                    }
                    foreach (var key in done)
                    {
                        diffs.Remove(key);
                    }
                }while (done.Count > 0);
            }

            // Setup the equations and solution
            _solution    = new DenseVector <double>(_equations.Count);
            _oldSolution = new DenseVector <double>(_equations.Count);
            foreach (var eq in diffs)
            {
                var index       = _map.Map(eq.Key);
                var rowEquation = eq.Value.CreateEquation(index, _map, _solver);
                _equations.Add(eq.Key, rowEquation);
                switch (eq.Key.Type)
                {
                case UnknownTypes.Scale:
                    if (eq.Key.Value.IsZero())
                    {
                        _solution[index] = 1.0;
                    }
                    else
                    {
                        _solution[index] = eq.Key.Value;
                    }
                    break;

                case UnknownTypes.Length:
                    if (eq.Key.Value < 0)
                    {
                        _solution[index] = 0;
                    }
                    else
                    {
                        _solution[index] = eq.Key.Value;
                    }
                    break;

                case UnknownTypes.X:
                case UnknownTypes.Y:
                    _solution[index] = Fix;
                    break;

                default:
                    _solution[index] = eq.Key.Value;
                    break;
                }
                _oldSolution[index] = _solution[index];
                if (LogInfo)
                {
                    Console.WriteLine($"df/d{eq.Key} = {eq.Value}");
                }
            }
            foreach (var m in _minimum)
            {
                var initial = m.Key.Value;
                if (m.Key.Value <= m.Value)
                {
                    initial = m.Value + 1e-9;
                }
                if (_map.TryGet(m.Key, out var index))
                {
                    _solution[index]    = initial;
                    _oldSolution[index] = _solution[index];
                }
            }
        }
Beispiel #3
0
 public ConstantRowEquation(Unknown uk, ISparseSolver <double> solver, int row)
 {
     _uk  = uk;
     _rhs = solver.GetElement(row);
 }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnknownFunction"/> class.
 /// </summary>
 /// <param name="unknown">The unknown.</param>
 public UnknownFunction(Unknown unknown)
 {
     _unknown = unknown ?? throw new ArgumentNullException(nameof(unknown));
 }
Beispiel #5
0
 public RowEquation(Unknown uk, ISparseSolver <double> solver, int row, int column)
 {
     _uk  = uk;
     _elt = null;
     _elt = solver.GetElement(new MatrixLocation(row, column));
 }