Skip to content

Commit

Permalink
Plug photosic functions (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrapin authored Jan 25, 2021
1 parent b271a36 commit 33b0386
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 45 deletions.
5 changes: 4 additions & 1 deletion nevergrad/functions/leaderboard.csv
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"bragg,16",0.932163816851,"[2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5,2.5]"
"bragg,80",0.000694261986,"[2.999999999999995,2.0,2.9999999999999982,2.0,2.9999999999999942,2.0000000000000204,2.9999999999999982,2.0000000000000204,2.999999999999997,2.0000000000000577,2.999999999999994,2.0000000000000178,2.9999999999999996,2.0000000000000258,2.9999999999999956,2.0,3.0,2.0,3.0,2.0000000000000258,2.9999999999998677,2.0,2.999999999999998,2.0000000000000524,2.999999999999998,2.0,2.999999999999731,2.0000000000000204,3.0,2.0,3.0,2.0,2.999999999999869,2.0000000000000524,2.999999999999998,2.0,2.999999999999731,2.0,2.9999999999999982,2.0,86.60255045244762,106.06599965930195,86.60255443305651,106.06600747129924,86.60257272790835,106.06594450536937,86.60261007043326,106.06596664220048,86.60256991846003,106.06601083817202,86.60251126812021,106.06605239950612,86.60252027566017,106.06598075034444,86.60259101280616,106.06599907105759,86.60256520284221,106.06597421940252,86.60257498882078,106.06598483902887,86.60258117175987,106.06597850626063,86.60257221703684,106.06597517269579,86.60257273903393,106.06600072357362,86.60255405767539,106.06597375534662,86.60256934296459,106.06596027660167,86.60259348834224,106.06598724294983,86.60256638431304,106.06597523757073,86.60257261435468,106.06600050328808,86.60255444923789,106.06596589510059,86.60257650073991,106.06599053476317]"
"bragg_as_tuple,80",0.056503784218,"[2.0,104.19050205854796,3.0,103.3871864897671,2.432368384694066,107.16845414573265,3.0,105.04397032814755,3.0,101.4115279880702,2.0,101.82340730595631,2.0,106.29835865548985,3.0,100.50409945218179,2.0,103.8534464724715,2.7907130282425876,106.34796100526032,3.0,106.3101270117349,3.0,104.8166097932767,2.0,105.20061310675672,3.0,108.05008553576533,2.0,106.65130169395327,2.0,106.79217956093568,2.0,107.57998256628777,3.0,106.11538453696315,3.0,104.18859447446258,2.0,104.61099654298489,3.0,104.34768767390064,2.0,105.010434962529,2.0,103.77385312377623,3.0,105.21975402145074,2.0,103.66175885994471,2.0,104.86077071154178,3.0,103.68862394055434,2.0,106.2706255197518,2.0,103.64438413549227,3.0,105.6950172939732,2.0,106.69119981455366,2.0,104.1415087744897,2.592088288124082,107.06731776173814,3.0,102.19253037912654,2.527507400315132,102.89774703567296,2.0,105.81418087457429,2.0,106.50427342380392,2.0,105.61415322728554,2.0,106.66208085013761,2.0,105.28693844768354]"
"cf_photosic_realistic,20",0.086025742545,"[2.0,3.0,2.1076,2.0,3.0,2.5783,2.0,3.0,2.0,3.0,90.0231,78.9789,72.8369,99.9577,82.7487,62.7583,104.1682,139.9002,93.3356,75.6039]"
"cf_photosic_reference,16",0.431072337632,"[109.72985831489979,98.18574120560555,107.42439496690479,87.99264365961669,112.531428339492,89.65278659791956,105.05127078132273,103.79772329843809,96.93018121427048,133.71819394988916,99.02177080593657,109.72456993535758,115.95956118008561,92.84831198907784,118.42356371437981,103.77850212331678]"
"chirped,16",0.594587712739,"[109.72985831489979,98.18574120560555,107.42439496690479,87.99264365961669,112.531428339492,89.65278659791956,105.05127078132273,103.79772329843809,96.93018121427048,133.71819394988916,99.02177080593657,109.72456993535758,115.95956118008561,92.84831198907784,118.42356371437981,103.77850212331678]"
"chirped,30",0.275923457222,"[89.04887416,109.54188095,89.74520725,121.81700431,179.99830918,124.38222473,95.31017129,116.0239629,92.92345776,118.06108198,179.99965859,116.89288181,88.90191494,110.30816229,93.11974992,137.42629858,118.81810084,110.74139708,85.15270955,100.9382438,81.44070951,100.6382896,84.97336252,110.59252719,134.89164276,121.84205195,89.28450356,106.72776991,85.77168797,102.33562547]"
"chirped,50",0.11656397290100001,"[93.65223418423034,118.48802644462693,107.4154177701731,146.1273996196957,104.76814425546243,122.46259169233943,101.63070554932264,132.01391635156088,113.23959177552805,134.21191762900222,104.52593631355413,123.84798946548463,99.53290830788647,124.5717299569956,114.84349635591072,136.69596308542052,94.99997481641495,107.55307917381322,82.394994109015,96.18784040922577,79.12042692721471,101.72854687985267,87.61947895483019,118.06040186972581,129.67099220470743,120.08173331893012,89.30208035845052,105.27994144055239,83.02744104990198,98.53738160919845,80.48856203248494,103.55664056758202,92.3091829150373,123.27796260700957,97.2529066260935,110.36369311643149,84.99816155046332,97.87908100788103,75.53207272339498,92.1090256322082,78.09126315339792,98.11980069427835,79.9260762785673,95.27087465581414,75.64339912130325,93.41051297755547,79.35090675442197,100.5778583399821,83.55758649426299,101.84134444518143]"
Expand All @@ -16,7 +18,8 @@
"morpho,24",0.6984662118260001,"[67.26849677257343,79.24321185258943,84.29061134635378,58.83044583096272,89.66572506678874,86.60888828264055,415.8388734310249,599.8812594909314,351.94128455297187,24.34139102007844,366.79991579172713,583.7215097268089,142.5375766459714,332.92774484926065,298.8202195311825,356.7309670527933,287.1173309233849,404.2807210392618,299.98188714461025,50.225572629250514,285.53191584921916,44.076125606500476,293.1054295993933,30.933276872030664]"
"morpho,36",0.567957669878,"[100.96536878725058,232.16468960961782,78.85523207361015,214.51223438763753,75.89441727306279,218.40581990210538,76.70459049976964,235.00081098229646,77.78059518483653,264.00128466299293,245.07267831506675,428.4287303804663,266.7856810799451,175.86829483871026,165.63461525208467,465.17445657405034,366.1270507007997,237.522658110795,263.7132091597654,167.32906314397042,357.8902823684377,410.9159102460021,372.02631916119753,258.35300162196523,451.044201067961,441.53945835010575,449.1650939325491,179.39104718198786,88.8271411737769,118.64558898301105,160.53307148993068,89.39389813292529,178.5255698838547,119.09614445773937,96.00413532059109,112.94976196610708]"
"morpho,4",1.127904740499,"[154.7298583148998,293.18574120560555,317.42439496690474,132.9926436596167]"
"morpho,40",1.3287687158769999,"[150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,300.0,300.0,300.0,300.0,300.0,300.0,300.0,300.0,300.0,300.0,315.0,315.0,315.0,315.0,315.0,315.0,315.0,315.0,315.0,315.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0]"
"morpho,40",1.328768715877,"[150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,300.0,300.0,300.0,300.0,300.0,300.0,300.0,300.0,300.0,300.0,315.0,315.0,315.0,315.0,315.0,315.0,315.0,315.0,315.0,315.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0,150.0]"
"morpho,48",0.534372807012,"[59.33041960751288,99.17042673266693,79.03990949984373,100.09608817250242,215.08536494524782,213.48080383766006,223.38605676295748,62.1877773107194,78.59192259916864,206.57254442847284,100.49504172262408,238.26268084590538,248.08033497215342,229.886747557948,433.3828290545404,251.2175606263238,285.9568063421582,547.58879753913,411.9624924656734,293.409498298531,208.29322551871047,263.62956155288805,191.701242059666,265.6697290883516,227.32598408391584,230.84889396426746,460.1048548025645,315.76739512321615,278.6271526313933,226.4690710457063,268.86615382660784,270.84424195020307,369.5653419525832,303.82934198377154,344.9339446335882,216.1983454359172,150.53249477057577,299.9978076550384,287.7024584829054,133.05659796829676,0.0,134.8996745953048,127.83643493293731,133.66493131686872,136.72142244431078,148.07302352778007,106.8677064965502,163.45841077158826]"
"morpho,60",0.506129799275,"[38.67453681483682,88.28871817633663,84.73389094259322,228.7841915983999,78.4693541506427,74.51130065973761,238.62081383220206,84.92830393468446,205.48866573642988,225.78976695019594,88.95573577667194,84.17012501437259,230.54112705036326,76.22728816230067,228.8534308053093,325.2301133545765,199.91046929538717,522.5115049313821,509.1418693460837,459.06249748581547,277.022585281524,534.2806553873863,476.5669980315472,227.79666898503064,217.05741507842302,444.1141338132043,292.71525381625327,242.140266341181,123.88086466622762,243.09049745401435,329.36626303083824,301.6930056482294,281.1311060006105,326.1860125896211,453.55453061726837,538.4674329857884,240.32482835825087,379.4108259357168,375.6720323365376,274.1700953102454,402.37446179436074,470.5713737390224,444.6953636032597,448.7323854573844,523.2623296942974,221.40222977791905,280.99419285782517,110.24976061061855,122.0535877024353,88.821255667321,170.8305211691875,107.30104877051781,113.21800840812338,130.33200730579932,131.72665624297295,299.9995243960067,130.57982009633457,106.1567451891198,103.3634746091467,120.3740347248083]"
"morpho,8",1.171114450093,"[142.5961411541705,148.53656440956593,307.6337800249742,294.38885149411345,301.41997857411707,300.96690932557925,151.1925400501033,140.24379338988612]"
"morpho_as_tuple,16",1.296884857369,"[150.0,300.0,315.0,150.0,150.0,300.0,315.0,150.0,150.0,300.0,315.0,150.0,150.0,300.0,315.0,150.0]"
39 changes: 23 additions & 16 deletions nevergrad/functions/photonics/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def _make_parametrization(
if name == "bragg":
shape = (2, dimension // 2)
bounds = [(2, 3), (30, 180)]
elif name == "cf_photosic_realistic":
shape = (2, dimension // 2)
bounds = [(1, 9), (30, 180)]
elif name == "cf_photosic_reference":
shape = (1, dimension)
bounds = [(30, 180)]
elif name == "chirped":
shape = (1, dimension)
bounds = [(30, 180)]
Expand Down Expand Up @@ -147,14 +153,16 @@ def __init__(
rolling: bool = False,
as_tuple: bool = False,
) -> None:
assert name in ["bragg", "morpho", "chirped"]
assert name in [
"bragg",
"morpho",
"chirped",
"cf_photosic_reference",
"cf_photosic_realistic",
], f"Unknown {name}"
self.name = name + ("_as_tuple" if as_tuple else "")
self._as_tuple = as_tuple
self._base_func = {
"morpho": photonics.morpho,
"bragg": photonics.bragg,
"chirped": photonics.chirped,
}[name]
self._base_func: tp.Callable[[np.ndarray], float] = getattr(photonics, name)
param = _make_parametrization(
name=name,
dimension=dimension,
Expand All @@ -164,26 +172,25 @@ def __init__(
)
super().__init__(self._compute, param)

def to_ndarray(self, *args: tp.Any) -> np.ndarray:
return np.concatenate(args).T if self._as_tuple else args[0] # type: ignore
def to_array(self, *args: tp.Any, **kwargs: tp.Any) -> np.ndarray:
assert not kwargs
data = np.concatenate(args).T if self._as_tuple else args[0]
assert data.size == self.dimension
return np.array(data, copy=False).ravel()

def evaluation_function(self, *recommendations: p.Parameter) -> float:
assert len(recommendations) == 1, "Should not be a pareto set for a monoobjective function"
assert len(recommendations[0].kwargs.items()) == 0
x = self.to_ndarray(*recommendations[0].args)
# pylint: disable=not-callable
recom = recommendations[0]
x = self.to_array(*recom.args, **recom.kwargs)
loss = self.function(x)
assert isinstance(loss, float)
base.update_leaderboard(f"{self.name},{self.parametrization.dimension}", loss, x, verbose=True)
return loss

def _compute(self, *args: tp.Any, **kwargs: tp.Any) -> float:
assert len(kwargs.items()) == 0
x = self.to_ndarray(*args)
x_cat = np.array(x, copy=False).ravel()
assert x_cat.size == self.dimension
x = self.to_array(*args, **kwargs)
try:
output = self._base_func(x_cat)
output = self._base_func(x)
except Exception: # pylint: disable=broad-except
output = float("inf")
if np.isnan(output):
Expand Down
58 changes: 30 additions & 28 deletions nevergrad/functions/photonics/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,48 @@ def test_photonics_values(name: str, value: float, expected: float) -> None:
]


EPS_AND_D = [ # photosic realistic
2.0000,
3.0000,
2.1076,
2.0000,
3.0000,
2.5783,
2.0000,
3.0000,
2.0000,
3.0000,
90.0231,
78.9789,
72.8369,
99.9577,
82.7487,
62.7583,
104.1682,
139.9002,
93.3356,
75.6039,
]


@testing.parametrized(
morpho=("morpho", 1.127904, None),
chirped=("chirped", 0.594587, None),
good_chirped=("chirped", 0.275923, GOOD_CHIRPED), # supposed to be better
good_chirped=("chirped", 0.275923, np.array([GOOD_CHIRPED])), # supposed to be better
bragg=("bragg", 0.96776, None),
photosic_realistic=("cf_photosic_realistic", 0.0860257, np.array(EPS_AND_D).reshape((2, -1))),
photosic_reference=("cf_photosic_reference", 0.431072, None),
)
def test_photonics_values_random(name: str, expected: float, data: tp.Optional[tp.List[float]]) -> None:
if name == "morpho" and os.environ.get("CIRCLECI", False):
raise SkipTest("Too slow in CircleCI")
size = len(data) if data is not None else (16 if name != "morpho" else 4)
size = data.size if data is not None else (16 if name != "morpho" else 4)
photo = core.Photonics(name, size)
if data is None:
x = np.random.RandomState(12).normal(0, 1, size=size)
candidate = photo.parametrization.spawn_child().set_standardized_data(x)
else:
candidate = photo.parametrization.spawn_child(new_value=[data])
candidate = photo.parametrization.spawn_child(new_value=data)
np.testing.assert_almost_equal(photo(candidate.value), expected, decimal=4)
np.testing.assert_almost_equal(photo.evaluation_function(candidate), expected, decimal=4)

Expand All @@ -171,29 +197,5 @@ def test_photosic_reference() -> None:


def test_photosic_realist() -> None:
eps_and_d = np.array(
[
2.0000,
3.0000,
2.1076,
2.0000,
3.0000,
2.5783,
2.0000,
3.0000,
2.0000,
3.0000,
90.0231,
78.9789,
72.8369,
99.9577,
82.7487,
62.7583,
104.1682,
139.9002,
93.3356,
75.6039,
]
)
cf_test = photonics.cf_photosic_realistic(eps_and_d)
cf_test = photonics.cf_photosic_realistic(np.array(EPS_AND_D))
np.testing.assert_almost_equal(cf_test, 0.08602574254532869)

0 comments on commit 33b0386

Please sign in to comment.