Getting Started with Sematryx
In this tutorial, you'll solve your first optimization problem and learn the core concepts of Sematryx.
What You'll Learn
We'll solve a simple optimization problem to introduce you to the core concepts:
- Defining objective functions
- Setting search bounds
- Running optimization with Sematryx
- Understanding optimization results
- Configuring the AEAO Tetrad
Prerequisites
Before You Start
- ✅ Python 3.8+ installed
- ✅ Basic Python programming knowledge
- ✅ Understanding of optimization concepts (minimization/maximization)
For API access, you'll need an API key from the API Keys page
Step 1: Install AEAO
Install Sematryx using pip:
pip install aeaoStep 2: Your First Optimization
Let's solve a classic optimization problem - minimizing the sphere function. This is a simple 2D problem where we want to find the point closest to the origin:
from sematryx import sematryx
# Define your objective function
def sphere(x):
"""Minimize the sum of squares (classic optimization test)"""
return sum(xi**2 for xi in x)
# Run optimization
result = aeao(
objective_function=sphere,
bounds=[[-5, 5], [-5, 5]], # 2D problem
max_evaluations=1000
)
print(f"Best solution: {result['best_solution']}")
print(f"Best fitness: {result['best_fitness']}")
print(f"Evaluations used: {result['evaluations']}")What's Happening?
- Objective Function:
sphere(x)calculates sum of squares - Bounds: Search space is [-5, 5] for each dimension
- Max Evaluations: Sematryx will evaluate the function up to 1000 times
- Result: Best solution found and its objective value
Step 3: Understanding the Results
Sematryx returns a dictionary with optimization results:
{
"success": true,
"best_solution": [0.001, -0.002],
"best_fitness": 0.000005,
"evaluations": 847,
"duration_seconds": 1.23,
"strategy_used": "shgo",
"tetrad_config": {
"use_agentic_intelligence": false,
"use_expository_intelligence": true,
"use_autodidactic_intelligence": false,
"use_domain_extension": true
}
}Result Fields
- best_solution: Optimal parameter values found (should be near [0, 0] for sphere)
- best_fitness: Best objective value (should be near 0)
- evaluations: Number of function evaluations used
- strategy_used: Optimization algorithm Sematryx selected
- tetrad_config: Which AEAO Tetrad features were active
Step 4: Configure the AEAO Tetrad
Sematryx's AEAO Engine is built on four pillars of intelligence. Enable them to enhance your optimization:
from sematryx import sematryx
# Enable specific tetrad pillars
result = aeao(
objective_function=sphere,
bounds=[[-5, 5], [-5, 5]],
use_agentic_intelligence=True, # Multi-agent coordination
use_autodidactic_intelligence=True, # Self-improvement
explanation_level=3 # Detailed explanations
)The AEAO Tetrad
- 🤖 Agentic Intelligence: Multiple AI agents collaborate to select the best strategy
- 📖 Expository Intelligence: Get explanations of optimization decisions (levels 0-5)
- 🧠 Autodidactic Intelligence: System learns and improves from optimization experience
- 🏗️ Domain Extension: Specialized libraries for business domains (enabled by default)
Step 5: Real-World Example
Here's a more practical example - optimizing a portfolio allocation:
from sematryx import sematryx
import numpy as np
# Real-world example: Portfolio optimization
def portfolio_risk(weights):
"""
Minimize portfolio risk given expected returns
weights: [w1, w2, w3] - allocation percentages
"""
returns = np.array([0.12, 0.08, 0.15]) # Expected returns
covariance = np.array([[0.04, 0.01, 0.02],
[0.01, 0.03, 0.01],
[0.02, 0.01, 0.05]])
# Portfolio return
portfolio_return = np.dot(weights, returns)
# Portfolio risk (variance)
portfolio_variance = np.dot(weights, np.dot(covariance, weights))
# Risk-adjusted return (negative for minimization)
return -portfolio_return / np.sqrt(portfolio_variance)
# Optimize with constraints (weights sum to 1)
result = aeao(
objective_function=portfolio_risk,
bounds=[[0, 1], [0, 1], [0, 1]],
max_evaluations=2000
)
print(f"Optimal allocation: {result['best_solution']}")
print(f"Risk-adjusted return: {-result['best_fitness']}")This example shows how to optimize a real-world problem with multiple variables and constraints.
🎉 Congratulations!
You've successfully solved your first optimization problem with Sematryx! You now understand how to define problems, run optimization, and configure the AEAO Tetrad.