{
  "$defs": {
    "Beam": {
      "additionalProperties": false,
      "description": "所ごとのビーム探索の設定（[`SwapSpec::beam`]）",
      "properties": {
        "passes": {
          "default": 2,
          "description": "所を一巡する回数の上限（残す選び方が巡回の前後で変わらなければその前に止める）",
          "format": "uint",
          "minimum": 0,
          "type": "integer"
        },
        "width": {
          "default": 20,
          "description": "所ごとに残す選び方の数",
          "format": "uint",
          "minimum": 0,
          "type": "integer"
        }
      },
      "type": "object"
    },
    "Filter": {
      "additionalProperties": false,
      "description": "候補の絞り込み（省いた条件は絞らない）",
      "properties": {
        "diameter": {
          "default": [
            null,
            null
          ],
          "description": "径の範囲 [mm]",
          "items": {
            "format": "double",
            "type": [
              "number",
              "null"
            ]
          },
          "maxItems": 2,
          "minItems": 2,
          "type": "array"
        },
        "efl": {
          "default": [
            null,
            null
          ],
          "description": "焦点距離の範囲 [mm]",
          "items": {
            "format": "double",
            "type": [
              "number",
              "null"
            ]
          },
          "maxItems": 2,
          "minItems": 2,
          "type": "array"
        },
        "vendor": {
          "default": null,
          "description": "メーカー（大文字・小文字・空白・記号を無視して、カタログのメーカー名に含まれれば合う）",
          "type": [
            "string",
            "null"
          ]
        }
      },
      "type": "object"
    },
    "Paraxial": {
      "additionalProperties": false,
      "description": "近軸の前ふるいの設定（[`SwapSpec::paraxial`]）",
      "properties": {
        "efl_tol": {
          "description": "焦点距離の目標からの許し [mm]（省けば目標の 1 %）",
          "format": "double",
          "type": [
            "number",
            "null"
          ]
        }
      },
      "type": "object"
    },
    "PartRef": {
      "additionalProperties": false,
      "description": "型番 1 つ",
      "properties": {
        "part": {
          "type": "string"
        },
        "vendor": {
          "type": "string"
        }
      },
      "required": [
        "vendor",
        "part"
      ],
      "type": "object"
    },
    "Prescreen": {
      "additionalProperties": false,
      "description": "粗いふるいの設定（[`SwapSpec::prescreen`]）",
      "properties": {
        "keep": {
          "default": 1000,
          "description": "二段目に回す組の数（一段目の評価関数の小さい順。評価できなかった組は回さない）",
          "format": "uint",
          "minimum": 0,
          "type": "integer"
        },
        "rings": {
          "default": 2,
          "description": "一段目の評価関数のリング数（`rings` より小さく。実カタログ 5.6 万組で、リング 3 に対して 2 なら上位 1000 組が\n同じ顔ぶれ、1 なら上位 10 組の最悪が 438 位）",
          "format": "uint",
          "minimum": 0,
          "type": "integer"
        }
      },
      "type": "object"
    },
    "Slot": {
      "additionalProperties": false,
      "description": "差し替える所",
      "properties": {
        "block": {
          "description": "玉のブロック（type lens）の id",
          "type": "string"
        },
        "filter": {
          "$ref": "#/$defs/Filter",
          "default": {
            "diameter": [
              null,
              null
            ],
            "efl": [
              null,
              null
            ],
            "vendor": null
          }
        },
        "flip": {
          "default": false,
          "description": "向き（reversed）を両方試す（false ならブロックの向きのまま）",
          "type": "boolean"
        },
        "parts": {
          "default": [],
          "description": "候補の型番（空なら filter で絞ったカタログのすべて）",
          "items": {
            "$ref": "#/$defs/PartRef"
          },
          "type": "array"
        }
      },
      "required": [
        "block"
      ],
      "type": "object"
    },
    "SwapJac": {
      "description": "ヤコビアンの求め方（[`SwapSpec::jac`]、`optimize --jac` と同じ名前）",
      "oneOf": [
        {
          "const": "forward",
          "description": "すべて前進差分",
          "type": "string"
        },
        {
          "const": "fd",
          "description": "すべて中心差分",
          "type": "string"
        },
        {
          "const": "auto",
          "description": "解析的に求められる列が多い項は解析的に",
          "type": "string"
        }
      ]
    },
    "SwapMerit": {
      "description": "評価関数（[`SwapSpec::merit`]）",
      "oneOf": [
        {
          "const": "spot",
          "description": "スポットの RMS",
          "type": "string"
        },
        {
          "const": "wavefront",
          "description": "波面の RMS",
          "type": "string"
        }
      ]
    },
    "SwapRef": {
      "description": "RMS の基準（[`SwapSpec::reference`]）",
      "oneOf": [
        {
          "const": "centroid",
          "description": "重心",
          "type": "string"
        },
        {
          "const": "chief",
          "description": "主光線",
          "type": "string"
        }
      ]
    },
    "VarRange": {
      "additionalProperties": false,
      "description": "変数のオブジェクトの形（[`VarSpec::Full`]）",
      "properties": {
        "max": {
          "description": "上限（省けば無し）",
          "format": "double",
          "type": [
            "number",
            "null"
          ]
        },
        "min": {
          "description": "下限（省けば無し）",
          "format": "double",
          "type": [
            "number",
            "null"
          ]
        },
        "name": {
          "type": "string"
        }
      },
      "required": [
        "name"
      ],
      "type": "object"
    },
    "VarSpec": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "$ref": "#/$defs/VarRange"
        }
      ],
      "description": "差し替え探索の変数: 文字の短い形 \"d1.t\"・\"d1.t=下限:上限\"（片側は空でよい）か、オブジェクトの形\n`{\"name\": \"d1.t\", \"min\": 0.5, \"max\": 40}`（min・max は省ける）"
    }
  },
  "$id": "https://masa-iwm.github.io/lensbench/schema/v0.11/swap.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "additionalProperties": false,
  "description": "差し替え探索の設定（CLI の --spec の JSON）",
  "properties": {
    "$schema": {
      "description": "このファイルのスキーマ（エディタの補完・検査用。読むときは無視する）",
      "type": "string"
    },
    "beam": {
      "anyOf": [
        {
          "$ref": "#/$defs/Beam"
        },
        {
          "type": "null"
        }
      ],
      "description": "所ごとのビーム探索（省けば全組の総当たり）: 全組でなく、所を 1 つずつ候補を全部試して評価関数の小さい\n`width` 通りを残しながら進む。評価する組の数は全組の積でなく、およそ `width` × 候補の数の和 × 巡回の数。\n総当たりの最良を見落とすことがある。`prescreen` とは同時に使えない"
    },
    "clear_aperture": {
      "default": false,
      "description": "有効径の条件: 差し替える玉の各面で、各視野の瞳の縁の実光線（主波長、瞳の上下左右）が面の半径\n（カタログの玉の半直径）の内側にあることを片側の条件の項として足す（重みは像の項の重みの合計の 1e6 倍）",
      "type": "boolean"
    },
    "efl": {
      "default": null,
      "description": "系の焦点距離の目標（省けば目標なし）。重みは像の項の重みの合計の 10 倍（`TermKind::Paraxial`）",
      "format": "double",
      "type": [
        "number",
        "null"
      ]
    },
    "iters": {
      "default": 30,
      "description": "組ごとの反復の上限",
      "format": "uint",
      "minimum": 0,
      "type": "integer"
    },
    "jac": {
      "$ref": "#/$defs/SwapJac",
      "default": "forward",
      "description": "ヤコビアン: \"forward\"（すべて前進差分、既定）・\"fd\"（すべて中心差分）・\"auto\"（解析的に求められる列が 16 個以上の項は解析的に、少ない項は中心差分）。\n差し替え探索の変数（間隔・群の移動など少数）では差分の方が速い（実カタログ 7 万組で fd は auto の 4.3 倍）。\nforward は差分の追跡が中心差分の半分で済み、fd よりさらに約 1.4 倍速い（実カタログ 3,000 組で上位 300 組の顔ぶれ・順位は\nfd と同じ、評価関数の差は相対 3e-7 以下）"
    },
    "max_count": {
      "default": 100000,
      "description": "組の数の上限（大きすぎる探索を誤って始めないため。ビーム探索では評価する組の数の見込み）",
      "format": "uint",
      "minimum": 0,
      "type": "integer"
    },
    "merit": {
      "$ref": "#/$defs/SwapMerit",
      "default": "spot",
      "description": "評価関数"
    },
    "paraxial": {
      "anyOf": [
        {
          "$ref": "#/$defs/Paraxial"
        },
        {
          "type": "null"
        }
      ],
      "description": "近軸の前ふるい（省けばなし、`efl` があるときだけ）: 変数（範囲つきの厚さだけ）の範囲の角で系の屈折力を求め、\n`efl` の目標に届かない組を光線を追わずに落とす（その組は誤り「…misses the target…」の結果になる）。\n屈折力は厚さのそれぞれについて 1 次なので、範囲の中の最大・最小は角にあり、落とした組はどの値でも目標に届かない。\nピックアップ・ソルブ・マクロのある状態や屈折率分布の媒質の厚さを動かす状態は 1 次とは限らないので確かめない"
    },
    "prescreen": {
      "anyOf": [
        {
          "$ref": "#/$defs/Prescreen"
        },
        {
          "type": "null"
        }
      ],
      "description": "粗いふるい（省けばなし）: 全組を光線の少ない評価関数で最適化し、評価関数の小さい組だけを今の評価で\n最適化し直す。残した組の結果はふるいなしと同じ。残さなかった組は結果に出ない"
    },
    "reference": {
      "$ref": "#/$defs/SwapRef",
      "default": "centroid",
      "description": "基準"
    },
    "rings": {
      "default": 3,
      "format": "uint",
      "minimum": 0,
      "type": "integer"
    },
    "slots": {
      "items": {
        "$ref": "#/$defs/Slot"
      },
      "type": "array"
    },
    "starts": {
      "default": [],
      "description": "出発点（変数の名前 → 初期値。書かない変数はプロジェクトの値）。組ごとにすべての出発点から最適化し、\n評価関数の最も小さい解を採る（空なら プロジェクトの値から 1 回）",
      "items": {
        "additionalProperties": {
          "format": "double",
          "type": "number"
        },
        "type": "object"
      },
      "type": "array"
    },
    "state": {
      "default": null,
      "description": "状態（None なら変更なし）",
      "type": [
        "string",
        "null"
      ]
    },
    "state_vars": {
      "default": [],
      "description": "状態ごとに別の値で解く変数（`states` のとき。群の移動 `G2.shift` など、書き方は `vars` と同じ）。\n`vars` は全状態で共通の 1 つの値",
      "items": {
        "$ref": "#/$defs/VarSpec"
      },
      "type": "array"
    },
    "states": {
      "default": [],
      "description": "状態をまたいだ探索（ズーム位置など）: プロジェクトの状態の名前の並び。組ごとに全状態を同時に\n最適化する（評価関数は全状態の像の項）。`state` とは同時に使えない。`terms` の `config` は\nこの並びの番号（1 始まり）",
      "items": {
        "type": "string"
      },
      "type": "array"
    },
    "terms": {
      "default": [],
      "description": "評価関数に足す項（`optimize --term` と同じ JSON。片側の条件 `min`・`max` も）。\n例: `{\"type\": \"paraxial\", \"q\": \"efl\", \"min\": 351}`",
      "items": true,
      "type": "array"
    },
    "vars": {
      "default": [],
      "description": "組ごとに最適化する変数（\"d1.t\"・\"d1.t=0.5:40\"・\"G2.shift\"、または `{\"name\": \"d1.t\", \"min\": 0.5, \"max\": 40}`）",
      "items": {
        "$ref": "#/$defs/VarSpec"
      },
      "type": "array"
    }
  },
  "required": [
    "slots"
  ],
  "title": "lensbench 差し替え探索の設定",
  "type": "object"
}
