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


Python SdpRelaxation.write_to_file方法代码示例

本文整理汇总了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)))
开发者ID:chrinide,项目名称:ncpol2sdpa,代码行数:32,代码来源:benchmark_case.py

示例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')
开发者ID:chrinide,项目名称:ncpol2sdpa,代码行数:32,代码来源:example_commutative.py

示例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")
开发者ID:chrinide,项目名称:ncpol2sdpa,代码行数:23,代码来源:gloptipoly_demo.py

示例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")
开发者ID:chrinide,项目名称:ncpol2sdpa,代码行数:32,代码来源:max_cut.py

示例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)))
开发者ID:chrinide,项目名称:ncpol2sdpa,代码行数:32,代码来源:harmonic_oscillator.py

示例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)
开发者ID:chrinide,项目名称:ncpol2sdpa,代码行数:27,代码来源:sparse_pop.py


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