Follows the solution path to find the best lambda value.
161900
def solution_path(self, min_lambda, max_lambda, lambda_bins, verbose=0):
        ''''''
        self.u = np.zeros(self.Dk.shape[0], dtype='double')
        lambda_grid = np.exp(np.linspace(np.log(max_lambda), np.log(min_lambda), lambda_bins))
        aic_trace = np.zeros(lambda_grid.shape) # The AIC score for each lambda value
        aicc_trace = np.zeros(lambda_grid.shape) # The AICc score for each lambda value (correcting for finite sample size)
        bic_trace = np.zeros(lambda_grid.shape) # The BIC score for each lambda value
        dof_trace = np.zeros(lambda_grid.shape) # The degrees of freedom of each final solution
        log_likelihood_trace = np.zeros(lambda_grid.shape)
        beta_trace = []
        best_idx = None
        best_plateaus = None

        if self.edges is None:
            self.edges = defaultdict(list)
            elist = csr_matrix(self.D).indices.reshape((self.D.shape[0], 2))
            for n1, n2 in elist:
                self.edges[n1].append(n2)
                self.edges[n2].append(n1)

        # Solve the series of lambda values with warm starts at each point
        for i, lam in enumerate(lambda_grid):
            if verbose:
                print('#{0} Lambda = {1}'.format(i, lam))

            # Fit to the final values
            beta = self.solve(lam)

            if verbose:
                print('Calculating degrees of freedom')

            # Count the number of free parameters in the grid (dof) -- TODO: the graph trend filtering paper seems to imply we shouldn't multiply by (k+1)?
            dof_vals = self.Dk_minus_one.dot(beta) if self.k > 0 else beta
            plateaus = calc_plateaus(dof_vals, self.edges, rel_tol=0.01) if (self.k % 2) == 0 else nearly_unique(dof_vals, rel_tol=0.03)
            dof_trace[i] = max(1,len(plateaus)) #* (k+1)

            if verbose:
                print('Calculating Information Criteria')

            # Get the negative log-likelihood
            log_likelihood_trace[i] = -0.5 * ((self.y - beta)**2).sum()

            # Calculate AIC = 2k - 2ln(L)
            aic_trace[i] = 2. * dof_trace[i] - 2. * log_likelihood_trace[i]
            
            # Calculate AICc = AIC + 2k * (k+1) / (n - k - 1)
            aicc_trace[i] = aic_trace[i] + 2 * dof_trace[i] * (dof_trace[i]+1) / (len(beta) - dof_trace[i] - 1.)

            # Calculate BIC = -2ln(L) + k * (ln(n) - ln(2pi))
            bic_trace[i] = -2 * log_likelihood_trace[i] + dof_trace[i] * (np.log(len(beta)) - np.log(2 * np.pi))

            # Track the best model thus far
            if best_idx is None or bic_trace[i] < bic_trace[best_idx]:
                best_idx = i
                best_plateaus = plateaus

            # Save the trace of all the resulting parameters
            beta_trace.append(np.array(beta))
            
            if verbose:
                print('DoF: {0} AIC: {1} AICc: {2} BIC: {3}\n'.format(dof_trace[i], aic_trace[i], aicc_trace[i], bic_trace[i]))

        if verbose:
            print('Best setting (by BIC): lambda={0} [DoF: {1}, AIC: {2}, AICc: {3} BIC: {4}]'.format(lambda_grid[best_idx], dof_trace[best_idx], aic_trace[best_idx], aicc_trace[best_idx], bic_trace[best_idx]))

        return {'aic': aic_trace,
                'aicc': aicc_trace,
                'bic': bic_trace,
                'dof': dof_trace,
                'loglikelihood': log_likelihood_trace,
                'beta': np.array(beta_trace),
                'lambda': lambda_grid,
                'best_idx': best_idx,
                'best': beta_trace[best_idx],
                'plateaus': best_plateaus}
