Hướng dẫn sum of squares optimization python - tổng số bình phương tối ưu hóa python

Trong hướng dẫn này, chúng tôi giải thích cách thực hiện tổng các tính toán cơ bản của các bình phương (SOS) và cung cấp các ví dụ về tổng số của lập trình/tối ưu hóa hình vuông bằng cách sử dụng bộ giải. Tùy thuộc vào ngôn ngữ/môi trường máy tính toán học mà bạn quen thuộc nhất, bạn có thể sử dụng một trong hai công cụ sau:

  • Macaulay2: sumsofsquares.m2
  • MATLAB: Sostools
  • Julia: sumofsquares.jl
  • Python: sumofsquares.py

Ở cấp độ cao, các công cụ này phân tích vấn đề SOS được thể hiện về các đa thức, thành một vấn đề tối ưu hóa semidefinite (SDP) sau đó được giải quyết bằng số bằng cách sử dụng bộ giải SDP phụ trợ.

Trong các ví dụ đơn giản dưới đây, chúng tôi cung cấp các triển khai bằng cách sử dụng một trong các công cụ này. Chỉ cần nhấp vào tab tương ứng để xem mã mà sau đó bạn có thể sao chép và dán vào chương trình tương ứng.

Để biết các vấn đề nâng cao hơn, xin vui lòng xem các bài tập cho khóa học ngắn AMS về các khoản tiền của hình vuông.

Chúng tôi chỉ ra rằng các công cụ SOS khác cũng có sẵn, chủ yếu ở Matlab, chẳng hạn như Yalmip và Gloptipoly.

Cài đặt và cấu hình

Dưới đây là các hướng dẫn đơn giản để cài đặt và định cấu hình các công cụ mong muốn. Hãy nhớ lại rằng đối với một trong hai lựa chọn, một bộ giải SDP phụ trợ là cần thiết, vì vậy bạn cũng có thể cần phải cài đặt nó. Vui lòng xem tài liệu đầy đủ của mỗi gói để biết thêm chi tiết.

Gói chính

Để cài đặt, nhập các lệnh sau.

  • Macaulay2
  • Matlab
  • Julia
  • Python

installPackage "SemidefiniteProgramming"
installPackage "SumsOfSquares"

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools

] add SumOfSquares
] add DynamicPolynomials
] add Mosek

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

  • Macaulay2
  • Matlab
  • Julia
  • Python

needsPackage( "SumsOfSquares" );

-- the default solver is CSDP
-- it can be changed with the command
changeSolver ("MOSEK", "/path/to/mosek")

% Make sure SOSTOOLS package is listed under "Set Path"

% Set backend solver, in this case CSDP
options.solver='csdp';

using SumOfSquares
using DynamicPolynomials
using MosekTools


# Using Mosek as the SDP solver
model = SOSModel(Mosek.Optimizer)

import sympy as sp
import numpy as np
from SumOfSquares import SOSProblem, poly_opt_prob

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

Bộ giải SDP phụ trợ

  • Tính toán SOS dựa trên các bộ giải SemideFinite Lập trình (SDP). Những người giải quyết sau đây có sẵn vào tháng 12 năm 2018:
  • Macaulay2: CSDP (được cài đặt sẵn), Mosek, SDPA.
  • MATLAB: CDCS, CSDP, MOSEK (chỉ tương thích với Yalmip), SDPA, SDPnal, SDPT3, Sedumi.
  • Julia: CDCS, COSMO, CSDP, MOSEK, PROXSDP, SCS, SDPA, SDPaf Family, SDPnal, SDPT3, Sedumi.

Python: Mosek, CVXopt

Ví dụ

Ví dụ 1: Kiểm tra xem có đa thức là SOS không

  • Macaulay2
  • Matlab
  • Julia
  • Python

R = QQ[x,y];
p = 2*x^4 + 2*x^3*y - x^2*y^2 + 5*y^4;
sosPoly solveSOS p
-- returns the rational SOS decomposition
-- p = 5 (-(11/25)*x^2+y^2)^2 +  17/5*(5/17*x^2+x*y)^2 + 1568/2125 * x^4

syms x y;
p = 2*x^4 + 2*x^3*y - x^2*y^2 + 5*y^4;
[Q, Z] = findsos(p, options)
% Returns matrices Q and Z so that Z.'*Q*Z = p

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
0

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
1

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

  • Macaulay2
  • Matlab
  • Julia
  • Python

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
2

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
3

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
4

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
5

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

Bộ giải SDP phụ trợ

Tính toán SOS dựa trên các bộ giải SemideFinite Lập trình (SDP). Những người giải quyết sau đây có sẵn vào tháng 12 năm 2018:
  • Macaulay2
  • Matlab
  • Julia
  • Python

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
6

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
7

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
8

% Download SOSTOOLS and save it in some directory.
% The following code has been tested with SOSTOOLS release v3.03
% Start MATLAB, go to this directory, and run the script:
addsostools
9

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

  • Macaulay2
  • Matlab
  • Julia
  • Python

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
0

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
1

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
3

