← Back to Tutorials
Advanced• 40 minutes
Advanced Optimization Strategies
Multi-strategy optimization, Private Learning Store, performance tuning, and AI agent integration.
Multi-Strategy Optimization
For complex, multimodal landscapes, run multiple strategies in parallel and dynamically allocate budget to the best performer:
Multi-strategy optimization
from sematryx import optimize
# Multi-strategy optimization for complex landscapes
# Use 'auto' strategy to let Sematryx select the best approach
result = optimize(
objective_function=multimodal_function,
bounds=bounds,
strategy="auto", # Automatically select best strategy
max_evaluations=5000
)
# Results show which strategy was used
print(f"Strategy used: {result.strategy_used}")
print(f"Solution: {result.solution}")
print(f"Value: {result.objective_value}")Private Learning Store
Advanced configuration for your organization's private learning store:
Private Learning Store
from sematryx import optimize
# Optimization with private learning
result = optimize(
objective_function=proprietary_function,
bounds=bounds,
# Learning configuration
learning={
'read_from_public': True, # Benefit from public patterns
'read_from_private': True, # Use your private patterns
'write_to_public': False, # Keep your patterns private
'write_to_private': True # Save to private store
}
)
# Check learning operations
if result.learning_operations:
print(f"Learning operations: {result.learning_operations}")Performance Tuning
Optimize for speed with parallel evaluation, caching, and early termination:
Performance configuration
from sematryx import optimize
# Performance-optimized configuration
result = optimize(
objective_function=fast_function,
bounds=bounds,
# Resource limits
max_evaluations=5000,
# Minimal explanations for speed
explanation_level=1
)
# Performance metrics
print(f"Evaluations: {result.evaluations_used}")
print(f"Duration: {result.duration_seconds:.2f}s")
print(f"Strategy: {result.strategy_used}")AI Agent Integration
Sematryx is designed to work as an optimization backend for AI agents via MCP or REST API:
Agent integration
# MCP Integration for AI Agents
# Your AI agents can call Sematryx directly via MCP or REST
# Example: Claude using sematryx_optimize via MCP
"""
User: Find the minimum of f(x,y) = x² + y² - x*y in [-5, 5]
Claude (via MCP):
<tool_call>
sematryx_optimize({
"expression": "x**2 + y**2 - x*y",
"variables": ["x", "y"],
"bounds": [[-5, 5], [-5, 5]],
"max_evaluations": 1000
})
</tool_call>
Result: {"optimal_value": 0.0, "optimal_solution": {"x": 0.0, "y": 0.0}, ...}
"""
# REST API for any AI system
import requests
response = requests.post(
'https://api.sematryx.com/v1/optimize',
headers={'Authorization': f'Bearer {api_key}'},
json={
'objective_function': 'sphere',
'variables': ['x1', 'x2', 'x3'],
'bounds': [[-5.0, 5.0], [-5.0, 5.0], [-5.0, 5.0]],
'max_evaluations': 2000,
'strategy': 'auto'
}
)
result = response.json()
print(f"Optimal value: {result['optimal_value']}")
print(f"Solution: {result['optimal_solution']}")
if 'explanation' in result:
print(f"Explanation: {result['explanation']}")Agent-Friendly Features
- ✓Natural language explanations: Agents can relay results to users
- ✓Structured results: Easy for agents to parse and reason about
- ✓MCP protocol: Native integration with Claude, Cursor, etc.
Callbacks & Monitoring
Monitor optimization progress in real-time with callbacks:
Callbacks and monitoring
import requests, time
API_KEY = "smtrx_..."
BASE = "https://api.sematryx.com"
headers = {"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"}
# Start a long-running optimization
resp = requests.post(f"{BASE}/v1/optimize", headers=headers, json={
"objective_function": "rastrigin",
"variables": ["x1", "x2", "x3", "x4", "x5"],
"bounds": [[-5.12, 5.12]] * 5,
"max_evaluations": 10000,
"strategy": "auto"
})
resp.raise_for_status()
op_id = resp.json()["operation_id"]
print(f"Started: {op_id}")
# Poll for progress with early stopping
start_time = time.time()
prev_evals = 0
while True:
result = requests.get(
f"{BASE}/v1/optimize/result/{op_id}", headers=headers
).json()
status = result.get("status")
evals = result.get("evaluations_used", 0)
if evals > prev_evals:
best = result.get("optimal_value")
elapsed = time.time() - start_time
print(f" [{elapsed:.1f}s] {evals} evals, best={best}")
prev_evals = evals
if status in ("completed", "failed"):
break
time.sleep(2)
if result["status"] == "completed":
print(f"Done: {result['optimal_value']} at {result['optimal_solution']}")
else:
print(f"Failed: {result.get('error')}")Debugging Tips
When optimizations don't converge as expected, use debug mode:
Debugging optimization
from sematryx import optimize
# Debug configuration with maximum explanations
result = optimize(
objective_function=problematic_function,
bounds=bounds,
# Maximum explanations for debugging
explanation_level=4, # Full audit trail
# Additional debugging info
max_evaluations=1000
)
# Access debug information
if not result.success:
print(f"Optimization failed")
print(f"Explanation: {result.explanation}")
if result.raw_response:
print(f"Raw response: {result.raw_response}")
# Check audit trail
if result.audit_id:
print(f"Audit ID: {result.audit_id}")🎉 Tutorial Complete!
You've completed all Sematryx tutorials! You now know how to:
- ✓ Set up optimization problems with constraints
- ✓ Configure Sematryx Intelligence
- ✓ Interpret results and explanations
- ✓ Use the REST API for real integrations
- ✓ Apply advanced strategies for complex problems