tarzanagh commited on
Commit
073f2e4
·
verified ·
1 Parent(s): 5f4f01f

INIT as in Algorithm 3: candidates and an optional budget U, returns the cheapest feasible candidate or none; gridstart.start: two runs of INIT, the start is the returned grid point or z0

Browse files
globmp/gridstart/README.md CHANGED
@@ -60,25 +60,25 @@ Results go to `results/gridstart` (or `--out`, or `$GLOBMP_GRIDSTART_OUT`).
60
 
61
  Counts are deterministic; times depend on the machine.
62
 
63
- ## The start in two runs (Algorithm 3 with a feasible curve z0)
64
 
65
- Algorithm 3 takes a feasible curve `z0`, uses its cost as the budget, and returns `z0` when no grid curve is
66
- cheaper (`globmp.init(..., z0=(cost, curve))`). `instance.start` builds the start this way:
67
 
68
- 1. INIT on few candidates: joint positions on a grid (`--h0`, default 0.25) with zero velocity at the joints, so
69
- every piece is a straight move (`RestStates`).
70
- 2. GlobMP with the exact repair from that curve gives `z0`.
71
- 3. INIT on the grid of spacing `h` with `z0`: the cheapest feasible grid point if one costs at most `f(z0)`,
72
- otherwise `z0`.
73
 
74
  ```bash
75
  python -m globmp.gridstart.run start --N 2 3 4 --h 0.5 0.25 --procs 6 # 5 s; reference/start.json
76
  ```
77
 
78
- The returned start is feasible and costs no more than `z0` and no more than any feasible grid point. In the six
79
- runs above step 3 returns `z0` (no grid point is cheaper) after 1 to 3,476 piece tests, and the start costs
80
- `E*_N` to 1e-9. For comparison, the grid point `z_h` alone costs 1.16 to 2.49 times `E*_N` at `h = 0.25`, and
81
- at `h = 0.5` there is no feasible grid point within `4 E*_N` for N = 4.
82
 
83
  ## Limits
84
 
 
60
 
61
  Counts are deterministic; times depend on the machine.
62
 
63
+ ## The start in two runs of INIT
64
 
65
+ Algorithm 3 takes the candidates and an optional budget `U`, and returns the cheapest feasible candidate curve
66
+ with cost at most `U`, or none. `instance.start` computes the start of Theorem 1 with two runs:
67
 
68
+ 1. INIT with no budget on few candidates: joint positions on a grid (`--h0`, default 0.25) with zero velocity at
69
+ the joints, so every piece is a straight move (`RestStates`).
70
+ 2. GlobMP with the exact repair from that curve gives a feasible curve `z0`.
71
+ 3. INIT on the grid of spacing `h` with the budget `U = f(z0)`. The start is the grid point it returns, or `z0`
72
+ if it returns none.
73
 
74
  ```bash
75
  python -m globmp.gridstart.run start --N 2 3 4 --h 0.5 0.25 --procs 6 # 5 s; reference/start.json
76
  ```
77
 
78
+ The start is feasible and costs no more than `z0` and no more than any feasible grid point. In the six runs
79
+ above step 3 returns none (no grid point costs at most `f(z0)`) after 1 to 3,476 piece tests, and the start `z0`
80
+ costs `E*_N` to 1e-9. For comparison, the grid point `z_h` alone costs 1.16 to 2.49 times `E*_N` at `h = 0.25`,
81
+ and at `h = 0.5` there is no feasible grid point within `4 E*_N` for N = 4.
82
 
83
  ## Limits
84
 
globmp/gridstart/instance.py CHANGED
@@ -258,8 +258,8 @@ def start(N, h, h0=0.25, descend=None):
258
  """The start of Theorem 1 as Algorithm 3 computes it, in two runs of INIT (globmp.init):
259
  1. INIT on the candidates RestStates(N, h0); `descend` (GlobMP with the exact repair) from its curve gives a
260
  feasible curve z0 of cost f(z0);
261
- 2. INIT on the grid of spacing h with z0: the cheapest feasible grid point if one costs at most f(z0),
262
- otherwise z0.
263
  descend: x (4N,) -> (x, cost) of a feasible curve no dearer than x (None: z0 is the curve of run 1 itself).
264
  Returns (x, cost, info): x is feasible and costs no more than z0 and no more than any feasible grid point."""
265
  t0 = time.time(); g0 = RestStates(N, h0); r0 = init(g0)