1264857
public static <S extends Solution<?>> S getBestSolution(S solution1, S solution2, Comparator<S> comparator) {
    return getBestSolution(solution1, solution2, comparator, () -> JMetalRandom.getInstance().nextDouble());
  }
968470
public <S extends Solution<?>> void execute(List<S> solutionsList) {
    switch (config.scalarizationType) {
      case ANGLE_UTILITY:
        if (config.extremePoints == null)
          ScalarizationUtils.angleUtility(solutionsList);
        else
          ScalarizationUtils.angleUtility(solutionsList, config.extremePoints);
        break;
      case CHEBYSHEV:
        if (config.idealValues == null)
          ScalarizationUtils.chebyshev(solutionsList);
        else
          ScalarizationUtils.chebyshev(solutionsList, config.idealValues);
        break;
      case NASH:
        if (config.nadirValues == null)
          ScalarizationUtils.nash(solutionsList);
        else
          ScalarizationUtils.nash(solutionsList, config.nadirValues);
        break;
      case SUM_OF_OBJECTIVES:
        ScalarizationUtils.sumOfObjectives(solutionsList);
        break;
      case PRODUCT_OF_OBJECTIVES:
        ScalarizationUtils.productOfObjectives(solutionsList);
        break;
      case TRADEOFF_UTILITY:
        ScalarizationUtils.tradeoffUtility(solutionsList);
        break;
      case UNIFORM:
        ScalarizationUtils.uniform(solutionsList);
        break;
      case WEIGHTED_CHEBYSHEV:
        if (config.idealValues == null)
          ScalarizationUtils.weightedChebyshev(solutionsList, config.weights);
        else
          ScalarizationUtils.weightedChebyshev(solutionsList, config.idealValues, config.weights);
        break;
      case WEIGHTED_PRODUCT:
        ScalarizationUtils.weightedProduct(solutionsList, config.weights);
        break;
      case WEIGHTED_SUM:
        ScalarizationUtils.weightedSum(solutionsList, config.weights);
        break;
    }
  }
923043
def best_fit(li, value):
    """"""

    index = min(bisect_left(li, value), len(li) - 1)

    if index in (0, len(li)):
        return index

    if li[index] - value < value - li[index-1]:
        return index
    else:
        return index-1
918647
def _compute_results(self):
        
        self.Y_best = best_value(self.Y)
        self.x_opt = self.X[np.argmin(self.Y),:]
        self.fx_opt = np.min(self.Y)
