Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 19, 2025

This PR adds a new comprehensive example demonstrating how to use the set_on_model callback feature with Z3's Optimize class. The example was requested in issue #207 to help users understand how to monitor optimization progress and implement custom termination conditions.

What's Added

A new example file 06 - Optimize with Model Callbacks.md under the Python Example Programs section that includes:

Basic Usage: Simple callback setup for monitoring optimization progress

def on_model(m):
    print("Found model:", m)
    sys.stdout.flush()
    
opt.set_on_model(on_model)

Multiple Objectives: Resource allocation example showing how to track model evolution with competing optimization goals

Early Termination: Custom monitoring class demonstrating how to implement termination logic based on model count or quality thresholds

MaxSAT Integration: Practical weighted MaxSAT example with solution collection and analysis, particularly useful with local search engines

External Files: Implementation based on the original issue's bare bones example, showing integration with SMT-LIB files and various optimization engine configurations

Key Benefits

  • Provides working code examples for monitoring optimization progress
  • Shows practical use cases for the callback functionality
  • Demonstrates integration with different optimization engines (MaxSAT, local search)
  • Includes the exact example from the issue description
  • Follows existing documentation structure and formatting

The examples show how set_on_model callbacks are particularly valuable when using iterative optimization engines that find many intermediate solutions during the search process.

Complete example page

Fixes #207.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Contributor Author

Copilot AI commented Sep 19, 2025

@NikolajBjorner 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Co-authored-by: NikolajBjorner <3085284+NikolajBjorner@users.noreply.github.com>
Copilot AI changed the title [WIP] Add an example that illustrates set_on_model for Optimize Add comprehensive example for Optimize set_on_model callback functionality Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an example that illustrates set_on_model for Optimize

2 participants