Solutions

Solutions

When you call solve_one() or solve(), Qaekwy returns a Solution object (or a list of them). This object represents a “satisfied state"โ€”a snapshot of the model where every variable has been assigned a specific value that respects all your constraints.

In Qaekwy, the Solution object is designed to be as unobtrusive as possible, behaving like a native Python object while maintaining the structure of your original model.

The solution object is inherited from standard Python dictionary, and therefore is serializable to JSon, or usable as you would use any Python dict object.

Attribute-Based Access

The most intuitive way to retrieve values is via attribute access. Every variable or array name you defined in your Model becomes a property of the Solution object.

Example

This example demonstrates how the Solution object maps variable names directly to their assigned results, including support for standard Python indexing for arrays.

import qaekwy as qw

# Instantiation
m = qw.Model()

# Variables definition
x = m.integer_variable("x", (0, 100))
y = m.integer_variable("y",  (0, 100))
a = m.integer_array("a", 3, (0, 100))

# Constraint definition
m.constraint(x + y >= 5 + a[2])

# Solving the model
solution = m.solve_one()

# Accessing assigned variables of the solution,
# the Python way
print(f"The value of x is {solution.x}")
print(f"The value of y is {solution.y}")
print(f"The value of a[0] is {solution.a[0]}")
print(f"The value of a[1] is {solution.a[1]}")
print(f"The value of a[2] is {solution.a[2]}")

Matrix Access

If you used the integer_matrix helper during the modeling phase, the Solution object preserves that 2D structure. You can access matrix values using the same [row][col] syntax used during model definition.

# Assuming a matrix defined as: grid = m.integer_matrix("grid", rows=9, cols=9, ...)

# Solving the model
solution = m.solve_one()

# Accessing a specific coordinate (Row 0, Column 7)
val = solution.grid[0][7]

Dictionary-Based access

# Solving the model
solution = m.solve_one()

# Dictionary-like browsing
for variable_name, variable_value in solution.items():
    print(f"{variable_name} value is {variable_value}")

Solution Utility

Beyond variable access, the Solution object provides utility method to help you quickly inspect your results:

  • .pretty_print(): Outputs a formatted, human-readable summary of all variable assignments in the solution. This is the fastest way to verify your results during development.
# Quick summary
solution.pretty_print()