238239
@Override
	public void run() throws SolverException {
		// Create an executor for concurrent evaluation of derivatives
		if(numberOfThreads > 1) {
			if(executor == null) {
				executor = Executors.newFixedThreadPool(numberOfThreads);
				executorShutdownWhenDone = true;
			}
		}

		try {

			// Allocate memory
			int numberOfParameters	= initialParameters.length;
			int numberOfValues		= targetValues.length;

			parameterTest		= initialParameters.clone();
			parameterIncrement	= new double[numberOfParameters];
			parameterCurrent	= new double[numberOfParameters];

			valueTest			= new double[numberOfValues];
			valueCurrent		= new double[numberOfValues];
			derivativeCurrent	= new double[parameterCurrent.length][valueCurrent.length];

			hessianMatrix = new double[parameterCurrent.length][parameterCurrent.length];
			beta = new double[parameterCurrent.length];

			iteration = 0;

			while(true) {
				// Count iterations
				iteration++;

				// Calculate values for test parameters
				setValues(parameterTest, valueTest);

				// Calculate error
				double errorMeanSquaredTest = getMeanSquaredError(valueTest);

				/*
				 * Note: The following test will be false if errorMeanSquaredTest is NaN.
				 * That is: NaN is consider as a rejected point.
				 */
				if(errorMeanSquaredTest < errorMeanSquaredCurrent) {
					errorRootMeanSquaredChange = Math.sqrt(errorMeanSquaredCurrent) - Math.sqrt(errorMeanSquaredTest);

					// Accept point
					System.arraycopy(parameterTest, 0, parameterCurrent, 0, parameterCurrent.length);
					System.arraycopy(valueTest, 0, valueCurrent, 0, valueCurrent.length);
					errorMeanSquaredCurrent		= errorMeanSquaredTest;

					// Derivative has to be recalculated
					isParameterCurrentDerivativeValid = false;

					// Decrease lambda (move faster)
					lambda			/= lambdaDivisor;
				} else {
					errorRootMeanSquaredChange = Math.sqrt(errorMeanSquaredTest) - Math.sqrt(errorMeanSquaredCurrent);

					// Reject point, increase lambda (move slower)
					lambda				*= lambdaMultiplicator;
				}

				// Update a new parameter trial, if we are not done
				if (!done()) {
					updateParameterTest();
				} else {
					break;
				}

				// Log iteration
				if (logger.isLoggable(Level.FINE))
				{
					String logString = "Iteration: " + iteration + "\tLambda="
							+ lambda + "\tError Current:" + errorMeanSquaredCurrent
							+ "\tError Change:" + errorRootMeanSquaredChange + "\t";
					for (int i = 0; i < parameterCurrent.length; i++) {
						logString += "[" + i + "] = " + parameterCurrent[i] + "\t";
					}
					logger.fine(logString);
				}
			}
		}
		finally {
			// Shutdown executor if present.
			if(executor != null && executorShutdownWhenDone) {
				executor.shutdown();
				executor = null;
			}
		}
	}
1270755
def doSolve(fitsfn: Path, args: str=None):
    
    # binpath = Path(find_executable('solve-field')).parent
    opts = args.split(' ') if args else []
# %% build command
    cmd = ['solve-field', '--overwrite', str(fitsfn)]
    cmd += opts
    print('\n', ' '.join(cmd), '\n')
# %% execute
    ret = subprocess.check_output(cmd, universal_newlines=True)

    # solve-field returns 0 even if it didn't solve!
    print(ret)
    if 'Did not solve' in ret:
        raise RuntimeError(f'could not solve {fitsfn}')

    print('\n\n *** done with astrometry.net ***\n ')
1255468
public void calcCost(Path path)
    {
        path.cost = 0.0;
        if (alphaFloat_ != null)
        {
            float c = 0.0f;
            for (int i = 0; path.fvector.get(i) != -1; i++)
            {
                c += alphaFloat_[path.fvector.get(i) + path.lnode.y * y_.size() + path.rnode.y];
            }
            path.cost = costFactor_ * c;
        }
        else
        {
            double c = 0.0;
            for (int i = 0; path.fvector.get(i) != -1; i++)
            {
                c += alpha_[path.fvector.get(i) + path.lnode.y * y_.size() + path.rnode.y];
            }
            path.cost = costFactor_ * c;
        }
    }
1209380
void apply(final LambdaUnaryOpDouble lambda) {
        params.apply(new FnIntDoubleToDouble() {
            @Override
            public double call(int idx, double val) {
                return lambda.call(val);
            }
        });
    }
959849
@Override
    protected void searchStep() {
        // more solutions to generate ?
        if(solutionIterator.hasNext()){
            // generate next solution
            SolutionType sol = solutionIterator.next();
            // update best solution
            updateBestSolution(sol);
        } else {
            // done
            stop();
        }
    }
381579
public static Solution<?> normalize(Solution<?> solution, double[] minValues, double[] maxValues) {

		if (solution == null) {
			throw new JMetalException("The solution should not be null");
		}
		
		if (minValues == null || maxValues == null) {
			throw new JMetalException("The minValues and maxValues should not be null");
		}
		
		if (minValues.length == 0 || maxValues.length == 0) {
			throw new JMetalException("The minValues and maxValues should not be empty");
		}

		if (minValues.length != maxValues.length) {
			throw new JMetalException("The minValues and maxValues should have the same length");
		}

		if (solution.getNumberOfObjectives() != minValues.length) {
			throw new JMetalException("The number of objectives should be the same to min and max length");
		}

		Solution<?> copy = (Solution<?>) solution.copy();

		for (int i = 0; i < copy.getNumberOfObjectives(); i++) {
			copy.setObjective(i, NormalizeUtils.normalize(solution.getObjective(i), minValues[i], maxValues[i]));
		}

		return copy;
	}
