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:
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}")Landscape Analysis
Analyze your problem's landscape before optimization to choose the right strategy:
from sematryx import analyze_landscape
# Analyze problem landscape before optimization
# Helps choose the right strategy
analysis = analyze_landscape(
objective_function=my_function,
bounds=bounds,
n_samples=500, # Sample points for analysis
analysis_depth='full' # 'quick', 'standard', 'full'
)
# Landscape characteristics
print(f"Dimensionality: {analysis['dimensions']}")
print(f"Modality: {analysis['modality']}") # unimodal, multimodal
print(f"Smoothness: {analysis['smoothness']}") # smooth, rugged, discontinuous
print(f"Separability: {analysis['separability']}") # separable, non-separable
print(f"Conditioning: {analysis['condition_number']}")
# Strategy recommendations
print(f"Recommended strategies: {analysis['recommended_strategies']}")
print(f"Expected difficulty: {analysis['difficulty_score']}")
# Use recommendations
result = optimize(
objective_function=my_function,
bounds=bounds,
strategy=analysis['recommended_strategies'][0],
use_landscape_analysis=analysis # Reuse analysis
)Landscape Types
Private Learning Store
Advanced configuration for your organization's 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:
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}")Batch Optimization
Run multiple related optimizations concurrently with cross-problem learning:
from sematryx import batch_optimize
import asyncio
# Batch optimization for multiple related problems
problems = [
{'id': 'region_north', 'objective': obj_north, 'bounds': bounds_north},
{'id': 'region_south', 'objective': obj_south, 'bounds': bounds_south},
{'id': 'region_east', 'objective': obj_east, 'bounds': bounds_east},
{'id': 'region_west', 'objective': obj_west, 'bounds': bounds_west},
]
# Run all optimizations concurrently
results = await batch_optimize(
problems=problems,
# Shared configuration
shared_config={
'max_evaluations': 1000,
'explanation_level': 2,
'use_learning': True
},
# Batch settings
max_concurrent=4, # Run 4 at once
priority='balanced', # 'speed', 'balanced', 'cost'
# Cross-problem learning
share_learning=True, # Problems can learn from each other
learning_weight=0.3 # Weight for cross-problem knowledge
)
# Aggregate results
for result in results:
print(f"{result['problem_id']}: {result['best_fitness']}")AI Agent Integration
Sematryx is designed to work as an optimization backend for AI agents via MCP or REST API:
# MCP Integration for AI Agents
# Your AI agents can call Sematryx directly
# Example: Claude calling Sematryx via MCP
"""
User: Optimize my portfolio to minimize risk while targeting 8% return
Claude (via MCP):
<tool_call>
sematryx.optimize_portfolio({
returns: [0.12, 0.08, 0.15, 0.10],
covariance: [...],
constraints: {
min_return: 0.08,
max_position: 0.30
},
explanation_level: 3
})
</tool_call>
Result includes natural language explanation:
"Recommended allocation: 35% Asset A, 28% Asset B, 22% Asset C, 15% Asset D.
This achieves 8.2% expected return with CVaR of 12.3%. The allocation
favors Asset A due to its superior risk-adjusted return (Sharpe 1.2)
while satisfying all position limits."
"""
# 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_code': objective_code, # Or use preset
'bounds': bounds,
'explanation_level': 3,
'explanation_format': 'natural_language' # For agent consumption
}
)
result = response.json()
# result['explanation']['natural_language'] contains
# human-readable explanation agents can relay to usersAgent-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:
from sematryx import Sematryx
# For advanced monitoring, use the client API
client = Sematryx(api_key="sk-...")
result = client.optimize(
# Real-time monitoring with callbacks
def progress_callback(iteration, current_best, improvement):
"""Called after each iteration"""
print(f"Iteration {iteration}: best={current_best:.6f}, improved={improvement}")
# Return False to stop early
if current_best < target_value:
return False
return True
def strategy_callback(strategy_name, reason):
"""Called when strategy changes"""
print(f"Strategy switched to {strategy_name}: {reason}")
result = optimize(
objective_function=my_function,
bounds=bounds,
# Callbacks
callbacks={
'on_iteration': progress_callback,
'on_strategy_change': strategy_callback,
'on_constraint_violation': violation_callback,
'on_improvement': improvement_callback
},
# Streaming results (for long-running optimizations)
stream_results=True,
stream_interval=10 # Emit updates every 10 iterations
)Debugging Tips
When optimizations don't converge as expected, use debug mode:
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 domain-specific libraries
- ✓ Apply advanced strategies for complex problems