@@ -270,10 +270,11 @@ def start(N, h, h0=0.25, descend=None):
270
  c0, path = r0['curves'][0]; x0 = states_to_x(g0, path); info['run1'].update(cost=c0, x=x0.tolist())
271
  xz, cz = (x0, c0) if descend is None else descend(x0)
272
  info['z0'] = dict(cost=float(cz), x=np.asarray(xz).tolist())
273
- g = GridStates(N, h, cz); r = init(g, z0=(float(cz), np.asarray(xz)))
274
- kind, cost, what = r['start']
275
- x = states_to_x(g, what) if kind == 'init' else np.asarray(what)
276
- info['run2'] = dict(returned='grid point' if kind == 'init' else 'z0', cost=float(cost), time=r['time'],
 
277
  states=[g.n_states(j) for j in range(N + 1)], tests=sum(l['tested'] for l in r['layers']),
278
  box=[(int(a), int(b)) for a, b in zip(g.lo, g.hi)])
279
  info['time'] = time.time() - t0
 
258
  """The start of Theorem 1 as Algorithm 3 computes it, in two runs of INIT (globmp.init):
259
  1. INIT on the candidates RestStates(N, h0); `descend` (GlobMP with the exact repair) from its curve gives a
260
  feasible curve z0 of cost f(z0);
261
+ 2. INIT on the grid of spacing h with the budget U = f(z0): it returns the cheapest feasible grid point
262
+ if one costs at most f(z0), otherwise none; the start is that grid point, or z0.
263
  descend: x (4N,) -> (x, cost) of a feasible curve no dearer than x (None: z0 is the curve of run 1 itself).
264
  Returns (x, cost, info): x is feasible and costs no more than z0 and no more than any feasible grid point."""
265
  t0 = time.time(); g0 = RestStates(N, h0); r0 = init(g0)
 
270
  c0, path = r0['curves'][0]; x0 = states_to_x(g0, path); info['run1'].update(cost=c0, x=x0.tolist())
271
  xz, cz = (x0, c0) if descend is None else descend(x0)
272
  info['z0'] = dict(cost=float(cz), x=np.asarray(xz).tolist())
273
+ g = GridStates(N, h, cz); r = init(g, float(cz))
274
+ if r['curves']: cost, path = r['curves'][0]; x = states_to_x(g, path)
275
+ else: cost, x = float(cz), np.asarray(xz)
276
+ info['run2'] = dict(returned='grid point' if r['curves'] else 'none', start='grid point' if r['curves'] else 'z0',
277
+ cost=float(cost), time=r['time'],
278
  states=[g.n_states(j) for j in range(N + 1)], tests=sum(l['tested'] for l in r['layers']),
279
  box=[(int(a), int(b)) for a, b in zip(g.lo, g.hi)])
280
  info['time'] = time.time() - t0
globmp/gridstart/reference/start.json CHANGED
@@ -5,12 +5,13 @@
5
  "h0": 0.25,
6
  "run1_cost": 687.0,
7
  "run1_tests": 1572,
8
- "z0_cost": 12.330473181107209,
9
- "run2_returned": "z0",
10
  "run2_tests": 3476,
11
- "start_cost": 12.330473181107209,
 
12
  "start_certified": true,
13
- "globmp_final": 12.330473181108118,
14
  "E_star": 12.33047317470573
15
  },
16
  "N2_h0.5": {
@@ -20,8 +21,9 @@
20
  "run1_cost": 687.0,
21
  "run1_tests": 1572,
22
  "z0_cost": 12.330473181107209,
23
- "run2_returned": "z0",
24
  "run2_tests": 57,
 
25
  "start_cost": 12.330473181107209,
26
  "start_certified": true,
27
  "globmp_final": 12.330473181108118,
@@ -33,12 +35,13 @@
33
  "h0": 0.25,
34
  "run1_cost": 441.0,
35
  "run1_tests": 562625,
36
- "z0_cost": 3.554053376594311,
37
- "run2_returned": "z0",
38
  "run2_tests": 290,
39
- "start_cost": 3.554053376594311,
 
40
  "start_certified": true,
41
- "globmp_final": 3.5540533765938562,
42
  "E_star": 3.5540533765934015
43
  },
44
  "N3_h0.5": {
@@ -48,11 +51,12 @@
48
  "run1_cost": 441.0,
49
  "run1_tests": 562625,
50
  "z0_cost": 3.554053376594311,
51
- "run2_returned": "z0",
52
  "run2_tests": 8,
 
53
  "start_cost": 3.554053376594311,
54
  "start_certified": true,
55
- "globmp_final": 3.5540533765934015,
56
  "E_star": 3.5540533765934015
57
  },
