how to get the maximum result with quadratic programming on CVXOPT ?

the question is posted in follow page as well. https://stackoverflow.com/questions/47119580/how-to-get-the-maximum-result-with-quadratic-programming-on-cvxopt

I want to use CVXOPT to solve a quadratic programming.

'''
                        Value       =2      =3     =5        
                    Value*return    >=9    >=9      no requirement       
                   Value*Duration   <=5    <=9      no requirement
      Value  return  Duration           
        4       5       2           x11      x12   x13    
        6       4       4           x21      x22   x23 

the matrix is like about, I want to get x11, x12,…x23.

the subject is as following:

G*x<=h
-5*x11         -4*x21      <=-9
      -5*x12         -4*x22<=-9
                           <=0
 2*x11         +4*x21      <=5
       2*x12        +4*x22 <=9
                           <=0


A*x=b

x11+x12+x13           =4
           x21+x22+x23=6
x11       +x21        =2
    x12       +x22    =3
        x13       +x23=5

I want get the target Max (x11^2+x12^2+x13^2+x21^2+x22^2+x23^2)

I wrote the following code:

import cvxopt as op
p_tg=op.matrix(0.0,(6,6))
p_tg[::7]=1
P = 2*op.matrix(p_tg)

q = op.matrix( [0.0,0,0,0,0,0])

G=op.matrix([[-5.0,0,0,2,0,0],
             [0,-5,0,0,2,0],
             [0,0,0,0,0,0],
             [-4,0,0,4,0,0],
             [0,-4,0,0,4,0],
             [0,0,0,0,0,0]])

h=op.matrix([-9.0,-9,0,5,9,0])  

A=op.matrix([[1.0,0,1,0,0],
             [1,0,0,1,0],
             [1,0,0,0,1],
             [0,1,1,0,0],
             [0,1,0,1,0],
             [0,1,0,0,1]])              
b=op.matrix([4,6,2.0,3,5])

sol=op.solvers.qp(P, q, G, h, A, b)

print(sol['x'])

of course, the above code get the minimum, not the maximum.

how to get the max result? I try to p_tg[::7]=-1, while it report error.

raise ValueError(“Rank(A) < p or Rank([P; A; G]) < n”)

ValueError: Rank(A) < p or Rank([P; A; G]) < n

cvxopt for linear and quadratic program

# -*- coding: utf-8 -*-
“””
Created on Sun Nov 5 08:03:26 2017
http://cvxopt.org/examples/tutorial/qp.html
“””

#1. Solving a linear program
”’ Linear programs can be specified via the solvers.lp() function.
As an example, we can solve the problem

minimize 2*x1+x2
subject to
-x1+x2<=1
x1+x2>=2
x2>=0
x1-2*x2<=4

the st is equal to
-x1+x2<=1
-x1-x2<=-2
-x2<=0
x1-2*x2<=4

”’

import cvxopt as op
A = op.matrix([ [-1.0, -1.0, 0.0, 1.0],
[1.0, -1.0, -1.0, -2.0] ])
b = op.matrix( [ 1.0, -2.0, 0.0, 4.0 ])
c = op.matrix( [ 2.0, 1.0 ])
sol=op.solvers.lp(c,A,b)
print(sol[‘x’])

#%%2. Solving a quadratic program

”’
minimize 2*x1**2+x2**2+x1*x2+x1+x2
st
x1>=0
x2>=0
x1+x2=1

the object and st are equal to

[x1, x2] *[[2,.5],[.5,1]] *[[x1],[x2]]

st
-x1 <=0
-x2<=0
x1+x2 =1

op.solvers.qp(P, q, G=None, h=None, A=None, b=None, solver=None,
kktsolver=None, initvals=None, **kwargs)

Solves a quadratic program
minimize (1/2)*x’*P*x + q’*x
subject to G*x <= h
A*x = b.
Input arguments.
P is a n x n dense or sparse ‘d’ matrix with the lower triangular part of P stored in the lower triangle. Must be positive semidefinite.
q is an n x 1 dense ‘d’ matrix.
G is an m x n dense or sparse ‘d’ matrix.
h is an m x 1 dense ‘d’ matrix.
A is a p x n dense or sparse ‘d’ matrix.
b is a p x 1 dense ‘d’ matrix or None.
solver is None or ‘mosek’.
The default values for G, h, A and b are empty matrices with zero rows.
”’

P = 2*op.matrix([ [2, .5],
[.5, 1] ])
q = op.matrix( [1.0, 1.0])

G = op.matrix([[-1.0,0.0],
[0.0,-1.0]])
h = op.matrix( [0.0,0.0])

A = op.matrix( [1.0, 1.0], (1,2)) # with 1 row, 2 columns
b = op.matrix(1.0)
sol=op.solvers.qp(P, q, G, h, A, b)

print(sol[‘x’])

anaconda’s environment

create a new envirement.

conda create -n envpy34 python=3.4 anaconda

active the new envirement

active envpy34

to check the current environment

conda info -e

and now run Spyder with Python 3.4 just type:

spyder

install spyder in the new environment:

conda install -n envpy34 spyder

To see a list of all of your environments, in your Terminal window or an Anaconda Prompt, run:

conda info --envs

OR

conda env list

 

Install a list of packages into a specified conda environment.
The arguments may be packages specifications (e.g. bitarray=0.8),

**usage**: “conda install [-h] [–yes] [–dry-run] [-f] [–file FILE] [–no-deps] [-m] [–no-pip] [–use-local] [-c CHANNEL] [–override-channels] [-n NAME | -p PATH] [-q] [package_spec [package_spec …]]

Install a list of packages into a specified conda environment.
The arguments may be packages specifications (e.g. bitarray=0.8),“

*package_spec*
package versions to install into conda environment

optional arguments:
-h, –help show this help message and exit
–yes do not ask for confirmation
–dry-run only display what would have been done
-f, –force force install (even when package already installed), implies –no-deps
–file FILE read package versions from FILE
–no-deps do not install dependencies
-m, –mkdir create prefix directory if necessary
–no-pip do not use pip to install if conda fails
–use-local use locally built packages
-c CHANNEL, –channel CHANNEL
additional channel to search for packages. These are URLs searched in the order they are given (including file:// for local directories). Then, the defaults or channels from .condarc are searched (unless –override-channels is given). You can use ‘defaults’ to get the default packages for conda, and ‘system’ to get the system packages, which also takes .condarc into account. You can also use any name and the .condarc channel_alias value will be prepended. The default channel_alias is http://conda.binstar.org/
–override-channels Do not search default or .condarc channels. Requires –channel.
-n NAME, –name NAME name of environment (in ROOT_DIR/envs)
-p PATH, –prefix PATH
full path to environment prefix (default: ROOT_DIR)
-q, –quiet do not display progress bar

examples:
conda install -n myenv scipy

 

pip install D:\Python\Software\OR\numpy-1.13.3+mkl-cp36-cp36m-win_amd64.whl

reference:

https://conda.io/docs/user-guide/tasks/manage-environments.html

https://conda.io/docs/user-guide/tasks/manage-python.html

https://stackoverflow.com/questions/30170468/how-to-run-spyder-in-virtual-environment

solve LP problem with Python

I need to solve a  problem,  at first I tried to use VBA, while I found it’s a Linear Programming or  nonlinear programming problem.  Excel does not works for the problem because of it beyound the limit of Excel,  then I think I may need to use Python to solve this problem.

I read some articles in stackoverflow and want to try cvxopt and cvxpy.

I visited official website of cvxopt(www.cvxopt.org) and cvxpy(www.cvxpy.org), and found they does not support Python 3.6,  thanks to  https://www.lfd.uci.edu/~gohlke/pythonlibs , which provide support version for python 3.6.

well, I may also need to try pyscipopt in Python.