Bộ giải SDP phụ trợ

  • Macaulay2
  • Matlab
  • Julia
  • Python

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
4

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
5

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
6

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
7

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

Bộ giải SDP phụ trợ

  • Macaulay2
  • Matlab
  • Julia
  • Python

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
8

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
9

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
0

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
1

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

  • Macaulay2
  • Matlab
  • Julia
  • Python

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
2

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
3

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
4

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
5

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

Bộ giải SDP phụ trợ

Tính toán SOS dựa trên các bộ giải SemideFinite Lập trình (SDP). Những người giải quyết sau đây có sẵn vào tháng 12 năm 2018:
  • Macaulay2
  • Matlab
  • Julia
  • Python

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
6

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
7

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
8

# Download SumOfSquares.py from github and run from the terminal:
# $ python setup.py install
9

Sau khi cài đặt, các dòng sau phải được nhập vào đầu mỗi phiên.

Bộ giải SDP phụ trợ

Tính toán SOS dựa trên các bộ giải SemideFinite Lập trình (SDP). Những người giải quyết sau đây có sẵn vào tháng 12 năm 2018:

  • Macaulay2
  • Matlab
  • Julia
  • Python

] add SumOfSquares
] add DynamicPolynomials
] add Mosek
4

needsPackage( "SumsOfSquares" );

-- the default solver is CSDP
-- it can be changed with the command
changeSolver ("MOSEK", "/path/to/mosek")
1

needsPackage( "SumsOfSquares" );

-- the default solver is CSDP
-- it can be changed with the command
changeSolver ("MOSEK", "/path/to/mosek")
2

needsPackage( "SumsOfSquares" );

-- the default solver is CSDP
-- it can be changed with the command
changeSolver ("MOSEK", "/path/to/mosek")
3

Ví dụ 5: Công thức kép và PseudoExpectations

Các vấn đề kép của một vấn đề SOS có thể được hiểu là tối ưu hóa các chức năng tuyến tính của đa thức cấp độ thấp, còn được gọi là giả hành. Sử dụng sumofsquares.py, chúng ta có thể chỉ định các ràng buộc về mặt giả, cũng như trích xuất giả giả tương ứng với kép của một ràng buộc SOS.

Ví dụ sau đây tính toán rõ ràng một chứng chỉ không khả thi để viết đa thức Motzkin \ (p (x, y) = x^4 y^2 + x^2 y^4 - 3 x^2 y^2 + 1 \) của hình vuông, một pseudoexpectation trong đó \ (\ tilde {\ mathbb {e}} [p (x, y)] = -1 \).

  • Python

needsPackage( "SumsOfSquares" );

-- the default solver is CSDP
-- it can be changed with the command
changeSolver ("MOSEK", "/path/to/mosek")
4

Ví dụ 5: Công thức kép và PseudoExpectations

Các vấn đề kép của một vấn đề SOS có thể được hiểu là tối ưu hóa các chức năng tuyến tính của đa thức cấp độ thấp, còn được gọi là giả hành. Sử dụng sumofsquares.py, chúng ta có thể chỉ định các ràng buộc về mặt giả, cũng như trích xuất giả giả tương ứng với kép của một ràng buộc SOS.
  • Python

needsPackage( "SumsOfSquares" );

-- the default solver is CSDP
-- it can be changed with the command
changeSolver ("MOSEK", "/path/to/mosek")
5

Ví dụ 5: Công thức kép và PseudoExpectations

  1. Các vấn đề kép của một vấn đề SOS có thể được hiểu là tối ưu hóa các chức năng tuyến tính của đa thức cấp độ thấp, còn được gọi là giả hành. Sử dụng sumofsquares.py, chúng ta có thể chỉ định các ràng buộc về mặt giả, cũng như trích xuất giả giả tương ứng với kép của một ràng buộc SOS.
  2. Ví dụ sau đây tính toán rõ ràng một chứng chỉ không khả thi để viết đa thức Motzkin \ (p (x, y) = x^4 y^2 + x^2 y^4 - 3 x^2 y^2 + 1 \) của hình vuông, một pseudoexpectation trong đó \ (\ tilde {\ mathbb {e}} [p (x, y)] = -1 \).
  3. Mỗi ràng buộc SOS đều có một giả hành liên quan, chúng ta có thể truy cập nó để sử dụng trong các thuật toán làm tròn. Ví dụ tiếp theo trình bày cả thư giãn SOS nguyên thủy và kép của vấn đề tối ưu hóa sau đây trên hình cầu:

\ [\ max_ {x, y, z} \ Quad (x+\ Phi y) (x- \ Phi y) (y+\ Phi Z) x) \ Quad \ Text {S.T. } \ Quad x^2 + y^2 + z^2 = 1. \]

  • Python

needsPackage( "SumsOfSquares" );

-- the default solver is CSDP
-- it can be changed with the command
changeSolver ("MOSEK", "/path/to/mosek")
6