我的非凸QP无法获得双重值

时间:2015-03-19 04:30:37

标签: c++ cplex

如何让CPLEX在二次程序中求解双值?它目前给我错误,我的程序是混合整数,实际上它不是。我想出了一个简单的例子如下: max z = x ^ 2 + 2x + y s.t 0&lt; = x&lt; = 10; 0 <= y <= 10

下面是我在cplex c ++中的代码:

IloEnv env;

IloNumVar x(env, 0, IloInfinity);
IloNumVar y(env, 0, IloInfinity);

IloExpr obj(env);
obj = x*x + 2*x + y;

IloModel model(env);
model.add(IloMaximize(env,obj));

IloRange r1(env, 0, x, 10);
IloRange r2(env, 0, y, 10);

model.add(r1);
model.add(r2);

IloCplex cplex(model);
cplex.setOut(env.getNullStream());
cplex.setWarning(env.getNullStream());
cplex.setParam(IloCplex::Param::SolutionTarget,IloCplex::SolutionOptimalGlobal);

try{
    cplex.solve();
    env.out() << "x: " << cplex.getValue(x) << endl;
    env.out() << "y: " << cplex.getValue(y) << endl;
    env.out() << "Dual r1: " << cplex.getDual(r1) << endl;
    env.out() << "Dual r2: " << cplex.getDual(r2) << endl;  

    } catch (IloException& e) {
    std::cerr << "IloException: " << e << endl;
} catch (std::exception& e) {
    std::cerr << "Standard exception: " << e.what() << endl;
} catch (...) {
    std::cerr << "Some other exception!" << endl;
}

虽然cplex能够求解最优解,但却无法生成双值。错误消息是“Cplex错误1017:不适用于混合整数程序。

1 个答案:

答案 0 :(得分:1)

您没有指定所使用的CPLEX版本。让我尝试最新的12.9。这是您的问题:

$ cat foo.lp
Maximize
 obj1: 2 x + y + [ 2 x ^2 ] / 2
Subject To
 c1: x <= 10
 c2: y <= 10
End

这是将OptimalityTarget参数设置为3以获得最佳全局解决方案时的CPLEX日志:

$ cplex -c "read foo.lp" "set optimalitytarget 3" "optimize"

Welcome to IBM(R) ILOG(R) CPLEX(R) Interactive Optimizer 12.9.0.0
  with Simplex, Mixed Integer & Barrier Optimizers
5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 5655-Y21
Copyright IBM Corp. 1988, 2019.  All Rights Reserved.

Type 'help' for a list of available commands.
Type 'help' followed by a command name for more
information on commands.

CPLEX> Problem 'foo.lp' read.
Read time = 0.00 sec. (0.00 ticks)
CPLEX> New value for type of solution CPLEX will attempt to compute: 3
CPLEX> CPXPARAM_OptimalityTarget                        3
Warning: global optimality target changes problem type to MIQP.
Found incumbent of value 0.000000 after 0.00 sec. (0.00 ticks)
Tried aggregator 1 time.
MIQP Presolve eliminated 2 rows and 2 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.00 ticks)

Root node processing (before b&c):
  Real time             =    0.00 sec. (0.00 ticks)
Parallel b&c, 4 threads:
  Real time             =    0.00 sec. (0.00 ticks)
  Sync time (average)   =    0.00 sec.
  Wait time (average)   =    0.00 sec.
                          ------------
Total (root+branch&cut) =    0.00 sec. (0.00 ticks)

Solution pool: 2 solutions saved.

MIP - Integer optimal solution:  Objective =  1.3000000000e+02
Solution time =    0.00 sec.  Iterations = 0  Nodes = 0
Deterministic time = 0.00 ticks  (2.00 ticks/sec)

CPLEX>

您会注意到以下警告(您看不到它,因为您禁用了警告流):

Warning: global optimality target changes problem type to MIQP.

为了将非凸QP求解为全局最优值,CPLEX必须将问题视为混合整数QP。而且双值不适用于混合整数问题。

要获得解决方案的双重值,您可以将问题转为固定的MIQP:将问题简化为具有固定范围的固定问题的QP。然后要求局部最优(否则问题将再次变为MIQP),然后重新解决。这是在Interactive中的样子:

$ cplex129 -c "read foo.lp" "set optimalitytarget 3" "optimize" \
              "change problem fixed_miqp" "set optimalitytarget 2" \
              "optimize" "display solution dual *"

[...]

CPLEX> MIQP problem relaxed to QP with fixed integer variables using
incumbent solution.
CPLEX> New value for type of solution CPLEX will attempt to compute: 2
CPLEX> CPXPARAM_OptimalityTarget                        2
Note: Q in objective is not positive semi-definite.
Tried aggregator 1 time.
QP Presolve eliminated 2 rows and 2 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.00 ticks)
Barrier time = 0.00 sec. (0.00 ticks)

Total time on 4 threads = 0.00 sec. (0.00 ticks)

Barrier - Optimal:  Objective =  1.3000000000e+02
Solution time =    0.00 sec.  Iterations = 0
Deterministic time = 0.00 ticks  (0.68 ticks/sec)

CPLEX> Constraint Name             Dual Price
c1                           22.000000
c2                            1.000000
CPLEX>