Skip to content

Commit 550bd2d

Browse files
authored
Allow ::string and ::number in list of nonlinear nodes (#35)
1 parent 7cc33ef commit 550bd2d

File tree

5 files changed

+1354
-203
lines changed

5 files changed

+1354
-203
lines changed

README.md

Lines changed: 28 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,11 @@ Leaf nodes in the expression graph are data: they can either reference
290290
optimization variables, or be real or complex valued numeric constants. They are
291291
described as follows.
292292

293-
| Head | Description | Example |
293+
| Type | Description | Example |
294294
| ---- | ----------- | ------- |
295-
| `"real"` | A real-valued numeric constant | {"type": "real", "value": 1.0} |
296-
| `"complex"` | A complex-valued numeric constant | {"type": "complex", "real": 1.0, "imag": 2.0} |
297-
| `"variable"` | A reference to an optimization variable | {"type": "variable", "name": "x"} |
295+
| `number` | A real-valued numeric constant | 1.0 |
296+
| `string` | A reference to an optimization variable | "x" |
297+
| `{"type": "complex"}` | A complex-valued numeric constant | {"type": "complex", "real": 1.0, "imag": 2.0} |
298298

299299
Nodes in the flattened list `"node_list"` can be referenced by an object with
300300
the `"type"` field `"node"` and a field `"index"` that is the one-based index of
@@ -306,148 +306,41 @@ the node in `"node_list"`.
306306

307307
#### Operators
308308

309-
All nonlinear operators in MathOptFormat are described by a JSON object with two fields:
310-
311-
- `"type"`
312-
313-
A string that corresponds to the operator.
309+
All nonlinear operators in MathOptFormat are described by a JSON object with two
310+
fields:
314311

315-
- `"args"`
312+
- `"type"`: A string that corresponds to the operator.
313+
- `"args"`: An ordered list of nodes that are passed as arguments to the
314+
operator.
316315

317-
An ordered list of nodes that are passed as arguments to the operator.
318-
319-
The number of elements in `"args"` depends on the arity of the operator. MathOptFormat distinguishes between three arities:
316+
The number of elements in `"args"` depends on the arity of the operator.
317+
MathOptFormat distinguishes between three arities:
320318

321319
- Unary operators take one argument
322320
- Binary operators take two arguments
323321
- N-ary operators take at least one argument
324322

325323
To give some examples, the unary function `log(x)` is encoded as:
326324
```json
327-
{
328-
"type": "log",
329-
"args": [
330-
{"type": "variable", "name": "x"}
331-
]
332-
}
325+
{"type": "log", "args": ["x"]}
333326
```
334327
The binary function `x^2` (i.e., `^(x, 2)`) is encoded as:
335328
```json
336-
{
337-
"type": "^",
338-
"args": [
339-
{"type": "variable", "name": "x"},
340-
{"type": "real", "value": 2}
341-
]
342-
}
329+
{"type": "^", "args": ["x", 2]}
343330
```
344331
The n-ary function `x + y + 1` (i.e., `+(x, y, 1)`) is encoded as:
345332
```json
346-
{
347-
"type": "+",
348-
"args": [
349-
{"type": "variable", "name": "x"},
350-
{"type": "variable", "name": "y"},
351-
{"type": "real", "value": 1}
352-
]
353-
}
333+
{"type": "+", "args": ["x", "y", 1.0]}
354334
```
355335

356336
Here is a complete list of the nonlinear operators supported by MathOptFormat
357337
and their corresponding arity.
358338

359-
| Name | Arity |
360-
| ---- | ----- |
361-
| `"+"` | Unary |
362-
| `"-"` | Unary |
363-
| `"abs"` | Unary |
364-
| `"sqrt"` | Unary |
365-
| `"cbrt"` | Unary |
366-
| `"abs2"` | Unary |
367-
| `"inv"` | Unary |
368-
| `"log"` | Unary |
369-
| `"log10"` | Unary |
370-
| `"log2"` | Unary |
371-
| `"log1p"` | Unary |
372-
| `"exp"` | Unary |
373-
| `"exp2"` | Unary |
374-
| `"expm1"` | Unary |
375-
| `"sin"` | Unary |
376-
| `"cos"` | Unary |
377-
| `"tan"` | Unary |
378-
| `"sec"` | Unary |
379-
| `"csc"` | Unary |
380-
| `"cot"` | Unary |
381-
| `"sind"` | Unary |
382-
| `"cosd"` | Unary |
383-
| `"tand"` | Unary |
384-
| `"secd"` | Unary |
385-
| `"cscd"` | Unary |
386-
| `"cotd"` | Unary |
387-
| `"asin"` | Unary |
388-
| `"acos"` | Unary |
389-
| `"atan"` | Unary |
390-
| `"asec"` | Unary |
391-
| `"acsc"` | Unary |
392-
| `"acot"` | Unary |
393-
| `"asind"` | Unary |
394-
| `"acosd"` | Unary |
395-
| `"atand"` | Unary |
396-
| `"asecd"` | Unary |
397-
| `"acscd"` | Unary |
398-
| `"acotd"` | Unary |
399-
| `"sinh"` | Unary |
400-
| `"cosh"` | Unary |
401-
| `"tanh"` | Unary |
402-
| `"sech"` | Unary |
403-
| `"csch"` | Unary |
404-
| `"coth"` | Unary |
405-
| `"asinh"` | Unary |
406-
| `"acosh"` | Unary |
407-
| `"atanh"` | Unary |
408-
| `"asech"` | Unary |
409-
| `"acsch"` | Unary |
410-
| `"acoth"` | Unary |
411-
| `"deg2rad"` | Unary |
412-
| `"rad2deg"` | Unary |
413-
| `"erf"` | Unary |
414-
| `"erfinv"` | Unary |
415-
| `"erfc"` | Unary |
416-
| `"erfcinv"` | Unary |
417-
| `"erfi"` | Unary |
418-
| `"gamma"` | Unary |
419-
| `"lgamma"` | Unary |
420-
| `"digamma"` | Unary |
421-
| `"invdigamma"` | Unary |
422-
| `"trigamma"` | Unary |
423-
| `"airyai"` | Unary |
424-
| `"airybi"` | Unary |
425-
| `"airyaiprime"` | Unary |
426-
| `"airybiprime"` | Unary |
427-
| `"besselj0"` | Unary |
428-
| `"besselj1"` | Unary |
429-
| `"bessely0"` | Unary |
430-
| `"bessely1"` | Unary |
431-
| `"erfcx"` | Unary |
432-
| `"dawson"` | Unary |
433-
| `"floor"` | Unary |
434-
| `"ceil"` | Unary |
435-
| `"/"` | Binary |
436-
| `"^"` | Binary |
437-
| `"atan"` | Binary |
438-
| `"&&"` | Binary |
439-
| `"||"` | Binary |
440-
| `"<="` | Binary |
441-
| `"<"` | Binary |
442-
| `">="` | Binary |
443-
| `">"` | Binary |
444-
| `"=="` | Binary |
445-
| `"+"` | N-ary |
446-
| `"-"` | N-ary |
447-
| `"*"` | N-ary |
448-
| `"ifelse"` | N-ary |
449-
| `"min"` | N-ary |
450-
| `"max"` | N-ary |
339+
| Arity | Operators |
340+
| ----- | --------- |
341+
| Unary | `"abs"`, `"sqrt"`, `"cbrt"`, `"abs2"`, `"inv"`, `"log"`, `"log10"`, `"log2"`, `"log1p"`, `"exp"`, `"exp2"`, `"expm1"`, `"sin"`, `"cos"`, `"tan"`, `"sec"`, `"csc"`, `"cot"`, `"sind"`, `"cosd"`, `"tand"`, `"secd"`, `"cscd"`, `"cotd"`, `"asin"`, `"acos"`, `"atan"`, `"asec"`, `"acsc"`, `"acot"`, `"asind"`, `"acosd"`, `"atand"`, `"asecd"`, `"acscd"`, `"acotd"`, `"sinh"`, `"cosh"`, `"tanh"`, `"sech"`, `"csch"`, `"coth"`, `"asinh"`, `"acosh"`, `"atanh"`, `"asech"`, `"acsch"`, `"acoth"`, `"deg2rad"`, `"rad2deg"`, `"erf"`, `"erfinv"`, `"erfc"`, `"erfcinv"`, `"erfi"`, `"gamma"`, `"lgamma"`, `"digamma"`, `"invdigamma"`, `"trigamma"`, `"airyai"`, `"airybi"`, `"airyaiprime"`, `"airybiprime"`, `"besselj0"`, `"besselj1"`, `"bessely0"`, `"bessely1"`, `"erfcx"`, `"dawson"`, `"floor"`, `"ceil"` |
342+
| Binary | `"/"`, `"^"`, `"atan"`, `"&&"`, `"\|\|"`, `"<="`, `"<"`, `">="`, `">"`, `"=="` |
343+
| N-ary | `"+"`, `"-"`, `"*"`, `"ifelse"`, `"min"`, `"max"` |
451344

452345
#### Example
453346

@@ -462,31 +355,15 @@ In MathOptFormat, this expression graph can be encoded as follows:
462355
"type": "ScalarNonlinearFunction",
463356
"root": {
464357
"type": "+",
465-
"args": [
466-
{"type": "node", "index": 1},
467-
{"type": "node", "index": 3},
468-
{"type": "variable", "name": "y"}
469-
]
358+
"args": [{"type": "node", "index": 1}, {"type": "node", "index": 3}, "y"]
470359
},
471-
"node_list": [
472-
{
473-
"type": "*",
474-
"args": [
475-
{"type": "complex", "real": 1, "imag": 3},
476-
{"type": "variable", "name": "x"}
477-
]
478-
}, {
479-
"type": "sin",
480-
"args": [
481-
{"type": "variable", "name": "x"}
482-
]
483-
}, {
484-
"type": "^",
485-
"args": [
486-
{"type": "node", "index": 2},
487-
{"type": "real", "value": 2}
488-
]
489-
}
490-
]
360+
"node_list": [{
361+
"type": "*",
362+
"args": [{"type": "complex", "real": 1, "imag": 3}, "x"]
363+
}, {
364+
"type": "sin", "args": ["x"]
365+
}, {
366+
"type": "^", "args": [{"type": "node", "index": 2}, 2]
367+
}]
491368
}
492369
```

examples/nlp.mof.json

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,31 @@
11
{
22
"description": "The problem: min{2x + sin(x)^2 + y}.",
3-
"version": {
4-
"major": 1,
5-
"minor": 1
6-
},
7-
"variables": [{
8-
"name": "x"
9-
}, {
10-
"name": "y"
11-
}],
3+
"version": {"major": 1, "minor": 7},
4+
"variables": [{"name": "x"}, {"name": "y"}],
125
"objective": {
136
"sense": "min",
147
"function": {
158
"type": "ScalarNonlinearFunction",
16-
"root": {
17-
"type": "node",
18-
"index": 4
19-
},
9+
"root": {"type": "node", "index": 4},
2010
"node_list": [{
2111
"type": "*",
22-
"args": [{
23-
"type": "real",
24-
"value": 2
25-
}, {
26-
"type": "variable",
27-
"name": "x"
28-
}]
12+
"args": [
13+
{"type": "real", "value": 2},
14+
{"type": "variable", "name": "x"}
15+
]
2916
}, {
3017
"type": "sin",
31-
"args": [{
32-
"type": "variable",
33-
"name": "x"
34-
}]
18+
"args": ["x"]
3519
}, {
3620
"type": "^",
37-
"args": [{
38-
"type": "node",
39-
"index": 2
40-
}, {
41-
"type": "real",
42-
"value": 2
43-
}]
21+
"args": [{"type": "node", "index": 2}, 2.0]
4422
}, {
4523
"type": "+",
46-
"args": [{
47-
"type": "node",
48-
"index": 1
49-
}, {
50-
"type": "node",
51-
"index": 3
52-
}, {
53-
"type": "variable",
54-
"name": "y"
55-
}]
24+
"args": [
25+
{"type": "node", "index": 1},
26+
{"type": "node", "index": 3},
27+
"y"
28+
]
5629
}]
5730
}
5831
},

python/mof.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ def summarize_schema():
2828
return summary
2929

3030
def oneOf_to_object(item):
31+
if "properties" not in item:
32+
print(item)
33+
return [{
34+
"name": None,
35+
"description": item["description"],
36+
"example": item["examples"][0],
37+
}]
3138
head = item["properties"]["type"]
3239
ret = []
3340
if "const" in head:

0 commit comments

Comments
 (0)