58
  "N4_h0.25": {
@@ -61,12 +65,13 @@
61
  "h0": 0.25,
62
  "run1_cost": 333.0,
63
  "run1_tests": 1510750,
64
- "z0_cost": 1.5046976746823475,
65
- "run2_returned": "z0",
66
  "run2_tests": 44,
67
- "start_cost": 1.5046976746823475,
 
68
  "start_certified": true,
69
- "globmp_final": 1.5046976746828022,
70
  "E_star": 1.504697674656427
71
  },
72
  "N4_h0.5": {
@@ -75,12 +80,13 @@
75
  "h0": 0.25,
76
  "run1_cost": 333.0,
77
  "run1_tests": 1510750,
78
- "z0_cost": 1.5046976748556062,
79
- "run2_returned": "z0",
80
  "run2_tests": 1,
81
- "start_cost": 1.5046976748556062,
 
82
  "start_certified": true,
83
- "globmp_final": 1.5046976746209566,
84
  "E_star": 1.504697674656427
85
  }
86
  }
 
5
  "h0": 0.25,
6
  "run1_cost": 687.0,
7
  "run1_tests": 1572,
8
+ "z0_cost": 12.330473175721181,
9
+ "run2_returned": "none",
10
  "run2_tests": 3476,
11
+ "start": "z0",
12
+ "start_cost": 12.330473175721181,
13
  "start_certified": true,
14
+ "globmp_final": 12.330473175327825,
15
  "E_star": 12.33047317470573
16
  },
17
  "N2_h0.5": {
 
21
  "run1_cost": 687.0,
22
  "run1_tests": 1572,
23
  "z0_cost": 12.330473181107209,
24
+ "run2_returned": "none",
25
  "run2_tests": 57,
26
+ "start": "z0",
27
  "start_cost": 12.330473181107209,
28
  "start_certified": true,
29
  "globmp_final": 12.330473181108118,
 
35
  "h0": 0.25,
36
  "run1_cost": 441.0,
37
  "run1_tests": 562625,
38
+ "z0_cost": 3.5540533765947657,
39
+ "run2_returned": "none",
40
  "run2_tests": 290,
41
+ "start": "z0",
42
+ "start_cost": 3.5540533765947657,
43
  "start_certified": true,
44
+ "globmp_final": 3.554053376593629,
45
  "E_star": 3.5540533765934015
46
  },
47
  "N3_h0.5": {
 
51
  "run1_cost": 441.0,
52
  "run1_tests": 562625,
53
  "z0_cost": 3.554053376594311,
54
+ "run2_returned": "none",
55
  "run2_tests": 8,
56
+ "start": "z0",
57
  "start_cost": 3.554053376594311,
58
  "start_certified": true,
59
+ "globmp_final": 3.554053376593629,
60
  "E_star": 3.5540533765934015
61
  },
62
  "N4_h0.25": {
 
65
  "h0": 0.25,
66
  "run1_cost": 333.0,
67
  "run1_tests": 1510750,
68
+ "z0_cost": 1.5046976748556062,
69
+ "run2_returned": "none",
70
  "run2_tests": 44,
71
+ "start": "z0",
72
+ "start_cost": 1.5046976748556062,
73
  "start_certified": true,
74
+ "globmp_final": 1.5046976746684777,
75
  "E_star": 1.504697674656427
76
  },
77
  "N4_h0.5": {
 
80
  "h0": 0.25,
81
  "run1_cost": 333.0,
82
  "run1_tests": 1510750,
83
+ "z0_cost": 1.504697674620047,
84
+ "run2_returned": "none",
85
  "run2_tests": 1,
86
+ "start": "z0",
87
+ "start_cost": 1.504697674620047,
88
  "start_certified": true,
89
+ "globmp_final": 1.5046976746184555,
90
  "E_star": 1.504697674656427
91
  }
92
  }
globmp/gridstart/run.py CHANGED
@@ -7,7 +7,8 @@
7
  (--first-order: also the released Algorithms 1-2 from z_h)
8
  summary table of all jobs (markdown on stdout, summary.json in the output directory)
9
  start the start as Algorithm 3 computes it (instance.start): INIT on joint positions with zero velocity,
10
- GlobMP with the exact repair from its curve (z0), INIT on the grid with z0; then GlobMP from the start
 
11
 
12
  python -m globmp.gridstart.run check
13
  python -m globmp.gridstart.run optimum --procs 4