1355775
def lambda_advantage(reward, value, length, discount, gae_lambda):
  """"""
  timestep = tf.range(reward.shape[1].value)
  mask = tf.cast(timestep[None, :] < length[:, None], tf.float32)
  next_value = tf.concat([value[:, 1:], tf.zeros_like(value[:, -1:])], 1)
  delta = reward + discount * next_value - value
  advantage = tf.reverse(tf.transpose(tf.scan(
      lambda agg, cur: cur + gae_lambda * discount * agg,
      tf.transpose(tf.reverse(mask * delta, [1]), [1, 0]),
      tf.zeros_like(delta[:, -1]), 1, False), [1, 0]), [1])
  return tf.check_numerics(tf.stop_gradient(advantage), 'advantage')
856584
def main(dimension, iterations):
    
    objective_function = minimize(functions.sphere)
    stopping_condition = max_iterations(iterations)
    (solution, metrics) = optimize(objective_function=objective_function,
                                   domain=Domain(-5.12, 5.12, dimension),
                                   stopping_condition=stopping_condition,
                                   parameters={'seed': 3758117674,
                                               'rho': 1.0, 'e_s': 15, 'e_f': 5},
                                   velocity_update=gc_velocity_update,
                                   parameter_update=update_rho,
                                   measurements=[fitness_measurement])
    return solution
420106
def follow_path( steps, past_steps = [] )
      if steps.is_a?( Hash )
        steps.each do |step, next_steps|
          path_to_this_point = [past_steps, step].flatten
          reflection = reflection_for( path_to_this_point ) rescue nil
          if reflection
            follow_path( next_steps, path_to_this_point )
          else
            follow_comparison( next_steps, past_steps, step )
          end
        end
      elsif steps.is_a?( Array ) && steps.length == 2
        if reflection_for( past_steps )
          follow_comparison( steps, past_steps, :id )
        else
          follow_comparison( steps, past_steps[0..-2], past_steps[-1] )
        end
      else
        raise "invalid obligation path #{[past_steps, steps].inspect}"
      end
    end
1035655
private void processSource(FastQueue<AssociatedIndex> matches, int numSource,
							   FastQueue<AssociatedIndex> output ) {
		//set up data structures
		scores.resize(numSource);
		solutions.resize(numSource);
		for( int i =0; i < numSource; i++ ) {
			solutions.data[i] = -1;
		}

		// select best matches
		for( int i = 0; i < matches.size(); i++ ) {
			AssociatedIndex a = matches.get(i);
			int found = solutions.data[a.src];
			if( found != -1 ) {
				if( found == -2 ) {
					// the best solution is invalid because two or more had the same score, see if this is better
					double bestScore = scores.data[a.src];
					int result = type.compareTo(bestScore,a.fitScore);
					if( result < 0 ) {
						// found a better one, use this now
						solutions.data[a.src] = i;
						scores.data[a.src] = a.fitScore;
					}
				} else {
					// see if this new score is better than the current best
					AssociatedIndex currentBest = matches.get( found );
					int result = type.compareTo(currentBest.fitScore,a.fitScore);
					if( result < 0 ) {
						// found a better one, use this now
						solutions.data[a.src] = i;
						scores.data[a.src] = a.fitScore;
					} else if( result == 0 ) {
						// two solutions have the same score
						solutions.data[a.src] = -2;
					}
				}
			} else {
				// no previous match found
				solutions.data[a.src] = i;
				scores.data[a.src] = a.fitScore;
			}
		}

		output.reset();
		for( int i =0; i < numSource; i++ ) {
			int index = solutions.data[i];
			if( index >= 0 ) {
				output.add( matches.get(index) );
			}
		}
	}
