lava-nc / lava-optimization

Neuromorphic mathematical optimization with Lava
https://lava-nc.org/optimization.html
BSD 3-Clause "New" or "Revised" License
51 stars 26 forks source link

Solver returns the wrong cost for the best solution #172

Closed AlessandroPierro closed 1 year ago

AlessandroPierro commented 1 year ago

Describe the bug The cost returned by the run report of OptimizationSolver doesn't match the cost computed on manually for QUBO.

To reproduce current behavior Steps to reproduce the behavior:

  1. Run this code with Loihi backend
    
    from lava.lib.optimization.problems.problems import QUBO
    from lava.lib.optimization.solvers.generic.solver import OptimizationSolver
    import numpy as np

Q = np.array([[11, -5, 2, 0, -6], [-5, -13, -1, -3, 10], [2, -1, 0, 2, 10], [0, -3, 2, 2, -1], [-6, 10, 10, -1, -3]])

problem = QUBO(Q) solver = OptimizationSolver(problem)

solution = solver.solve(timeout=50, target_cost=-10000, hyperparameters={ }, backend="Loihi2", )

report = solver.last_run_report

print(report["cost"]) print(report["best_state"] @ Q @ report["best_state"]) print(problem.evaluate_cost(report["best_state"]))



**Expected behavior**
The values printed in the last three lines should be identical.

**Environment (please complete the following information):**
 - Device: Intel cloud
 - OS: Linux
 - Lava version: 0.6.1

##Tasks
- [x] [AP, 1ih] Replicate bug
- [x] [AP, 1ih] Rule-out cause at high level API (OptimizationSolver)
- [x] [GG, 4ih] Rule-out cause at low level layers (as deep as needed)
- [ ] [GG, 2ih] Solve bug and add test to catch future errors
- [ ] [AP/GG, 3ih]Prepare for release 
phstratmann commented 1 year ago

This is due to the fact that for mixed Dense matrices, only even numbers are allowed. To fix this, we need an input validation stage, maybe including an automatic scaling of the QUBO matrix.