コード例 #1
0
        public override void GenerateJacobian()
        {
            int i = 0;

            Jacobian.Clear();
            var testEval = new Evaluator();


            foreach (var equation in Equations)
            {
                var incidenceVector = equation.Incidence(testEval);

                foreach (var variable in incidenceVector)
                {
                    if (!variable.IsConstant && variable.DefiningExpression == null)
                    {
                        int j = -1;
                        if (_variableIndex.TryGetValue(variable, out j))
                        {
                            Jacobian.Add(new JacobianElement()
                            {
                                EquationIndex = i, VariableIndex = j, Value = 1.0
                            });

                            if (UseHessian)
                            {
                                var differential = (equation.Right - equation.Left).SymbolicDiff(variable);

                                foreach (var variable2 in incidenceVector)
                                {
                                    if (!variable2.IsConstant && variable2.DefiningExpression == null)
                                    {
                                        int k = -1;
                                        if (_variableIndex.TryGetValue(variable2, out k) && k <= j)
                                        {
                                            Hessian.Add(new HessianElement()
                                            {
                                                EquationIndex = i, Variable1Index = j, Variable2Index = k, Expression = differential, Value = 1.0
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                i++;
            }



            if (UseHessian)
            {
                var incidenceVector = ObjectiveFunction.Incidence();

                foreach (var variable1 in incidenceVector)
                {
                    if (!variable1.IsConstant && variable1.DefiningExpression == null)
                    {
                        int j = -1;
                        if (_variableIndex.TryGetValue(variable1, out j))
                        {
                            var differential = ObjectiveFunction.SymbolicDiff(variable1);
                            foreach (var variable2 in incidenceVector)
                            {
                                if (!variable2.IsConstant && variable2.DefiningExpression == null)
                                {
                                    int k = -1;
                                    if (_variableIndex.TryGetValue(variable2, out k) && k <= j)
                                    {
                                        ObjectiveHessian.Add(new HessianElement()
                                        {
                                            EquationIndex = 0, Variable1Index = j, Variable2Index = k, Expression = differential, Value = 1.0
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }


            foreach (var equation in Constraints)
            {
                var incidenceVector = equation.Incidence(testEval);
                foreach (var variable in incidenceVector)
                {
                    if (!variable.IsConstant && variable.DefiningExpression == null)
                    {
                        int j = -1;
                        if (_variableIndex.TryGetValue(variable, out j))
                        {
                            Jacobian.Add(new JacobianElement()
                            {
                                EquationIndex = i, VariableIndex = j, Value = 1.0
                            });

                            if (UseHessian)
                            {
                                var differential = (equation.Right - equation.Left).SymbolicDiff(variable);

                                foreach (var variable2 in incidenceVector)
                                {
                                    if (!variable2.IsConstant && variable2.DefiningExpression == null)
                                    {
                                        int k = -1;
                                        if (_variableIndex.TryGetValue(variable2, out k) && k <= j)
                                        {
                                            Hessian.Add(new HessianElement()
                                            {
                                                EquationIndex = i, Variable1Index = j, Variable2Index = k, Expression = differential, Value = 1.0
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                i++;
            }

            if (UseHessian)
            {
                GenerateHessianStructureInfo();
            }
        }