1198267
def backtrack_or_continue
      best = nil
      if (@iterations_since_best / @backtracks) >= @backtrack_cutoff * @max_hood_size
        self.backtrack
        best = @best
      else
        best = @current_hood.best
        self.adjust_distributions_using_gradient
      end
      if best[:parameters].nil?
        # this should never happen!
        best = @best
      end
      best
    end
455328
public Result<List<StraightPathItem>> findStraightPath(float[] startPos, float[] endPos, List<Long> path,
            int maxStraightPath, int options) {

        List<StraightPathItem> straightPath = new ArrayList<>();
        if (Objects.isNull(startPos) || !vIsFinite(startPos) || Objects.isNull(endPos) || !vIsFinite(endPos)
                || Objects.isNull(path) || path.isEmpty() || path.get(0) == 0 || maxStraightPath <= 0) {
            return Result.invalidParam();
        }
        // TODO: Should this be callers responsibility?
        Result<float[]> closestStartPosRes = closestPointOnPolyBoundary(path.get(0), startPos);
        if (closestStartPosRes.failed()) {
            return Result.invalidParam("Cannot find start position");
        }
        float[] closestStartPos = closestStartPosRes.result;
        Result<float[]> closestEndPosRes = closestPointOnPolyBoundary(path.get(path.size() - 1), endPos);
        if (closestEndPosRes.failed()) {
            return Result.invalidParam("Cannot find end position");
        }
        float[] closestEndPos = closestEndPosRes.result;
        // Add start point.
        Status stat = appendVertex(closestStartPos, DT_STRAIGHTPATH_START, path.get(0), straightPath, maxStraightPath);
        if (!stat.isInProgress()) {
            return Result.success(straightPath);
        }

        if (path.size() > 1) {
            float[] portalApex = vCopy(closestStartPos);
            float[] portalLeft = vCopy(portalApex);
            float[] portalRight = vCopy(portalApex);
            int apexIndex = 0;
            int leftIndex = 0;
            int rightIndex = 0;

            int leftPolyType = 0;
            int rightPolyType = 0;

            long leftPolyRef = path.get(0);
            long rightPolyRef = path.get(0);

            for (int i = 0; i < path.size(); ++i) {
                float[] left;
                float[] right;
                int toType;

                if (i + 1 < path.size()) {
                    // Next portal.
                    Result<PortalResult> portalPoints = getPortalPoints(path.get(i), path.get(i + 1));
                    left = portalPoints.result.left;
                    right = portalPoints.result.right;
                    toType = portalPoints.result.toType;
                    if (portalPoints.failed()) {
                        closestEndPosRes = closestPointOnPolyBoundary(path.get(i), endPos);
                        if (closestEndPosRes.failed()) {
                            return Result.invalidParam();
                        }
                        closestEndPos = closestEndPosRes.result;
                        // Append portals along the current straight path segment.
                        if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) {
                            // Ignore status return value as we're just about to return anyway.
                            appendPortals(apexIndex, i, closestEndPos, path, straightPath, maxStraightPath, options);
                        }
                        // Ignore status return value as we're just about to return anyway.
                        appendVertex(closestEndPos, 0, path.get(i), straightPath, maxStraightPath);
                        return Result.success(straightPath);
                    }

                    // If starting really close the portal, advance.
                    if (i == 0) {
                        Tupple2<Float, Float> dt = distancePtSegSqr2D(portalApex, left, right);
                        if (dt.first < sqr(0.001f)) {
                            continue;
                        }
                    }
                } else {
                    // End of the path.
                    left = vCopy(closestEndPos);
                    right = vCopy(closestEndPos);
                    toType = Poly.DT_POLYTYPE_GROUND;
                }

                // Right vertex.
                if (triArea2D(portalApex, portalRight, right) <= 0.0f) {
                    if (vEqual(portalApex, portalRight) || triArea2D(portalApex, portalLeft, right) > 0.0f) {
                        portalRight = vCopy(right);
                        rightPolyRef = (i + 1 < path.size()) ? path.get(i + 1) : 0;
                        rightPolyType = toType;
                        rightIndex = i;
                    } else {
                        // Append portals along the current straight path segment.
                        if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) {
                            stat = appendPortals(apexIndex, leftIndex, portalLeft, path, straightPath, maxStraightPath,
                                    options);
                            if (!stat.isInProgress()) {
                                return Result.success(straightPath);
                            }
                        }

                        portalApex = vCopy(portalLeft);
                        apexIndex = leftIndex;

                        int flags = 0;
                        if (leftPolyRef == 0) {
                            flags = DT_STRAIGHTPATH_END;
                        } else if (leftPolyType == Poly.DT_POLYTYPE_OFFMESH_CONNECTION) {
                            flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION;
                        }
                        long ref = leftPolyRef;

                        // Append or update vertex
                        stat = appendVertex(portalApex, flags, ref, straightPath, maxStraightPath);
                        if (!stat.isInProgress()) {
                            return Result.success(straightPath);
                        }

                        portalLeft = vCopy(portalApex);
                        portalRight = vCopy(portalApex);
                        leftIndex = apexIndex;
                        rightIndex = apexIndex;

                        // Restart
                        i = apexIndex;

                        continue;
                    }
                }

                // Left vertex.
                if (triArea2D(portalApex, portalLeft, left) >= 0.0f) {
                    if (vEqual(portalApex, portalLeft) || triArea2D(portalApex, portalRight, left) < 0.0f) {
                        portalLeft = vCopy(left);
                        leftPolyRef = (i + 1 < path.size()) ? path.get(i + 1) : 0;
                        leftPolyType = toType;
                        leftIndex = i;
                    } else {
                        // Append portals along the current straight path segment.
                        if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) {
                            stat = appendPortals(apexIndex, rightIndex, portalRight, path, straightPath,
                                    maxStraightPath, options);
                            if (!stat.isInProgress()) {
                                return Result.success(straightPath);
                            }
                        }

                        portalApex = vCopy(portalRight);
                        apexIndex = rightIndex;

                        int flags = 0;
                        if (rightPolyRef == 0) {
                            flags = DT_STRAIGHTPATH_END;
                        } else if (rightPolyType == Poly.DT_POLYTYPE_OFFMESH_CONNECTION) {
                            flags = DT_STRAIGHTPATH_OFFMESH_CONNECTION;
                        }
                        long ref = rightPolyRef;

                        // Append or update vertex
                        stat = appendVertex(portalApex, flags, ref, straightPath, maxStraightPath);
                        if (!stat.isInProgress()) {
                            return Result.success(straightPath);
                        }

                        portalLeft = vCopy(portalApex);
                        portalRight = vCopy(portalApex);
                        leftIndex = apexIndex;
                        rightIndex = apexIndex;

                        // Restart
                        i = apexIndex;

                        continue;
                    }
                }
            }

            // Append portals along the current straight path segment.
            if ((options & (DT_STRAIGHTPATH_AREA_CROSSINGS | DT_STRAIGHTPATH_ALL_CROSSINGS)) != 0) {
                stat = appendPortals(apexIndex, path.size() - 1, closestEndPos, path, straightPath, maxStraightPath,
                        options);
                if (!stat.isInProgress()) {
                    return Result.success(straightPath);
                }
            }
        }

        // Ignore status return value as we're just about to return anyway.
        appendVertex(closestEndPos, DT_STRAIGHTPATH_END, 0, straightPath, maxStraightPath);
        return Result.success(straightPath);
    }