VOCS data structure¶
Variables, Objectives, Constraints, and other Settings (VOCS) helps define our optimization problems.
In [1]:
Copied!
from xopt.vocs import VOCS
from xopt.vocs import (
get_objective_data,
random_inputs,
get_constraint_data,
get_feasibility_data,
normalize_inputs,
denormalize_inputs,
)
from gest_api.vocs import MaximizeObjective
import pandas as pd
import numpy as np
import yaml
from xopt.vocs import VOCS
from xopt.vocs import (
get_objective_data,
random_inputs,
get_constraint_data,
get_feasibility_data,
normalize_inputs,
denormalize_inputs,
)
from gest_api.vocs import MaximizeObjective
import pandas as pd
import numpy as np
import yaml
In [2]:
Copied!
Y = """
variables:
a: [0, 1e3] # Note that 1e3 usually parses as a str with YAML.
b: [-1, 1]
objectives:
c: maximize
d: minimize
constraints:
e: ['Less_than', 2]
f: ['greater_than', 0]
constants:
g: 123
"""
vocs = VOCS(**yaml.safe_load(Y))
vocs
Y = """
variables:
a: [0, 1e3] # Note that 1e3 usually parses as a str with YAML.
b: [-1, 1]
objectives:
c: maximize
d: minimize
constraints:
e: ['Less_than', 2]
f: ['greater_than', 0]
constants:
g: 123
"""
vocs = VOCS(**yaml.safe_load(Y))
vocs
Out[2]:
VOCS(variables={'a': ContinuousVariable(dtype=None, default_value=None, domain=[0.0, 1000.0]), 'b': ContinuousVariable(dtype=None, default_value=None, domain=[-1.0, 1.0])}, objectives={'c': MaximizeObjective(dtype=None), 'd': MinimizeObjective(dtype=None)}, constraints={'e': LessThanConstraint(dtype=None, value=2.0), 'f': GreaterThanConstraint(dtype=None, value=0.0)}, constants={'g': Constant(dtype=None, value=123)}, observables={})
In [3]:
Copied!
# as dict
dict(vocs)
# as dict
dict(vocs)
Out[3]:
{'variables': {'a': ContinuousVariable(dtype=None, default_value=None, domain=[0.0, 1000.0]),
'b': ContinuousVariable(dtype=None, default_value=None, domain=[-1.0, 1.0])},
'objectives': {'c': MaximizeObjective(dtype=None),
'd': MinimizeObjective(dtype=None)},
'constraints': {'e': LessThanConstraint(dtype=None, value=2.0),
'f': GreaterThanConstraint(dtype=None, value=0.0)},
'constants': {'g': Constant(dtype=None, value=123)},
'observables': {}}
In [4]:
Copied!
# re-parse dict
vocs2 = VOCS(**dict(vocs))
# re-parse dict
vocs2 = VOCS(**dict(vocs))
In [5]:
Copied!
# Check that these are the same
vocs2 == vocs
# Check that these are the same
vocs2 == vocs
Out[5]:
True
In [6]:
Copied!
# This replaces the old vocs["variables"]
getattr(vocs, "variables")
# This replaces the old vocs["variables"]
getattr(vocs, "variables")
Out[6]:
{'a': ContinuousVariable(dtype=None, default_value=None, domain=[0.0, 1000.0]),
'b': ContinuousVariable(dtype=None, default_value=None, domain=[-1.0, 1.0])}
In [7]:
Copied!
vocs.objectives["c"]
vocs.objectives["c"]
Out[7]:
MaximizeObjective(dtype=None)
In [8]:
Copied!
isinstance(vocs.objectives["c"], MaximizeObjective)
isinstance(vocs.objectives["c"], MaximizeObjective)
Out[8]:
True
In [9]:
Copied!
# json
vocs.model_dump_json()
# json
vocs.model_dump_json()
Out[9]:
'{"variables":{"a":{"dtype":null,"default_value":null,"domain":[0.0,1000.0],"type":"ContinuousVariable"},"b":{"dtype":null,"default_value":null,"domain":[-1.0,1.0],"type":"ContinuousVariable"}},"objectives":{"c":{"dtype":null,"type":"MaximizeObjective"},"d":{"dtype":null,"type":"MinimizeObjective"}},"constraints":{"e":{"dtype":null,"value":2.0,"type":"LessThanConstraint"},"f":{"dtype":null,"value":0.0,"type":"GreaterThanConstraint"}},"constants":{"g":{"dtype":null,"value":123,"type":"Constant"}},"observables":{}}'
Objective Evaluation¶
In [10]:
Copied!
data = pd.DataFrame(random_inputs(vocs, 10))
# Add some outputs
data["c"] = data["a"] + data["b"]
data["d"] = data["a"] - data["b"]
data["e"] = data["a"] * 2 + data["b"] * 2
data["f"] = data["a"] * 2 - data["b"] * 2
data.index = np.arange(len(data)) + 5 # custom index
data
data = pd.DataFrame(random_inputs(vocs, 10))
# Add some outputs
data["c"] = data["a"] + data["b"]
data["d"] = data["a"] - data["b"]
data["e"] = data["a"] * 2 + data["b"] * 2
data["f"] = data["a"] * 2 - data["b"] * 2
data.index = np.arange(len(data)) + 5 # custom index
data
Out[10]:
| a | b | g | c | d | e | f | |
|---|---|---|---|---|---|---|---|
| 5 | 686.111729 | 0.404091 | 123 | 686.515819 | 685.707638 | 1373.031639 | 1371.415276 |
| 6 | 828.513441 | 0.495985 | 123 | 829.009426 | 828.017456 | 1658.018851 | 1656.034912 |
| 7 | 987.710668 | -0.362235 | 123 | 987.348433 | 988.072903 | 1974.696866 | 1976.145807 |
| 8 | 693.327213 | 0.064971 | 123 | 693.392184 | 693.262242 | 1386.784367 | 1386.524484 |
| 9 | 842.746407 | 0.188921 | 123 | 842.935328 | 842.557486 | 1685.870656 | 1685.114972 |
| 10 | 413.414235 | 0.527608 | 123 | 413.941843 | 412.886628 | 827.883685 | 825.773255 |
| 11 | 84.452671 | 0.289725 | 123 | 84.742396 | 84.162946 | 169.484792 | 168.325892 |
| 12 | 564.509208 | -0.893571 | 123 | 563.615637 | 565.402779 | 1127.231274 | 1130.805559 |
| 13 | 496.084695 | -0.318707 | 123 | 495.765989 | 496.403402 | 991.531978 | 992.806804 |
| 14 | 215.556041 | -0.694029 | 123 | 214.862012 | 216.250069 | 429.724023 | 432.500139 |
In [11]:
Copied!
# These are in standard form for minimization, available as a function
get_objective_data(vocs, data)
# These are in standard form for minimization, available as a function
get_objective_data(vocs, data)
Out[11]:
| objective_c | objective_d | |
|---|---|---|
| 5 | -686.515819 | 685.707638 |
| 6 | -829.009426 | 828.017456 |
| 7 | -987.348433 | 988.072903 |
| 8 | -693.392184 | 693.262242 |
| 9 | -842.935328 | 842.557486 |
| 10 | -413.941843 | 412.886628 |
| 11 | -84.742396 | 84.162946 |
| 12 | -563.615637 | 565.402779 |
| 13 | -495.765989 | 496.403402 |
| 14 | -214.862012 | 216.250069 |
In [12]:
Copied!
# use the to_numpy() method to convert for low level use.
get_objective_data(vocs, data).to_numpy()
# use the to_numpy() method to convert for low level use.
get_objective_data(vocs, data).to_numpy()
Out[12]:
array([[-686.51581927, 685.70763784],
[-829.00942556, 828.01745576],
[-987.34843323, 988.07290339],
[-693.39218364, 693.26224186],
[-842.93532825, 842.55748606],
[-413.94184264, 412.88662751],
[ -84.74239594, 84.16294619],
[-563.61563694, 565.40277949],
[-495.76598884, 496.4034021 ],
[-214.86201169, 216.25006938]])
In [13]:
Copied!
get_constraint_data(vocs, data)
get_constraint_data(vocs, data)
Out[13]:
| constraint_e | constraint_f | |
|---|---|---|
| 5 | 1371.031639 | -1371.415276 |
| 6 | 1656.018851 | -1656.034912 |
| 7 | 1972.696866 | -1976.145807 |
| 8 | 1384.784367 | -1386.524484 |
| 9 | 1683.870656 | -1685.114972 |
| 10 | 825.883685 | -825.773255 |
| 11 | 167.484792 | -168.325892 |
| 12 | 1125.231274 | -1130.805559 |
| 13 | 989.531978 | -992.806804 |
| 14 | 427.724023 | -432.500139 |
In [14]:
Copied!
get_feasibility_data(vocs, data)
get_feasibility_data(vocs, data)
Out[14]:
| feasible_e | feasible_f | feasible | |
|---|---|---|---|
| 5 | False | True | False |
| 6 | False | True | False |
| 7 | False | True | False |
| 8 | False | True | False |
| 9 | False | True | False |
| 10 | False | True | False |
| 11 | False | True | False |
| 12 | False | True | False |
| 13 | False | True | False |
| 14 | False | True | False |
In [15]:
Copied!
# normalize inputs to unit domain [0,1]
normed_data = normalize_inputs(vocs, data)
normed_data
# normalize inputs to unit domain [0,1]
normed_data = normalize_inputs(vocs, data)
normed_data
Out[15]:
| a | b | |
|---|---|---|
| 5 | 0.686112 | 0.702045 |
| 6 | 0.828513 | 0.747992 |
| 7 | 0.987711 | 0.318882 |
| 8 | 0.693327 | 0.532485 |
| 9 | 0.842746 | 0.594461 |
| 10 | 0.413414 | 0.763804 |
| 11 | 0.084453 | 0.644862 |
| 12 | 0.564509 | 0.053214 |
| 13 | 0.496085 | 0.340647 |
| 14 | 0.215556 | 0.152986 |
In [16]:
Copied!
# and denormalize
denormalize_inputs(vocs, normed_data)
# and denormalize
denormalize_inputs(vocs, normed_data)
Out[16]:
| a | b | |
|---|---|---|
| 5 | 686.111729 | 0.404091 |
| 6 | 828.513441 | 0.495985 |
| 7 | 987.710668 | -0.362235 |
| 8 | 693.327213 | 0.064971 |
| 9 | 842.746407 | 0.188921 |
| 10 | 413.414235 | 0.527608 |
| 11 | 84.452671 | 0.289725 |
| 12 | 564.509208 | -0.893571 |
| 13 | 496.084695 | -0.318707 |
| 14 | 215.556041 | -0.694029 |
Error handling¶
In [17]:
Copied!
Y = """
variables:
a: [0, 1e3] # Note that 1e3 usually parses as a str with YAML.
b: [-1, 1]
objectives:
c: maximize
d: minimize
constraints:
e: ['Less_than', 2]
f: ['greater_than', 0]
constants:
g: 1234
"""
vocs = VOCS(**yaml.safe_load(Y))
Y = """
variables:
a: [0, 1e3] # Note that 1e3 usually parses as a str with YAML.
b: [-1, 1]
objectives:
c: maximize
d: minimize
constraints:
e: ['Less_than', 2]
f: ['greater_than', 0]
constants:
g: 1234
"""
vocs = VOCS(**yaml.safe_load(Y))
In [18]:
Copied!
d = {"a": [1, 2, 3]}
df = pd.DataFrame(d)
df2 = pd.DataFrame(df).copy()
df2["b"] = np.nan
df2["b"] - 1
d = {"a": [1, 2, 3]}
df = pd.DataFrame(d)
df2 = pd.DataFrame(df).copy()
df2["b"] = np.nan
df2["b"] - 1
Out[18]:
0 NaN 1 NaN 2 NaN Name: b, dtype: float64
In [19]:
Copied!
data["a"] = np.nan
data["a"] = np.nan
In [20]:
Copied!
a = 2
def f(x=a):
return x
a = 99
f()
a = 2
def f(x=a):
return x
a = 99
f()
Out[20]:
2
In [21]:
Copied!
pd.DataFrame(6e66, index=[1, 2, 3], columns=["A"])
pd.DataFrame(6e66, index=[1, 2, 3], columns=["A"])
Out[21]:
| A | |
|---|---|
| 1 | 6.000000e+66 |
| 2 | 6.000000e+66 |
| 3 | 6.000000e+66 |
In [22]:
Copied!
# These are in standard form for minimization
data = pd.DataFrame({"c": [1, 2, 3, 4]}, index=[9, 3, 4, 5])
get_objective_data(vocs, data)
# These are in standard form for minimization
data = pd.DataFrame({"c": [1, 2, 3, 4]}, index=[9, 3, 4, 5])
get_objective_data(vocs, data)
Out[22]:
| objective_c | objective_d | |
|---|---|---|
| 9 | -1.0 | inf |
| 3 | -2.0 | inf |
| 4 | -3.0 | inf |
| 5 | -4.0 | inf |