@@ -192,7 +193,7 @@ def start_job(args):
192
  json.dump(res, open(os.path.join(out, f'start_N{N}_h{h}.json'), 'w'), indent=1, default=float)
193
  r1, r2, E = res.get('run1', {}), res.get('run2', {}), res.get('E_star')
194
  print(f"N={N} h={h}: run 1 cost {r1.get('cost')} ({r1.get('tests')} tests, {r1.get('time', 0):.1f}s) | z0 {res.get('z0', {}).get('cost')} | "
195
- f"run 2 returns {r2.get('returned')} ({r2.get('tests')} tests, {r2.get('time', 0):.2f}s) | start cost {cost} | "
196
  f"GlobMP from the start -> {res.get('exact', {}).get('final_cost')} {res.get('exact', {}).get('side')}"
197
  + (f" | E* {E}" if E else '') + f" | {res['time_total']:.0f}s", flush=True)
198
  return res
 
7
  (--first-order: also the released Algorithms 1-2 from z_h)
8
  summary table of all jobs (markdown on stdout, summary.json in the output directory)
9
  start the start as Algorithm 3 computes it (instance.start): INIT on joint positions with zero velocity,
10
+ GlobMP with the exact repair from its curve (z0), INIT on the grid with the budget f(z0); the start is
11
+ the returned grid point, or z0 if none is returned; then GlobMP from the start
12
 
13
  python -m globmp.gridstart.run check
14
  python -m globmp.gridstart.run optimum --procs 4
 
193
  json.dump(res, open(os.path.join(out, f'start_N{N}_h{h}.json'), 'w'), indent=1, default=float)
194
  r1, r2, E = res.get('run1', {}), res.get('run2', {}), res.get('E_star')
195
  print(f"N={N} h={h}: run 1 cost {r1.get('cost')} ({r1.get('tests')} tests, {r1.get('time', 0):.1f}s) | z0 {res.get('z0', {}).get('cost')} | "
196
+ f"run 2 returns {r2.get('returned')} ({r2.get('tests')} tests, {r2.get('time', 0):.2f}s) | start = {r2.get('start')}, cost {cost} | "
197
  f"GlobMP from the start -> {res.get('exact', {}).get('final_cost')} {res.get('exact', {}).get('side')}"
198
  + (f" | E* {E}" if E else '') + f" | {res['time_total']:.0f}s", flush=True)
199
  return res
globmp/init.py CHANGED
@@ -4,8 +4,8 @@ path over the joints.
4
  A candidate curve is one state per joint. Piece i is determined by the states at joints i and i+1, the cost is a
5
  sum over the pieces, and a curve is feasible exactly when every piece is. INIT keeps, for every state at a joint,
