当前位置: 首页>>代码示例>>Java>>正文


Java PSDQuadraticMultivariateRealFunction类代码示例

本文整理汇总了Java中com.joptimizer.functions.PSDQuadraticMultivariateRealFunction的典型用法代码示例。如果您正苦于以下问题:Java PSDQuadraticMultivariateRealFunction类的具体用法?Java PSDQuadraticMultivariateRealFunction怎么用?Java PSDQuadraticMultivariateRealFunction使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


PSDQuadraticMultivariateRealFunction类属于com.joptimizer.functions包,在下文中一共展示了PSDQuadraticMultivariateRealFunction类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: solveFeatureWeights

import com.joptimizer.functions.PSDQuadraticMultivariateRealFunction; //导入依赖的package包/类
/**
 * FeatureWeight factory which solves the best weights given Feature Expectations calculated from
 * the expert demonstrations and a history of Feature Expectations.
 * @param expertExpectations Feature Expectations calculated from the expert demonstrations
 * @param featureExpectations Feature History of feature expectations generated from past policies
 * @return the best feature weights
 */
private static FeatureWeights solveFeatureWeights(
		double[] expertExpectations, List<double[]> featureExpectations) {
	// We are solving a Quadratic Programming Problem here, yay!
	// Solve equation of form xT * P * x + qT * x + r
	// Let x = {w0, w1, ... , wn, t}
	int weightsSize = expertExpectations.length;

	// The objective is to maximize t, or minimize -t
	double[] qObjective = new double[weightsSize + 1];
	qObjective[weightsSize] = -1;
	LinearMultivariateRealFunction objectiveFunction = 
			new LinearMultivariateRealFunction( qObjective, 0);

	// We set the requirement that all feature expectations generated have to be less than the expert
	List<ConvexMultivariateRealFunction> expertBasedWeightConstraints = 
			new ArrayList<ConvexMultivariateRealFunction>();

	// (1/2)xT * Pi * x + qiT + ri <= 0
	// Equation (11) wT * uE >= wT * u(j) + t ===>  (u(j) - uE)T * w + t <= 0
	// Because x = {w0, w1, ... , wn, t}, we can set
	// qi = {u(j)_1 - uE_1, ... , u(j)_n - uE_n, 1}
	for (double[] expectations : featureExpectations) {
		double[] difference = new double[weightsSize + 1];
		for (int i = 0; i < expectations.length; ++i) {
			difference[i] = expectations[i] - expertExpectations[i];
		}
		difference[weightsSize] = 1;
		expertBasedWeightConstraints.add(new LinearMultivariateRealFunction(difference, 1));
	}

	// L2 norm of weights must be less than or equal to 1. So 
	// P = Identity, except for the last entry (which cancels t).
	double[][] identityMatrix = Utils.createConstantDiagonalMatrix(weightsSize + 1, 1);
	identityMatrix[weightsSize][weightsSize] = 0;
	expertBasedWeightConstraints.add(new PSDQuadraticMultivariateRealFunction(identityMatrix, null, -0.5));

	OptimizationRequest optimizationRequest = new OptimizationRequest();
	optimizationRequest.setF0(objectiveFunction);
	optimizationRequest.setFi(expertBasedWeightConstraints.toArray(
			new ConvexMultivariateRealFunction[expertBasedWeightConstraints.size()]));
	optimizationRequest.setCheckKKTSolutionAccuracy(false);
	optimizationRequest.setTolerance(1.E-12);
	optimizationRequest.setToleranceFeas(1.E-12);

	JOptimizer optimizer = new JOptimizer();
	optimizer.setOptimizationRequest(optimizationRequest);
	try {
		optimizer.optimize();
	} catch (Exception e) {
		System.out.println(e);
		return null;
	}
	OptimizationResponse optimizationResponse = optimizer.getOptimizationResponse();

	double[] solution = optimizationResponse.getSolution();
	double[] weights = Arrays.copyOfRange(solution, 0, weightsSize);
	double score = solution[weightsSize];
	return new FeatureWeights(weights, score);
}
 
开发者ID:f-leno,项目名称:DOO-Q_BRACIS2016,代码行数:67,代码来源:ApprenticeshipLearning.java


注:本文中的com.joptimizer.functions.PSDQuadraticMultivariateRealFunction类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。