@hackbuild/agents-views 0.2.13 → 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/yard/scope.js +64 -41
  2. package/package.json +2 -2
@@ -2013,6 +2013,8 @@ function micTick(dt) { if (!micAn) return; micAn.getFloatTimeDomainData(micBuf);
2013
2013
  // ---- yard/io/speech.js
2014
2014
  // ---------- speech ----------
2015
2015
  const ACTIONS = ['sit', 'down', 'come', 'stay', 'spin', 'speak', 'paw', 'fetch', 'bed'];
2016
+ // the acts the dog offers on its own: not fetch, bed or come
2017
+ const LEARN_OFFER = ACTIONS.filter(a => a !== 'fetch' && a !== 'bed' && a !== 'come');
2016
2018
  const SYN = { 'lie': 'down', 'lay': 'down', 'here': 'come', 'come here': 'come', 'roll': 'spin', 'turn': 'spin', 'bark': 'speak', 'shake': 'paw', 'go to bed': 'bed', 'bedtime': 'bed', 'get it': 'fetch', 'ball': 'fetch' };
2017
2019
  function handleSpeech(text) {
2018
2020
  const s = text.toLowerCase().replace(/[^a-z ]/g, ' ').trim(); if (!s) return;
@@ -2072,7 +2074,7 @@ const brain = {
2072
2074
  cur: 'idle', t: 0, sub: '', subT: 0, v: 0.25, a: 0.35,
2073
2075
  d: { hunger: 0.35, fatigue: 0.15, play: 0.5, social: 0.4, curiosity: 0.5, prey: 0 },
2074
2076
  util: {}, goal: null, goalT: 0, cmd: null, trial: null, sayT: 0, line: '...',
2075
- W: {}, N: {}, al: {}, offer: {}, trace: [], mem: [], imp: 0, visits: new Map(), barkT: 0, startle: 0, sleepWake: 0,
2077
+ W: {}, N: {}, al: {}, offer: {}, mem: [], imp: 0, visits: new Map(), barkT: 0, startle: 0, sleepWake: 0,
2076
2078
  bond: 0.5, Sp: 0.25, stage: 'awake', hyp: [], pending: null, dwell: 0, done: false, begRel: { n: 2, ok: 1 }, prevD: {}, negT: -99,
2077
2079
  speedMul() { return (this.cur === 'sleep' ? 0.6 : 1) * (1 - this.d.fatigue * 0.35) * (this.v < -0.3 ? 0.85 : 1) * (ON('L.organic') ? 1 + 0.08 * PT('activity') : 1); },
2078
2080
  init() {
@@ -2080,6 +2082,8 @@ const brain = {
2080
2082
  if (saved && saved.W) { this.W = saved.W; this.N = saved.N || {}; this.al = saved.al || {}; this.offer = saved.offer || {}; this.bond = saved.bond ?? 0.5; }
2081
2083
  else if (saved && !saved.W) this.W = saved; else this.seed();
2082
2084
  for (const c in this.W) this.ensure(c);
2085
+ for (const a of ACTIONS) this.offer[a] ??= 0.15;
2086
+ this.loadLearner();
2083
2087
  renderCueButtons(); renderHeat();
2084
2088
  },
2085
2089
  persist() { store.set('W', { W: this.W, N: this.N, al: this.al, offer: this.offer, bond: this.bond }); },
@@ -2089,8 +2093,9 @@ const brain = {
2089
2093
  const prior = { sit: 0.5, come: 0.55, down: 0.3, fetch: 0.35, bed: 0.15, speak: 0.12, spin: 0.08, stay: 0.2, paw: 0.08 };
2090
2094
  for (const a of ACTIONS) { this.W[a] = Object.fromEntries(ACTIONS.map(b => [b, b === a ? prior[a] : 0.02 + RNG() * 0.03])); this.ensure(a); }
2091
2095
  for (const a of ACTIONS) this.offer[a] = a === 'sit' ? 1 : a === 'down' ? 0.5 : 0.15;
2096
+ this.loadLearner();
2092
2097
  },
2093
- V(c, a) { return (this.W[c] ? this.W[c][a] : 0) - (ON('L.learn') && this.N[c] ? this.N[c][a] : 0); },
2098
+ V(c, a) { return (this.W[c] ? this.W[c][a] : 0) - (this.N[c] ? this.N[c][a] : 0); },
2094
2099
  say(s, dur = 2.5) { if (!s) return; this.line = s; this.sayT = dur; },
2095
2100
  remember(txt, imp) { this.mem.unshift({ t: now, h: CLOCK.day * 24 + CLOCK.h, txt, imp, ev: EV.n }); if (this.mem.length > 60) this.mem.pop(); this.imp += imp; },
2096
2101
  set(b, why = '') {
@@ -2153,40 +2158,66 @@ const brain = {
2153
2158
 
2154
2159
  // ---- yard/dog/learning.js
2155
2160
  // dog brain: cues and tricks. A cue starts a trial, an offered trick waits for a reward, and the
2156
- // reward updates the cue to action values through an eligibility trace.
2161
+ // reward updates the cue to action values through an eligibility trace. The table, the trace and the
2162
+ // offer history are the library's CueLearner, stepped on the yard's stream (RNG); brain.W, N, al and
2163
+ // offer are read only copies for the panels, refreshed after every change.
2164
+ let LEARN = null;
2165
+ ENV.dispose(() => LEARN && LEARN.free());
2166
+ const learnShape = () => LEARN.configure({
2167
+ alpha: TV('Ln.alpha'), theta: TV('Ln.theta'), traceSeconds: TV('Ln.trace'), inhibitionDecay: TV('Ln.inhib'), offerRate: TV('Ln.offer'),
2168
+ ignore: TV('Br.ignore'), cueMu: TV('Br.cueMu'), cueTau: TV('Br.cueTau'), noise: ON('L.noise'), refusals: ON('L.organic'),
2169
+ });
2157
2170
  Object.assign(brain, {
2171
+ // a fresh learner holding brain.W, N, al and offer as they stand
2172
+ loadLearner() {
2173
+ if (LEARN) LEARN.free();
2174
+ LEARN = new HDRIVE.CueLearner({ rng: () => 0.5, actions: ACTIONS }); learnShape();
2175
+ LEARN.load({ ver: 1, cues: Object.fromEntries(Object.keys(this.W).map(c => [c, { w: this.W[c], n: this.N[c], associability: this.al[c] }])), offer: this.offer });
2176
+ this.syncLearn();
2177
+ },
2178
+ // the copies have no prototype, so a word such as constructor or __proto__ is an ordinary cue
2179
+ syncLearn() {
2180
+ const s = LEARN.toJSON(), W = Object.create(null), N = Object.create(null), al = Object.create(null);
2181
+ for (const c of Object.keys(s.cues)) { const r = s.cues[c]; W[c] = r.w; N[c] = r.n; al[c] = r.associability; }
2182
+ this.W = W; this.N = N; this.al = al;
2183
+ Object.assign(this.offer, s.offer);
2184
+ },
2158
2185
  cue(word) {
2159
2186
  if (!word) return;
2160
2187
  word = word.toLowerCase().slice(0, 18);
2161
2188
  const cueEv = EV.emit({ agent: 'dog', kind: 'cue', text: 'heard "' + word + '"' });
2162
- if (!this.W[word]) { this.W[word] = Object.fromEntries(ACTIONS.map(b => [b, 0.02 + RNG() * 0.03])); this.ensure(word); renderCueButtons(); }
2189
+ if (!this.W[word]) { learnShape(); LEARN.ensureWith(RNG, word); this.syncLearn(); renderCueButtons(); }
2163
2190
  if (this.cur === 'sleep') this.wake('heard "' + word + '"');
2164
2191
  const tr = ON('L.organic') ? PT('trainability') : 0;
2165
2192
  const distract = this.d.prey * 1.5 + (this.cur === 'chase' ? 1 : 0) + (this.attn > now ? -0.4 : 0) - tr * 0.12;
2166
2193
  // everyday non compliance is high (Browne: 44 percent of sit and down ignored); more when he is not watching
2167
- const watched = HUM.lookingAtDog || (HUM.task && HUM.task.name === 'train');
2168
- if (ON('L.organic') && RNG() < clamp(TV('Br.ignore') - 0.08 * tr + 0.4 * distract + (watched ? 0 : 0.15), 0.03, 0.85) && this.V(word, pick(ACTIONS)) < 2) {
2194
+ const watched = !!(HUM.lookingAtDog || (HUM.task && HUM.task.name === 'train'));
2195
+ // an ignored cue still counts as a trial for massed practice; noise off is the canned path, with no refusals.
2196
+ // then a Gumbel draw per act against a bar raised by distraction; nothing strong: offer something it has been
2197
+ // rewarded for before (free operant history); then the latency
2198
+ learnShape();
2199
+ const res = LEARN.respondWith(RNG, word, now, { distraction: distract, trainability: tr, watched, offerable: LEARN_OFFER });
2200
+ if (res.kind === 'ignored') {
2169
2201
  this.trial = { cue: word, action: 'ignored', t0: now, done: now, rewarded: false, ev: cueEv }; dog.earPerk = Math.max(dog.earPerk, 0.5); this.say(pick(['...', 'hm?', '']), 1); renderHeat(word, null); return;
2170
2202
  }
2171
- let best = 'none', bs = 0.75 + distract;
2172
- for (const a of ACTIONS) { const g = -Math.log(-Math.log(RNG() + 1e-9) + 1e-9) * 0.55; const s = this.V(word, a) * 4 * (1 + 0.1 * tr) + g; if (s > bs) { bs = s; best = a; } }
2173
- // nothing strong: offer something it has been rewarded for before (free operant history), or a random guess
2174
- if (best === 'none' && RNG() < 0.5 && distract < 0.8) { best = ON('L.learn') ? this.sampleOffer() : pick(['sit', 'down', 'spin', 'speak', 'paw', 'come']); this.say('this?', 2); }
2203
+ const best = res.kind === 'act' ? res.action : 'none';
2204
+ if (res.kind === 'act' && res.offered) this.say('this?', 2);
2175
2205
  this.trial = { cue: word, action: best, t0: now, done: null, rewarded: false, ev: cueEv };
2176
2206
  dog.postureT.sit = dog.postureT.down = dog.postureT.bow = dog.postureT.reach = 0; dog.pawLift = 0; motor.mode = 'stop';
2177
- const lat = ON('L.noise') ? ORG.exg(TV('Br.cueMu') * (1 - 0.15 * tr), 0.08, TV('Br.cueTau') * (1 - 0.2 * tr)) : 0;
2207
+ const lat = res.latency;
2178
2208
  const go = () => {
2179
2209
  this.kindNext = 'cue'; this.causeNext = cueEv;
2180
2210
  if (best === 'none') { this.set('confused', 'did not understand "' + word + '"'); this.cmd = { action: 'none', t: 0 }; }
2181
2211
  else { this.cmd = { action: best, t: 0, cue: word }; this.set('obey', '"' + word + '" means ' + best + ' (V=' + this.V(word, best).toFixed(2) + ', latency ' + Math.round(lat * 1000) + ' ms)'); }
2182
- this.trace.unshift({ cue: word, action: best, t: now, done: null }); if (this.trace.length > 8) this.trace.pop();
2183
2212
  };
2184
2213
  this.pending = null; if (lat > 0.05) { this.pending = { fn: go, at: now + lat, look: V3(YOU.x, 1.5, YOU.z), b: best === 'none' ? 'confused' : 'obey' }; dog.earPerk = 1; } else go();
2185
2214
  renderHeat(word, best);
2186
2215
  },
2187
- sampleOffer() { const e = ACTIONS.filter(a => a !== 'fetch' && a !== 'bed' && a !== 'come').map(a => [a, Math.exp((this.offer[a] || 0.1) * 2)]); let tot = e.reduce((s, x) => s + x[1], 0), r = RNG() * tot; for (const [a, w] of e) if ((r -= w) <= 0) return a; return 'sit'; },
2216
+ sampleOffer() { learnShape(); return LEARN.sampleOfferWith(RNG, LEARN_OFFER) ?? 'sit'; },
2188
2217
  // a trick the dog does on its own (free operant): reward can later attach it to whatever cue the human says
2189
- offerTrick(a) { this.offered = { a, t: now }; this.trace.unshift({ cue: null, action: a, t: now, done: now }); if (this.trace.length > 8) this.trace.pop(); },
2218
+ offerTrick(a) { this.offered = { a, t: now }; LEARN.offerTrick(a, now); },
2219
+ // the act a cue started finished: its trial in the trace is done, so its credit ages from now
2220
+ trialDone(action) { if (ACTIONS.includes(action)) LEARN.markDone(action, now); },
2190
2221
  reward(r, src) {
2191
2222
  EV.emit({ agent: 'dog', kind: 'reward', text: (r > 0 ? 'got ' : 'was told off: ') + src + ' (' + (r > 0 ? '+' : '') + r.toFixed(1) + ')', cause: this.trial && now - this.trial.t0 < 25 ? this.trial.ev : null });
2192
2223
  this.v = clamp(this.v + 0.3 * r, -1, 1); this.a = clamp(this.a + 0.12 * Math.abs(r), 0, 1);
@@ -2195,10 +2226,12 @@ Object.assign(brain, {
2195
2226
  if (ON('L.learn')) this.learnTrace(r, src);
2196
2227
  else {
2197
2228
  const tr = this.trial;
2198
- if (tr && !tr.rewarded && tr.action !== 'none' && now - (tr.done || now) < 4.5 && now - tr.t0 < 25) {
2229
+ if (tr && !tr.rewarded && tr.action !== 'none' && tr.action !== 'ignored' && now - (tr.done || now) < 4.5 && now - tr.t0 < 25) {
2199
2230
  const row = this.W[tr.cue], alpha = 0.35 * Math.abs(r);
2200
2231
  if (r > 0) { row[tr.action] += alpha * (1 - row[tr.action]); for (const a of ACTIONS) if (a !== tr.action) row[a] -= alpha * 0.25 * row[a]; }
2201
2232
  else row[tr.action] += alpha * (0 - row[tr.action]);
2233
+ for (const a of ACTIONS) LEARN.prior(tr.cue, a, row[a]);
2234
+ this.syncLearn();
2202
2235
  tr.rewarded = true; this.persist(); renderHeat(tr.cue, tr.action);
2203
2236
  $('heard').textContent = (r > 0 ? 'learned: ' : 'unlearned: ') + '"' + tr.cue + '" -> ' + tr.action + ' = ' + row[tr.action].toFixed(2);
2204
2237
  }
@@ -2208,32 +2241,21 @@ Object.assign(brain, {
2208
2241
  // a scolding from the human brings the appeasement set, keyed to the human's posture, not to any misdeed (Horowitz 2009)
2209
2242
  if (r < 0 && ON('L.social') && (src === 'no' || src === 'scold') && this.cur !== 'sleep' && (r <= -0.7 || RNG() < 0.2 + 0.25 * PT('fearfulness'))) this.force('appease', 2.5);
2210
2243
  },
2211
- // eligibility traces plus Pearce Kaye Hall associability: credit fades with delay, so a late treat can teach the wrong thing
2244
+ // eligibility traces plus Pearce Kaye Hall associability: credit fades with delay, so a late treat can teach the wrong thing.
2245
+ // spaced beats massed practice (Meyer and Ladewig 2008, Demant 2011): many recent trials damp the learning rate.
2246
+ // the most recent eligible pair takes the credit
2212
2247
  learnTrace(r, src) {
2213
- // spaced beats massed practice (Meyer and Ladewig 2008, Demant 2011): many recent trials damp the learning rate
2214
- const recentN = this.trace.filter(e => now - e.t < 120).length;
2215
- const lam = r > 0 ? 1 : 0, base = TV('Ln.alpha') * (1 + 0.15 * PT('trainability')) / (1 + 0.12 * recentN), th = TV('Ln.theta');
2216
- let any = false, txt = '';
2217
- for (const e of this.trace) {
2218
- if (e.credited) continue;
2219
- const age = now - (e.done || e.t), w = Math.exp(-age / TV('Ln.trace')); if (w < 0.05) continue;
2220
- if (e.action === 'none' || e.action === 'ignored') continue;
2221
- if (e.cue) {
2222
- this.ensure(e.cue); const Vv = this.V(e.cue, e.action), al = this.al[e.cue], k = base * al * w * Math.abs(r); const tap = { cue: e.cue, action: e.action, before: Vv, credit: w, r, src, t: now, age };
2223
- if (lam > Vv) { this.W[e.cue][e.action] = clamp(this.W[e.cue][e.action] + k * (lam - Vv), 0, 1.2); for (const a of ACTIONS) if (a !== e.action) this.W[e.cue][a] *= 1 - k * 0.15; }
2224
- else this.N[e.cue][e.action] = clamp(this.N[e.cue][e.action] + k * (Vv - lam), 0, 1.2);
2225
- this.al[e.cue] = clamp((1 - th) * al + th * Math.abs(lam - Vv), 0.05, 1);
2226
- txt = (r > 0 ? 'learned: ' : 'unlearned: ') + '"' + e.cue + '" -> ' + e.action + ' V=' + this.V(e.cue, e.action).toFixed(2) + ' (credit ' + Math.round(w * 100) + '%)';
2227
- tap.after = this.V(e.cue, e.action); this.lastLearn = tap;
2228
- renderHeat(e.cue, e.action);
2229
- }
2230
- this.offer[e.action] = clamp((this.offer[e.action] || 0.1) + (r > 0 ? 0.25 : -0.15) * w * TV('Ln.offer'), 0, 3);
2231
- e.credited = true; any = true;
2232
- if (this.trial && this.trial.cue === e.cue && this.trial.action === e.action) this.trial.rewarded = true;
2233
- break; // the most recent eligible pair takes the credit
2248
+ learnShape();
2249
+ const c = LEARN.reward(r, now, PT('trainability'));
2250
+ if (!c) return;
2251
+ this.syncLearn();
2252
+ if (c.cue) {
2253
+ this.lastLearn = { cue: c.cue, action: c.action, before: c.before, credit: c.credit, r, src, t: now, age: c.age, after: c.value };
2254
+ renderHeat(c.cue, c.action);
2255
+ $('heard').textContent = (r > 0 ? 'learned: ' : 'unlearned: ') + '"' + c.cue + '" -> ' + c.action + ' V=' + c.value.toFixed(2) + ' (credit ' + Math.round(c.credit * 100) + '%)';
2234
2256
  }
2235
- if (txt) $('heard').textContent = txt;
2236
- if (any) this.persist();
2257
+ if (this.trial && this.trial.cue === c.cue && this.trial.action === c.action) this.trial.rewarded = true;
2258
+ this.persist();
2237
2259
  },
2238
2260
  });
2239
2261
 
@@ -2287,7 +2309,8 @@ Object.assign(brain, {
2287
2309
  this.a += ((0.25 + d.play * 0.3 + d.prey * 0.4 - (this.cur === 'sleep' ? 0.25 : 0)) - this.a) * dt * 0.15;
2288
2310
  dog.earPerk += ((this.a - 0.4) * 1.2 - dog.earPerk) * dt * 1.5;
2289
2311
  dog.hop = Math.max(0, dog.hop - dt * 0.12); this.startle = Math.max(0, this.startle - dt);
2290
- if (ON('L.learn')) { const k = Math.exp(-TV('Ln.inhib') * dt / 60); for (const c in this.N) for (const a in this.N[c]) this.N[c][a] *= k; }
2312
+ // inhibition decays per minute, faster than excitation
2313
+ if (ON('L.learn') && LEARN) { learnShape(); LEARN.decay(dt); this.syncLearn(); }
2291
2314
  // bond: mutual gaze, praise and touch raise it (MacLean and Hare 2015), it decays very slowly
2292
2315
  if (ON('L.social')) {
2293
2316
  const youH = humHead(tmpV[6]); const hf = headForward(tmpV[7]); const toY = youH.clone().sub(headWorld(dog.eyeL, V3())).normalize();
@@ -2563,7 +2586,7 @@ Object.assign(brain, {
2563
2586
  },
2564
2587
  obey(dt, youP, youHead) {
2565
2588
  const c = this.cmd, P = dog.postureT; if (!c) { this.done = true; return; } c.t += dt;
2566
- const finish = (hold = 6) => { if (this.trial && !this.trial.done) { this.trial.done = now; const e = this.trace.find(x => x.cue === c.cue && !x.done); if (e) e.done = now; this.say(pick(LINES.obey), 1.8); } if (c.t > hold) { this.cmd = null; this.done = true; } };
2589
+ const finish = (hold = 6) => { if (this.trial && !this.trial.done) { this.trial.done = now; this.trialDone(c.action); this.say(pick(LINES.obey), 1.8); } if (c.t > hold) { this.cmd = null; this.done = true; } };
2567
2590
  const sitDone = () => NG.active ? (NG.guidance === 'Sit' && this.t > 1.4) : dog.posture.sit > 0.85, downDone = () => NG.active ? (NG.guidance === 'Lie' && this.t > 1.8) : dog.posture.down > 0.85;
2568
2591
  switch (c.action) {
2569
2592
  case 'sit': motor.mode = 'stop'; P.sit = 1; lookAt(youHead); if (sitDone()) finish(7); break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hackbuild/agents-views",
3
- "version": "0.2.13",
3
+ "version": "0.2.14",
4
4
  "description": "Views that bind the agents packages to the hackbuild-ui class layer: the behaviour, utility and drive bars, the cues by acts associations table, the eye inset, and Yard, the dog simulator's stage and rack as one view. Each small view ships as a Vue component and as a custom element.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  "@hackbuild/gait": "^0.1.0",
50
50
  "@hackbuild/tunables": "^0.1.0",
51
51
  "@hackbuild/senses": "0.1.5",
52
- "@hackbuild/drives": "0.1.7",
52
+ "@hackbuild/drives": "0.1.8",
53
53
  "@hackbuild/cameras": "^0.1.0",
54
54
  "@hackbuild/weights": "^0.1.0"
55
55
  },