6
  the cheapest feasible way to reach it (V) and a back-pointer. A pair (s, s') is skipped without a piece test when
7
- V(s) + w(s, s') exceeds the budget U, the cost of a known feasible curve z0; when no candidate curve is within the
8
- budget, z0 is the answer. With the cost-to-go rule the test is
9
  V(s) + w(s, s') + togo(s') > U, where togo(s') is a lower bound on the cost of the remaining pieces; it skips more
10
  pairs and returns the same curve.
11
 
@@ -31,20 +31,16 @@ import time
31
  import numpy as np
32
 
33
 
34
- def init(inst, U=np.inf, rule='togo', improve_only=True, ties='first', chunk=2e7, z0=None):
35
  """INIT. rule 'togo' (default): budget with cost-to-go; 'budget': the plain budget rule V(s) + w > U (same
36
  curve, more piece tests). improve_only: also skip pairs that cannot lower the current V(s') (same curve).
37
  Sources are processed cheapest first, `chunk` matrix entries at a time. ties: among ways of equal cost into a
38
  state, keep the first one processed ('first', fewest piece tests) or the one from the lowest (label, state)
39
- index ('state', independent of the processing order). z0 = (cost, curve): a known feasible curve; the budget
40
- is then min(U, cost), and z0 is the start when no candidate curve is within it (Algorithm 3, last line).
41
 
42
  Returns dict(curves, layers, time): curves = {label: (cost, [state index at joint 0..M])}, the cheapest
43
  feasible curve with cost <= U per final label (empty = FAIL); layers = per-piece counts (sources, targets,
44
- pairs, budget_pass, tested, free, live). With z0 also start = ('init', cost, path) for the cheapest returned
45
- curve, or ('z0', cost, curve) when none is returned: a feasible curve that costs no more than z0 and no more
46
- than any feasible candidate."""
47
- if z0 is not None: U = min(U, float(z0[0]))
48
  M = inst.n_pieces; L = getattr(inst, 'n_labels', 1); label = getattr(inst, 'label', None); t0 = time.time()
49
  pairs = hasattr(inst, 'free_pairs'); first = ties == 'first'
50
  idx = np.arange(inst.n_states(0)); lab = np.zeros(len(idx), np.int64); V = np.zeros(len(idx))
@@ -91,7 +87,7 @@ def init(inst, U=np.inf, rule='togo', improve_only=True, ties='first', chunk=2e7
91
  Vn.ravel()[k[better]] = val[better]; pred.ravel()[k[better]] = si[a[better]]
92
  ll, tt = np.nonzero(np.isfinite(Vn))
93
  st.update(live=len(tt), time=time.time() - tl); layers.append(st)
94
- if len(tt) == 0: return _out({}, layers, t0, z0)
95
  idx, lab, V = tt, ll, Vn[ll, tt]; back.append((idx, lab, pred[ll, tt]))
96
  curves = {}
97
  for l in np.unique(lab):
@@ -100,11 +96,4 @@ def init(inst, U=np.inf, rule='togo', improve_only=True, ties='first', chunk=2e7
100
  path[lev] = int(back[lev][0][j])
101
  if lev > 0: j = int(back[lev][2][j])
102
  curves[int(l)] = (cost, path)
103
- return _out(curves, layers, t0, z0)
104
-
105
-
106
- def _out(curves, layers, t0, z0):
107
- r = dict(curves=curves, layers=layers, time=time.time() - t0)
108
- if z0 is not None:
109
- r['start'] = ('init',) + min(curves.values(), key=lambda c: c[0]) if curves else ('z0', float(z0[0]), z0[1])
110
- return r
 
4
  A candidate curve is one state per joint. Piece i is determined by the states at joints i and i+1, the cost is a
5
  sum over the pieces, and a curve is feasible exactly when every piece is. INIT keeps, for every state at a joint,
6
  the cheapest feasible way to reach it (V) and a back-pointer. A pair (s, s') is skipped without a piece test when
7
+ V(s) + w(s, s') exceeds the budget U (the cost of a known feasible curve; +inf if none is known). With the
8
+ cost-to-go rule the test is
9
  V(s) + w(s, s') + togo(s') > U, where togo(s') is a lower bound on the cost of the remaining pieces; it skips more
10
  pairs and returns the same curve.
11
 
 
31
  import numpy as np
32
 
33
 
34
+ def init(inst, U=np.inf, rule='togo', improve_only=True, ties='first', chunk=2e7):
35
  """INIT. rule 'togo' (default): budget with cost-to-go; 'budget': the plain budget rule V(s) + w > U (same
36
  curve, more piece tests). improve_only: also skip pairs that cannot lower the current V(s') (same curve).
37
  Sources are processed cheapest first, `chunk` matrix entries at a time. ties: among ways of equal cost into a
38
  state, keep the first one processed ('first', fewest piece tests) or the one from the lowest (label, state)
39
+ index ('state', independent of the processing order).
 
40
 
41
  Returns dict(curves, layers, time): curves = {label: (cost, [state index at joint 0..M])}, the cheapest
42
  feasible curve with cost <= U per final label (empty = FAIL); layers = per-piece counts (sources, targets,
43
+ pairs, budget_pass, tested, free, live)."""
 
 
 
44
  M = inst.n_pieces; L = getattr(inst, 'n_labels', 1); label = getattr(inst, 'label', None); t0 = time.time()
45
  pairs = hasattr(inst, 'free_pairs'); first = ties == 'first'
46
  idx = np.arange(inst.n_states(0)); lab = np.zeros(len(idx), np.int64); V = np.zeros(len(idx))
 
87
  Vn.ravel()[k[better]] = val[better]; pred.ravel()[k[better]] = si[a[better]]
88
  ll, tt = np.nonzero(np.isfinite(Vn))
89
  st.update(live=len(tt), time=time.time() - tl); layers.append(st)
90
+ if len(tt) == 0: return dict(curves={}, layers=layers, time=time.time() - t0)
91
  idx, lab, V = tt, ll, Vn[ll, tt]; back.append((idx, lab, pred[ll, tt]))
92
  curves = {}
93
  for l in np.unique(lab):
 
96
  path[lev] = int(back[lev][0][j])
97
  if lev > 0: j = int(back[lev][2][j])
98
  curves[int(l)] = (cost, path)
99
+ return dict(curves=curves, layers=layers, time=time.time() - t0)