8000 add driver fo CQP. · lambe/NLP.py@9d7765e · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d7765e

Browse files
syarradpo
authored andcommitted
add driver fo CQP.
1 parent aa4ebf8 commit 9d7765e

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

nlp/drivers/nlp_cqp.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""Simple AMPL driver for CQP."""
4+
5+
import logging
6+
import sys
7+
from nlp.model.pysparsemodel import PySparseAmplModel
8+
from nlp.optimize.cqp import RegQPInteriorPointSolver
9+
from nlp.optimize.pcg import TruncatedCG
10+
from nlp.tools.logs import config_logger
11+
12+
13+
def cqp_stats(cqp):
14+
"""Obtain CQP statistics and indicate failures with negatives."""
15+
if cqp.status in ("fatol", "frtol", "gtol"):
16+
it = cqp.iter
17+
fc, gc = cqp.model.obj.ncalls, cqp.model.grad.ncalls
18+
pg = cqp.pgnorm
19+
ts = cqp.tsolve
20+
else:
21+
it = -cqp.iter
22+
fc, gc = -cqp.model.obj.ncalls, -cqp.model.grad.ncalls
23+
pg = -1.0 if cqp.pgnorm is None else -cqp.pgnorm
24+
ts = -1.0 if cqp.tsolve is None else -cqp.tsolve
25+
return (it, fc, gc, pg, ts)
26+
27+
28+
nprobs = len(sys.argv) - 1
29+
if nprobs == 0:
30+
raise ValueError("Please supply problem name as argument")
31+
32+
# Create root logger.
33+
logger = config_logger("nlp", "%(name)-3s %(levelname)-5s %(message)s")
34+
35+
# Create CQP logger.
36+
cqp_logger = config_logger("nlp.cqp",
37+
"%(name)-8s %(levelname)-5s %(message)s",
38+
level=logging.WARN if nprobs > 1 else logging.INFO)
39+
40+
if nprobs > 1:
41+
logger.info("%12s %5s %6s %8s %8s %6s %6s %5s %7s",
42+
"name", "nvar", "iter", "f", u"‖P∇f‖", "#f", u"#∇f", "stat",
43+
"time")
44+
45+
for problem in sys.argv[1:]:
46+
model = PySparseAmplModel(problem)
47+
model.compute_scaling_obj()
48+
49+
# Check for inequality- or equality-constrained problem.
50+
if model.m > 0:
51+
msg = '%s has %d linear or nonlinear constraints'
52+
logger.error(msg, model.name, model.m)
53+
continue
54+
55+
cqp = RegQPInteriorPointSolver(model, TruncatedCG, maxiter=100)
56+
try:
57+
cqp.solve()
58+
status = cqp.status
59+
niter, fcalls, gcalls, pgnorm, tsolve = cqp_stats(cqp)
60+
except:
61+
msg = sys.exc_info()[1].message
62+
status = msg if len(msg) > 0 else "xfail" # unknown failure
63+
niter, fcalls, gcalls, pgnorm, tsolve = cqp_stats(cqp)
64+
65+
logger.info("%12s %5d %6d %8.1e %8.1e %6d %6d %5s %7.3f",
66+
model.name, model.nvar, niter, cqp.f, pgnorm,
67+
fcalls, gcalls, status, tsolve)

nlp/optimize/cqp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,9 +490,9 @@ class RegQPInteriorPointSolver(object):
490490
491491
Solve a convex quadratic program of the form::
492492
493-
minimize cᵀx + 1/2 xᵀ Q x
493+
minimize cᵀx + ½ xᵀ Q x
494494
subject to A1 x + A2 s = b, (QP)
495-
s >= 0,
495+
s 0,
496496
497497
where Q is a symmetric positive semi-definite matrix, the variables
498498
x are the original problem variables and s are slack variables. Any

0 commit comments

Comments
 (0)
0