本文整理匯總了Python中ncpol2sdpa.SdpRelaxation.write_to_file方法的典型用法代碼示例。如果您正苦於以下問題:Python SdpRelaxation.write_to_file方法的具體用法?Python SdpRelaxation.write_to_file怎麽用?Python SdpRelaxation.write_to_file使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ncpol2sdpa.SdpRelaxation
的用法示例。
在下文中一共展示了SdpRelaxation.write_to_file方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: generate_variables
# 需要導入模塊: from ncpol2sdpa import SdpRelaxation [as 別名]
# 或者: from ncpol2sdpa.SdpRelaxation import write_to_file [as 別名]
level = 1
# Get Hermitian variables
X = generate_variables(n_vars, hermitian=True)
# Define the objective function
obj = 0
for i in range(n_vars):
for j in range(n_vars):
obj += X[i] * X[j]
# Equality constraints
equalities = []
for i in range(n_vars):
equalities.append(X[i] * X[i] - 1.0)
# Simple monomial substitutions
substitutions = {}
for i in range(n_vars):
for j in range(i + 1, n_vars):
# [X_i, X_j] = 0
substitutions[X[i] * X[j]] = X[j] * X[i]
# Obtain SDP relaxation
time0 = time.time()
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(level, objective=obj, equalities=equalities,
substitutions=substitutions)
sdpRelaxation.write_to_file('benchmark.dat-s')
print('%0.2f s' % ((time.time() - time0)))
示例2: generate_variables
# 需要導入模塊: from ncpol2sdpa import SdpRelaxation [as 別名]
# 或者: from ncpol2sdpa.SdpRelaxation import write_to_file [as 別名]
Created on Fri May 10 09:45:11 2013
@author: Peter Wittek
"""
from ncpol2sdpa import generate_variables, SdpRelaxation
# Number of variables
n_vars = 2
# Level of relaxation
level = 2
# Get commutative variables
X = generate_variables(n_vars, commutative=True)
# Define the objective function
obj = X[0] * X[1] + X[1] * X[0]
# Inequality constraints
inequalities = [-X[1] ** 2 + X[1] + 0.5]
# Simple monomial substitutions
monomial_substitution = {}
monomial_substitution[X[0] ** 2] = X[0]
# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(X)
sdpRelaxation.get_relaxation(level, objective=obj, inequalities=inequalities,
substitutions=monomial_substitution)
sdpRelaxation.write_to_file('example_commutative.dat-s')
示例3: generate_variables
# 需要導入模塊: from ncpol2sdpa import SdpRelaxation [as 別名]
# 或者: from ncpol2sdpa.SdpRelaxation import write_to_file [as 別名]
# -*- coding: utf-8 -*-
"""
This script replicates the results of gloptipolydemo.m, which is packaged with
Gloptipoly3.
Created on Thu May 15 11:16:58 2014
@author: wittek
"""
from ncpol2sdpa import SdpRelaxation, generate_variables
# Get commutative variables
x = generate_variables(2, commutative=True)
g0 = 4 * x[0] ** 2 + x[0] * x[1] - 4 * x[1] ** 2 - \
2.1 * x[0] ** 4 + 4 * x[1] ** 4 + x[0] ** 6 / 3
# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(x)
sdpRelaxation.get_relaxation(3, objective=g0)
sdpRelaxation.write_to_file("gloptipoly_demo.dat-s")
示例4: len
# 需要導入模塊: from ncpol2sdpa import SdpRelaxation [as 別名]
# 或者: from ncpol2sdpa.SdpRelaxation import write_to_file [as 別名]
Section 5.12 of the following paper:
Henrion, D.; Lasserre, J. & Löfberg, J. GloptiPoly 3: moments, optimization and
semidefinite programming. Optimization Methods & Software, 2009, 24, 761-779
Created on Thu May 15 12:12:40 2014
@author: wittek
"""
import numpy as np
from ncpol2sdpa import SdpRelaxation, generate_variables
W = np.diag(np.ones(8), 1) + np.diag(np.ones(7), 2) + np.diag([1, 1], 7) + \
np.diag([1], 8)
W = W + W.T
n = len(W)
e = np.ones(n)
Q = (np.diag(np.dot(e.T, W)) - W) / 4
x = generate_variables(n, commutative=True)
equalities = [xi ** 2 - 1 for xi in x]
objective = -np.dot(x, np.dot(Q, np.transpose(x)))
level = 1
sdpRelaxation = SdpRelaxation(x)
sdpRelaxation.get_relaxation(level, objective=objective, equalities=equalities,
removeequalities=True)
sdpRelaxation.write_to_file("max_cut.dat-s")
示例5: generate_variables
# 需要導入模塊: from ncpol2sdpa import SdpRelaxation [as 別名]
# 或者: from ncpol2sdpa.SdpRelaxation import write_to_file [as 別名]
"""
import time
from sympy.physics.quantum.dagger import Dagger
from ncpol2sdpa import generate_variables, SdpRelaxation,\
bosonic_constraints
# Level of relaxation
level = 1
# Number of variables
N = 3
# Parameters for the Hamiltonian
hbar, omega = 1, 1
# Define ladder operators
a = generate_variables(N, name='a')
substitutions = bosonic_constraints(a)
hamiltonian = sum(hbar * omega * (Dagger(a[i]) * a[i]) for i in range(N))
time0 = time.time()
# Obtain SDP relaxation
sdpRelaxation = SdpRelaxation(a, verbose=1)
sdpRelaxation.get_relaxation(level, objective=hamiltonian,
substitutions=substitutions)
# Export relaxation to SDPA format
sdpRelaxation.write_to_file("harmonic_oscillator.dat-s")
print('%0.2f s' % ((time.time() - time0)))
示例6: generate_variables
# 需要導入模塊: from ncpol2sdpa import SdpRelaxation [as 別名]
# 或者: from ncpol2sdpa.SdpRelaxation import write_to_file [as 別名]
# -*- coding: utf-8 -*-
"""
This example 18.1 from the following paper:
Kim, S. & Kojima, M. (2012). Exploiting Sparsity in SDP Relaxation of
Polynomial Optimization Problems. In Handbook on Semidefinite, Conic and
Polynomial Optimization. Springer, 2012, 499--531.
Created on Sun Nov 30 19:18:04 2014
@author: Peter Wittek
"""
from ncpol2sdpa import generate_variables, SdpRelaxation
level = 2
X = generate_variables(3, commutative=True)
inequalities = [1-X[0]**2-X[1]**2 >= 0,
1-X[1]**2-X[2]**2 >= 0]
sdpRelaxation = SdpRelaxation(X, verbose=2)
sdpRelaxation.get_relaxation(level, objective=X[1] - 2*X[0]*X[1] + X[1]*X[2],
inequalities=inequalities, chordal_extension=True)
sdpRelaxation.solve()
sdpRelaxation.write_to_file("/home/wittek/sparse2.csv")
print(sdpRelaxation.primal, sdpRelaxation.dual, sdpRelaxation.status)