public IEnumerable <double[]> Solve(Term objective, IEnumerable <Term> constraints, Variable[] variables, double[] startPoint) { var constraintSquares = constraints.Select(c => TermBuilder.Power(c, 2)); var penalizedObjective = objective + penaltyWeight * TermUtils.SafeSum(constraintSquares); var compiledPenalizedObjective = penalizedObjective.Compile(variables); var solution = unconstrainedOptimizer.Solve(x => compiledPenalizedObjective.Differentiate(x), startPoint, gradientNormThreshold); yield return(solution); }
protected double[] InitialPoint(List <CNSAT.Var> constraints, RpropResult res) { Tuple <double[], double> tup; bool found = true; res.initialValue = new double[dim]; res.finalValue = new double[dim]; double[] gradient; do { gradient = new double[dim]; found = true; res.initialUtil = 1; for (int i = 0; i < dim; i++) { res.initialValue[i] = rand.NextDouble() * ranges[i] + limits[i, 0]; } this.fevalsCount++; for (int i = 0; i < constraints.Count; i++) { if (constraints[i].Assignment == CNSAT.Assignment.True) { if (constraints[i].PositiveTerm == null) { constraints[i].PositiveTerm = TermUtils.Compile(constraints[i].Term, this.currentArgs); } constraints[i].CurTerm = constraints[i].PositiveTerm; } else { if (constraints[i].NegativeTerm == null) { constraints[i].NegativeTerm = TermUtils.Compile(constraints[i].Term.Negate(), this.currentArgs); } constraints[i].CurTerm = constraints[i].NegativeTerm; } tup = constraints[i].CurTerm.Differentiate(res.initialValue); for (int j = 0; j < dim; j++) { if (Double.IsNaN(tup.Item1[j])) { found = false; break; } gradient[j] += tup.Item1[j]; } if (!found) { break; } if (tup.Item2 <= 0.0) { if (res.initialUtil > 0.0) { res.initialUtil = tup.Item2; } else { res.initialUtil += tup.Item2; } } } //tup = term.Differentiate(res.initialValue); } while(!found); res.finalUtil = res.initialUtil; Buffer.BlockCopy(res.initialValue, 0, res.finalValue, 0, sizeof(double) * dim); return(gradient); }