Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
adekusar-drl authored and manoelmarques committed Nov 9, 2020
1 parent 09abfcb commit 9fe46a2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
9 changes: 5 additions & 4 deletions qiskit/optimization/algorithms/admm_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def solve(self, problem: QuadraticProgram) -> ADMMOptimizationResult:
# map integer variables to binary variables
from ..converters.integer_to_binary import IntegerToBinary
int2bin = IntegerToBinary()
original_variables = problem.variables
original_problem = problem
problem = int2bin.convert(problem)

# we deal with minimization in the optimizer, so turn the problem to minimization
Expand Down Expand Up @@ -377,15 +377,16 @@ def solve(self, problem: QuadraticProgram) -> ADMMOptimizationResult:
objective_value = objective_value * sense

# convert back integer to binary
base_result = OptimizationResult(solution, objective_value, original_variables,
base_result = OptimizationResult(solution, objective_value, problem.variables,
OptimizationResultStatus.SUCCESS)
base_result = int2bin.interpret(base_result)

# third parameter is our internal state of computations.
result = ADMMOptimizationResult(x=base_result.x, fval=base_result.fval,
variables=base_result.variables,
variables=original_problem.variables,
state=self._state,
status=self._get_feasibility_status(problem, base_result.x))
status=self._get_feasibility_status(original_problem,
base_result.x))

# debug
self._log.debug("solution=%s, objective=%s at iteration=%s",
Expand Down
23 changes: 23 additions & 0 deletions test/optimization/test_admm.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,26 @@ def test_admm_setters_getters(self):
params = ADMMParameters(maxiter=12)
optimizer.parameters = params
self.assertEqual(optimizer.parameters.maxiter, 12)

def test_integer_variables(self):
"""Tests ADMM with integer variables."""
mdl = Model('integer-variables')

v = mdl.integer_var(lb=5, ub=20, name='v')
w = mdl.continuous_var(name='w', lb=0.)

mdl.minimize(v + w)
op = QuadraticProgram()
op.from_docplex(mdl)

solver = ADMMOptimizer()
solution = solver.solve(op)

self.assertIsNotNone(solution)
self.assertIsInstance(solution, ADMMOptimizationResult)
self.assertIsNotNone(solution.x)
np.testing.assert_almost_equal([5., 0.], solution.x, 3)
self.assertIsNotNone(solution.fval)
np.testing.assert_almost_equal(5., solution.fval, 3)
self.assertIsNotNone(solution.state)
self.assertIsInstance(solution.state, ADMMState)

0 comments on commit 9fe46a2

Please sign in to comment.