From f0362cf65a32b33969fc6ce9e2fbbe00173879ea Mon Sep 17 00:00:00 2001 From: zilvinas Date: Wed, 16 Oct 2024 02:37:23 +0300 Subject: [PATCH] .. --- .../{index-RO_tAo3e.js => index-tgGTH1kp.js} | 8 ++--- dist/index.html | 5 ++-- index.html | 5 ++-- index.html.orign | 30 +++++++++++++++++++ src/game-15-reconstruction/EpisodeRunner.ts | 2 ++ .../utils/ConsoleUtils.ts | 9 ++---- 6 files changed, 42 insertions(+), 17 deletions(-) rename dist/assets/{index-RO_tAo3e.js => index-tgGTH1kp.js} (68%) create mode 100644 index.html.orign diff --git a/dist/assets/index-RO_tAo3e.js b/dist/assets/index-tgGTH1kp.js similarity index 68% rename from dist/assets/index-RO_tAo3e.js rename to dist/assets/index-tgGTH1kp.js index 9b83f2c..841fb71 100644 --- a/dist/assets/index-RO_tAo3e.js +++ b/dist/assets/index-tgGTH1kp.js @@ -1,12 +1,12 @@ -var N=Object.defineProperty;var X=(g,t,e)=>t in g?N(g,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):g[t]=e;var u=(g,t,e)=>X(g,typeof t!="symbol"?t+"":t,e);(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const n of document.querySelectorAll('link[rel="modulepreload"]'))s(n);new MutationObserver(n=>{for(const i of n)if(i.type==="childList")for(const c of i.addedNodes)c.tagName==="LINK"&&c.rel==="modulepreload"&&s(c)}).observe(document,{childList:!0,subtree:!0});function e(n){const i={};return n.integrity&&(i.integrity=n.integrity),n.referrerPolicy&&(i.referrerPolicy=n.referrerPolicy),n.crossOrigin==="use-credentials"?i.credentials="include":n.crossOrigin==="anonymous"?i.credentials="omit":i.credentials="same-origin",i}function s(n){if(n.ep)return;n.ep=!0;const i=e(n);fetch(n.href,i)}})();class E{static blue(t){return`\x1B[34m${t}\x1B[0m`}static green(t){return`\x1B[32m${t}\x1B[0m`}static color(t,e){return`\x1B[${e}m${t}\x1B[0m`}static clearScreen(){this.prnt("clearScrn ... ","color: orange;")}static prnt(t,e=""){const s=document.getElementById("console");if(s){const n=document.createElement("div");n.textContent=t,e&&(n.style.cssText=e),s.appendChild(n),s.scrollTop=s.scrollHeight}}static warn(t){this.prnt(t,"color: orange;")}static testPrint(){this.prnt("Hello, World!"),this.prnt("Error: Something went wrong!","color: red;"),this.prnt("Info: This is some information.","color: #00ff00;"),this.prnt("Warning: Be careful!","color: orange;")}}const H=class H{static prnt(t,e="color: black;"){E.prnt(t,e)}static str(t,e){const s=t.toString();if(s.length>=e)return s;const n=e-s.length;return s+H.emptyString.substring(0,n)}static sum(t){return!t||(t=t.filter(e=>e!==0),t.length===0)?0:t.reduce((e,s)=>e+s,0)}static toString(t){return`[${Array.from(t).map(String).join(", ")}]`}static px(){return(t,e)=>t}static equalArrays(t,e){if(t.length!==e.length)return!1;for(let s=0;sMath.random()-.5)}static _sleep(t){return new Promise(e=>setTimeout(e,t))}static async sleep(t){await this._sleep(t)}};u(H,"emptyString"," ");let r=H;var o=(g=>(g.L="L",g.R="R",g.U="U",g.D="D",g))(o||{});class k{constructor(t,e){this.key=t,this.value=e}getKey(){return this.key}getValue(){return this.value}static P(t,e){return new k(t,e)}}class m{static makeMove(t,e){const n=t.indexOf(-1),i=this.getXY(n);let c=i.getKey(),l=i.getValue();e===o.L&&(c-=1),e===o.R&&(c+=1),e===o.U&&(l-=1),e===o.D&&(l+=1);const h=this.getIndex(c,l),d=[...t],p=d[h];return d[n]=p,d[h]=-1,d}static getXY(t){const e=t%4,s=Math.floor(t/4);return k.P(e,s)}static getIndex(t,e){return e*4+t}static _getValidMoves(t){const e=this.getXY(t),s=Object.values(o).slice();return e.getKey()===0&&s.splice(s.indexOf(o.L),1),e.getKey()===3&&s.splice(s.indexOf(o.R),1),e.getValue()===0&&s.splice(s.indexOf(o.U),1),e.getValue()===3&&s.splice(s.indexOf(o.D),1),s}static getValidMoves(t,e){const s=this._getValidMoves(t),n=this.getXY(t);return this.contains(e,n.getKey()-1,n.getValue())&&s.splice(s.indexOf(o.L),1),this.contains(e,n.getKey()+1,n.getValue())&&s.splice(s.indexOf(o.R),1),this.contains(e,n.getKey(),n.getValue()-1)&&s.splice(s.indexOf(o.U),1),this.contains(e,n.getKey(),n.getValue()+1)&&s.splice(s.indexOf(o.D),1),s}static contains(t,e,s){return t.includes(this.getIndex(e,s))}static stateAsString(t,e){return Array.from({length:16},(s,n)=>{let i;const c=t[n];return c===-1?i=E.blue("*"):e.includes(c)?i=E.color(c.toString(),100):e.includes(n+1)?i=E.green(c.toString()):i=c.toString(),i+=" ",n!==0&&(n+1)%4===0&&(i+=` +var N=Object.defineProperty;var X=(g,t,e)=>t in g?N(g,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):g[t]=e;var u=(g,t,e)=>X(g,typeof t!="symbol"?t+"":t,e);(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const n of document.querySelectorAll('link[rel="modulepreload"]'))s(n);new MutationObserver(n=>{for(const i of n)if(i.type==="childList")for(const c of i.addedNodes)c.tagName==="LINK"&&c.rel==="modulepreload"&&s(c)}).observe(document,{childList:!0,subtree:!0});function e(n){const i={};return n.integrity&&(i.integrity=n.integrity),n.referrerPolicy&&(i.referrerPolicy=n.referrerPolicy),n.crossOrigin==="use-credentials"?i.credentials="include":n.crossOrigin==="anonymous"?i.credentials="omit":i.credentials="same-origin",i}function s(n){if(n.ep)return;n.ep=!0;const i=e(n);fetch(n.href,i)}})();class y{static blue(t){return`\x1B[34m${t}\x1B[0m`}static green(t){return`\x1B[32m${t}\x1B[0m`}static color(t,e){return`\x1B[${e}m${t}\x1B[0m`}static clearScreen(){const t=document.getElementById("console");t&&(t.innerHTML="")}static prnt(t,e=""){const s=document.getElementById("console");if(s){const n=document.createElement("span");n.textContent=t,e&&(n.style.cssText=e),s.appendChild(n),s.scrollTop=s.scrollHeight}}static warn(t){this.prnt(t,"color: orange;")}static testPrint(){this.prnt("Hello, World!"),this.prnt("Error: Something went wrong!","color: red;"),this.prnt("Info: This is some information.","color: #00ff00;"),this.prnt("Warning: Be careful!","color: orange;")}}const H=class H{static prnt(t,e="color: black;"){y.prnt(t,e)}static str(t,e){const s=t.toString();if(s.length>=e)return s;const n=e-s.length;return s+H.emptyString.substring(0,n)}static sum(t){return!t||(t=t.filter(e=>e!==0),t.length===0)?0:t.reduce((e,s)=>e+s,0)}static toString(t){return`[${Array.from(t).map(String).join(", ")}]`}static px(){return(t,e)=>t}static equalArrays(t,e){if(t.length!==e.length)return!1;for(let s=0;sMath.random()-.5)}static _sleep(t){return new Promise(e=>setTimeout(e,t))}static async sleep(t){await this._sleep(t)}};u(H,"emptyString"," ");let r=H;var o=(g=>(g.L="L",g.R="R",g.U="U",g.D="D",g))(o||{});class k{constructor(t,e){this.key=t,this.value=e}getKey(){return this.key}getValue(){return this.value}static P(t,e){return new k(t,e)}}class m{static makeMove(t,e){const n=t.indexOf(-1),i=this.getXY(n);let c=i.getKey(),l=i.getValue();e===o.L&&(c-=1),e===o.R&&(c+=1),e===o.U&&(l-=1),e===o.D&&(l+=1);const h=this.getIndex(c,l),d=[...t],p=d[h];return d[n]=p,d[h]=-1,d}static getXY(t){const e=t%4,s=Math.floor(t/4);return k.P(e,s)}static getIndex(t,e){return e*4+t}static _getValidMoves(t){const e=this.getXY(t),s=Object.values(o).slice();return e.getKey()===0&&s.splice(s.indexOf(o.L),1),e.getKey()===3&&s.splice(s.indexOf(o.R),1),e.getValue()===0&&s.splice(s.indexOf(o.U),1),e.getValue()===3&&s.splice(s.indexOf(o.D),1),s}static getValidMoves(t,e){const s=this._getValidMoves(t),n=this.getXY(t);return this.contains(e,n.getKey()-1,n.getValue())&&s.splice(s.indexOf(o.L),1),this.contains(e,n.getKey()+1,n.getValue())&&s.splice(s.indexOf(o.R),1),this.contains(e,n.getKey(),n.getValue()-1)&&s.splice(s.indexOf(o.U),1),this.contains(e,n.getKey(),n.getValue()+1)&&s.splice(s.indexOf(o.D),1),s}static contains(t,e,s){return t.includes(this.getIndex(e,s))}static stateAsString(t,e){return Array.from({length:16},(s,n)=>{let i;const c=t[n];return c===-1?i=y.blue("*"):e.includes(c)?i=y.color(c.toString(),100):e.includes(n+1)?i=y.green(c.toString()):i=c.toString(),i+=" ",n!==0&&(n+1)%4===0&&(i+=` `),i}).join("")}static getReverseAction(t){return t===o.D?o.U:t===o.U?o.D:t===o.L?o.R:t===o.R?o.L:null}}class v{static main(){const t=b.generateLessons()[0];let e=[...t.getState()];const s=[...t.getGoals()];v.prntState(e,s),e=v.shuffle(e,t.getLockedStateElements(),1e3),v.prntState(e,s)}static shuffle(t,e,s){const n=e.map(c=>c-1);let i=0;for(;ii+1),s.state=[...a.stateDone],s.episodesToTrain=100,a.shuffle(s,s.lockedStateElements),s}static moveHole(t,e){const s=r.shuffleArray(e)[0],n=t.state.indexOf(-1),i=t.state[s];return t.state[s]=-1,t.state[n]=i,t}static state7_8(t){const e=new a(t);return e.goals=[7,8],e.lockedStateElements=[1,2,3,4,5,6],e.state=[...a.stateDone],e.episodesToTrain=100,a.shuffle(e,[1,2,3,4,5,6,7]),e}static state9_13(t){const e=new a(t);return e.goals=[9,13],e.lockedStateElements=[1,2,3,4,5,6,7,8],e.state=[...a.stateDone],e.episodesToTrain=100,a.shuffle(e,e.lockedStateElements),e}static state10_15(t){const e=new a(t);return e.goals=[10,11,14,15],e.lockedStateElements=[1,2,3,4,5,6,7,8,9,13],e.state=v.shuffle([...a.stateDone],e.lockedStateElements,500),e.episodesToTrain=100,e}static state12(t){const e=new a(t);return e.goals=[12],e.lockedStateElements=[1,2,3,4,5,6,7,8,9,10,11,13,14,15],e.state=v.shuffle([...a.stateDone],e.lockedStateElements,500),e.episodesToTrain=10,e}isLockedIndex(t){return this.lockedStateElements.includes(t+1)}shuffleState(){this.state=v.shuffle(this.state,this.lockedStateElements,500)}static shuffle(t,e){let s=t.state.filter(n=>!e.includes(n));s=r.shuffleArray(s),t.state=[...e,...s]}resetState(){const t=a.generateLessons()[this.lessonNb];this.goals=[...t.goals],this.lockedStateElements=[...t.lockedStateElements],this.state=[...t.state]}};u(a,"stateDone",[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,-1]);let b=a;class w{constructor(t,e){u(this,"state");u(this,"goals");u(this,"fixedElements");t instanceof w?(this.state=[...t.getState()],this.goals=[...t.getGoals()],this.fixedElements=[...t.getFixedElements()]):(this.state=[...t],this.goals=[...(e==null?void 0:e.getGoals())??[]],this.fixedElements=[...(e==null?void 0:e.getLockedStateElements())??[]])}getState(){return this.state}setState(t){this.state=t}getGoals(){return this.goals}setGoals(t){this.goals=t}getFixedElements(){return this.fixedElements}setFixedElements(t){this.fixedElements=t}getHashCodeV2(){const t=this.getHashCodeV3__();return this.hashString(t)}getHashCodeV3__(){return Array.from({length:16},(t,e)=>{let s;const n=this.state[e];return n===-1?s="*":this.goals.includes(n)?s=String(n):this.goals.includes(e+1)?s="o":s=" ",s=s+" ",e!==0&&(e+1)%4===0&&(s=s+` `),s}).join("")}getHashCode(){const t=this.state.map(String).join(",");return this.hashString(t)}hashString(t){let e=0;for(let s=0;sn-1);return m.getValidMoves(e,s)}executeAction(t,e){const s=m.makeMove(t.getState(),e),n=new w(s,f.stateProducer);let i=f._isTerminalSuccess(s,this.goals);this.state=new w(s,f.stateProducer);let c=NaN;this.reverseAction=m.getReverseAction(e),this.circleAction.push(e),this.circleAction.length>8&&this.circleAction.shift(),(r.equalArrays(this.circleAction,this.bigCircleAction1)||r.equalArrays(this.circleAction,this.bigCircleAction2)||r.equalArrays(this.circleAction,this.smallCircleAction1)||r.equalArrays(this.circleAction,this.smallCircleAction2))&&(i=!0);const l=this.state.getState().indexOf(-1);return f.stateProducer.isLockedIndex(l)&&(i=!0,c=-1),isNaN(c)&&(c=this.getReward(s,this.goals)),new G(n,e,c,i)}static _isTerminalSuccess(t,e){if(t.length!==16)throw new Error("newState.size() != 16");return e.filter(s=>t[s-1]===s).length===e.length}prntInfo(){r.prnt(` ================================================ -`);const t=this.state.getState(),e=t.indexOf(-1),s=m.getXY(e);r.prnt(s);const n=m.getIndex(s.getKey(),s.getValue());r.prnt(`${e} - ${n}`);const i=m._getValidMoves(e);r.prnt(i);const c=this.getReward(t,this.goals);r.prnt(c);const l=m.stateAsString(t,this.goals);r.prnt(l)}getReward(t,e){const s=t.indexOf(-1),n=m.getXY(s),c=e.map(h=>this.getDistance(m.getXY(t.indexOf(h)),m.getXY(h-1))).reduce((h,d)=>h+d,0);if(c===0)return 100.5;const l=e.reduce((h,d)=>h+this.getDistance(m.getXY(t.indexOf(d)),n),0);return r.prnt(`d0Sum: ${c}`),r.prnt(`d1Sum: ${l}`),1/(c+l)}getDistance(t,e){const s=Math.pow(e.getKey()-t.getKey(),2),n=Math.pow(e.getValue()-t.getValue(),2);return Math.sqrt(s+n)}};u(f,"stateProducer");let x=f;class O{constructor(t,e,s,n,i){u(this,"state");u(this,"action");u(this,"reward");u(this,"done");u(this,"newState");this.state=new w(t),this.action=e,this.reward=s,this.done=n,this.newState=new w(i)}getState(){return this.state}getAction(){return this.action}getReward(){return this.reward}isDone(){return this.done}getNewState(){return this.newState}equals(t){return t instanceof O?this.hashCode()===t.hashCode():!1}hashCode(){return this.state.getHashCodeV2()^this.newState.getHashCodeV2()}}class ${constructor(t){u(this,"state");u(this,"qValues");this.state=t,this.qValues=new Map}setValue(t,e){const s=x.getPossibleActions(this.state);if(this.qValues.size===0&&s.forEach(n=>this.qValues.set(n,0)),!s.includes(t)){E.warn("WARNING: !moves.includes(action)");return}this.qValues.set(t,e)}getValue(t){return this.qValues.get(t)||0}getActionWithMaxValue(t){const e=x.getPossibleActions(this.state).filter(n=>n!==t),s=t===null?null:this.getAction(t);return s??(E.warn("WARNING: no action found"),e.length>0?e[0]:o.D)}getAction(t){const s=Array.from(this.qValues.entries()).filter(([n])=>n!==t).reduce((n,i)=>i[1]>n[1]?i:n,[t,-1/0]);return s[1]>-1/0?s[0]:void 0}getMaxValue(){return this.qValues.size===0?0:Math.max(...Array.from(this.qValues.values()))}}class Q{static updateQTable(t,e,s,n,i,c,l,h){let d;if(c)d=i;else{const p=this.getQValue(t,e,s),y=this.getMaxQValue(t,n);d=this.calcQValue(i,p,y,h,l)}this.addStateWithZeroValuesToQTableIfStateNotExist(t,e),this.updateQTableEntry(t,e.getHashCodeV2(),s,d)}static updateQTableEntry(t,e,s,n){var i;(i=t.get(e))==null||i.setValue(s,n)}static getMaxQValue(t,e){var n;const s=e.getHashCodeV2();return this.addStateWithZeroValuesToQTableIfStateNotExist(t,e),((n=t.get(s))==null?void 0:n.getMaxValue())??0}static getQValue(t,e,s){var i;const n=e.getHashCodeV2();return this.addStateWithZeroValuesToQTableIfStateNotExist(t,e),((i=t.get(n))==null?void 0:i.getValue(s))??0}static addStateWithZeroValuesToQTableIfStateNotExist(t,e){t.has(e.getHashCodeV2())||this.addStateWithZeroValuesToQTable(t,e)}static addStateWithZeroValuesToQTable(t,e){const s=e.getHashCodeV2(),n=new $(e);t.set(s,n)}static calcQValue(t,e,s,n,i){return e+i*(t+n*s-e)}}const V=class V{static async runEpisode(t,e,s,n,i){const c=new U,l=new x(t);l.reset();let h=l.getInitState();l.prntInfo();const d=.5;let p=!1,y=0;for(;!p&&y<50;){y++;let S;const A=x.getPossibleActions(h);if(l.reverseAction!==null){const I=A.indexOf(l.reverseAction);A.splice(I,1)}c.nextDouble()this.getDistance(m.getXY(t.indexOf(h)),m.getXY(h-1))).reduce((h,d)=>h+d,0);if(c===0)return 100.5;const l=e.reduce((h,d)=>h+this.getDistance(m.getXY(t.indexOf(d)),n),0);return r.prnt(`d0Sum: ${c}`),r.prnt(`d1Sum: ${l}`),1/(c+l)}getDistance(t,e){const s=Math.pow(e.getKey()-t.getKey(),2),n=Math.pow(e.getValue()-t.getValue(),2);return Math.sqrt(s+n)}};u(f,"stateProducer");let x=f;class O{constructor(t,e,s,n,i){u(this,"state");u(this,"action");u(this,"reward");u(this,"done");u(this,"newState");this.state=new w(t),this.action=e,this.reward=s,this.done=n,this.newState=new w(i)}getState(){return this.state}getAction(){return this.action}getReward(){return this.reward}isDone(){return this.done}getNewState(){return this.newState}equals(t){return t instanceof O?this.hashCode()===t.hashCode():!1}hashCode(){return this.state.getHashCodeV2()^this.newState.getHashCodeV2()}}class ${constructor(t){u(this,"state");u(this,"qValues");this.state=t,this.qValues=new Map}setValue(t,e){const s=x.getPossibleActions(this.state);if(this.qValues.size===0&&s.forEach(n=>this.qValues.set(n,0)),!s.includes(t)){y.warn("WARNING: !moves.includes(action)");return}this.qValues.set(t,e)}getValue(t){return this.qValues.get(t)||0}getActionWithMaxValue(t){const e=x.getPossibleActions(this.state).filter(n=>n!==t),s=t===null?null:this.getAction(t);return s??(y.warn("WARNING: no action found"),e.length>0?e[0]:o.D)}getAction(t){const s=Array.from(this.qValues.entries()).filter(([n])=>n!==t).reduce((n,i)=>i[1]>n[1]?i:n,[t,-1/0]);return s[1]>-1/0?s[0]:void 0}getMaxValue(){return this.qValues.size===0?0:Math.max(...Array.from(this.qValues.values()))}}class Q{static updateQTable(t,e,s,n,i,c,l,h){let d;if(c)d=i;else{const p=this.getQValue(t,e,s),T=this.getMaxQValue(t,n);d=this.calcQValue(i,p,T,h,l)}this.addStateWithZeroValuesToQTableIfStateNotExist(t,e),this.updateQTableEntry(t,e.getHashCodeV2(),s,d)}static updateQTableEntry(t,e,s,n){var i;(i=t.get(e))==null||i.setValue(s,n)}static getMaxQValue(t,e){var n;const s=e.getHashCodeV2();return this.addStateWithZeroValuesToQTableIfStateNotExist(t,e),((n=t.get(s))==null?void 0:n.getMaxValue())??0}static getQValue(t,e,s){var i;const n=e.getHashCodeV2();return this.addStateWithZeroValuesToQTableIfStateNotExist(t,e),((i=t.get(n))==null?void 0:i.getValue(s))??0}static addStateWithZeroValuesToQTableIfStateNotExist(t,e){t.has(e.getHashCodeV2())||this.addStateWithZeroValuesToQTable(t,e)}static addStateWithZeroValuesToQTable(t,e){const s=e.getHashCodeV2(),n=new $(e);t.set(s,n)}static calcQValue(t,e,s,n,i){return e+i*(t+n*s-e)}}const E=class E{static async runEpisode(t,e,s,n,i){const c=new U,l=new x(t);l.reset();let h=l.getInitState();l.prntInfo();const d=.5;let p=!1,T=0;for(;!p&&T<50;){T++;let S;const A=x.getPossibleActions(h);if(l.reverseAction!==null){const q=A.indexOf(l.reverseAction);A.splice(q,1)}c.nextDouble()S+A.qValues.size,0),R=`Episode ${i} done, states count: ${D}, experience size: ${V.experience.size}`;r.prnt(R),r.prnt(""),await r.sleep(1e3)}static replayExperience(t,e,s,n,i){Array.from(t).sort(()=>Math.random()-.5).slice(0,i).forEach(l=>{Q.updateQTable(e,l.getState(),l.getAction(),l.getNewState(),l.getReward(),l.isDone(),s,n)})}};u(V,"experience",new Set);let L=V;class U{nextDouble(){return Math.random()}}class W{static async train(t,e){const i=b.generateLessons(),c=async(h,d)=>{await L.runEpisode(h,t,.9,.1,d)},l=async h=>{for(let d=0;dArray.from(l.qValues.values())),s=e.reduce((l,h)=>l+h,0),n=e.length,i=n?s/n:0,c=new K;return c.count=n,c.sum=s,c.average=i,c}static async testQTable(t){r.prnt("********************* test q table **********************"),r.prnt("********************* test q table **********************"),r.prnt("********************* test q table **********************");let e=0;const s=b.generateLessons(),n=s.length;let i=s[e],c=v.shuffle(b.stateDone,[],1e3),l=new w(c,i),h=i.getGoals();this.prntState(l);let d=!1,p=0,y=null;for(;!d&&p<200;){await r.sleep(1e3/2),E.clearScreen(),p++;const R=l.getHashCodeV2();Q.addStateWithZeroValuesToQTableIfStateNotExist(t,l);const S=t.get(R),A=S?S.getActionWithMaxValue(y):o.D;y=m.getReverseAction(A);const T=m.makeMove(l.getState(),A),M=x._isTerminalSuccess(T,h);l=new w(T,i),d=r.equalArrays(l.getState(),b.stateDone),r.prnt(`${p} +action: `+S),A.includes(S);const V=l.executeAction(h,S),R=V.state;l.prntInfo();const P=V.reward;p=V.isTerminal,p&&(p=!0);const I=new O(h,S,P,p,R);E.experience.add(I),h=V.state}E.replayExperience(E.experience,e,n,s,1e3);const D=Array.from(e.values()).reduce((S,A)=>S+A.qValues.size,0),M=`Episode ${i} done, states count: ${D}, experience size: ${E.experience.size}`;r.prnt(M),r.prnt(""),await r.sleep(1e3),y.clearScreen()}static replayExperience(t,e,s,n,i){Array.from(t).sort(()=>Math.random()-.5).slice(0,i).forEach(l=>{Q.updateQTable(e,l.getState(),l.getAction(),l.getNewState(),l.getReward(),l.isDone(),s,n)})}};u(E,"experience",new Set);let L=E;class U{nextDouble(){return Math.random()}}class W{static async train(t,e){const i=b.generateLessons(),c=async(h,d)=>{await L.runEpisode(h,t,.9,.1,d)},l=async h=>{for(let d=0;dArray.from(l.qValues.values())),s=e.reduce((l,h)=>l+h,0),n=e.length,i=n?s/n:0,c=new K;return c.count=n,c.sum=s,c.average=i,c}static async testQTable(t){r.prnt("********************* test q table **********************"),r.prnt("********************* test q table **********************"),r.prnt("********************* test q table **********************");let e=0;const s=b.generateLessons(),n=s.length;let i=s[e],c=v.shuffle(b.stateDone,[],1e3),l=new w(c,i),h=i.getGoals();this.prntState(l);let d=!1,p=0,T=null;for(;!d&&p<200;){await r.sleep(1e3/2),y.clearScreen(),p++;const M=l.getHashCodeV2();Q.addStateWithZeroValuesToQTableIfStateNotExist(t,l);const S=t.get(M),A=S?S.getActionWithMaxValue(T):o.D;T=m.getReverseAction(A);const V=m.makeMove(l.getState(),A),R=x._isTerminalSuccess(V,h);l=new w(V,i),d=r.equalArrays(l.getState(),b.stateDone),r.prnt(`${p} ---- -`),this.prntState(l),M&&!d&&el!==s),t.has(n)?((c=t.get(n))==null?void 0:c.getActionWithMaxValue(s))||o.D:this.getRandomAction(i)}static getRandomAction(t){return t.length>0?t[Math.floor(Math.random()*t.length)]:o.D}}class K{constructor(){u(this,"count",0);u(this,"sum",0);u(this,"average",0)}}class Y{static async main(){r.prnt("tarining starts in:");for(let t=0;t<3;t++)r.prnt(t),await r.sleep(500);r.prnt("tarining .. "),await r.sleep(1e3),await C.train(),await r.sleep(1e3),r.prnt("testing .. "),await r.sleep(1e3),await C.test()}}Y.main().then(()=>{console.log("G15EntryPoint has completed execution.")}).catch(g=>{console.error("An error occurred:",g)}); +`),this.prntState(l),R&&!d&&el!==s),t.has(n)?((c=t.get(n))==null?void 0:c.getActionWithMaxValue(s))||o.D:this.getRandomAction(i)}static getRandomAction(t){return t.length>0?t[Math.floor(Math.random()*t.length)]:o.D}}class K{constructor(){u(this,"count",0);u(this,"sum",0);u(this,"average",0)}}class B{static async main(){r.prnt("tarining starts in:");for(let t=0;t<3;t++)r.prnt(t),await r.sleep(500);r.prnt("tarining .. "),await r.sleep(1e3),await C.train(),await r.sleep(1e3),r.prnt("testing .. "),await r.sleep(1e3),await C.test()}}B.main().then(()=>{console.log("G15EntryPoint has completed execution.")}).catch(g=>{console.error("An error occurred:",g)}); diff --git a/dist/index.html b/dist/index.html index 1db0055..e4509c1 100644 --- a/dist/index.html +++ b/dist/index.html @@ -12,20 +12,19 @@ font-family: monospace; padding: 10px; border-radius: 5px; - max-height: 400px; + max-height: 800px; overflow-y: auto; white-space: pre-wrap; /* Preserve formatting */ } - +
- \ No newline at end of file diff --git a/index.html b/index.html index 376477d..e470ea5 100644 --- a/index.html +++ b/index.html @@ -12,20 +12,19 @@ font-family: monospace; padding: 10px; border-radius: 5px; - max-height: 400px; + max-height: 800px; overflow-y: auto; white-space: pre-wrap; /* Preserve formatting */ } +
- - \ No newline at end of file diff --git a/index.html.orign b/index.html.orign new file mode 100644 index 0000000..f51c501 --- /dev/null +++ b/index.html.orign @@ -0,0 +1,30 @@ + + + + + + + console-like Output + + + + +
+
+
+ + + + \ No newline at end of file diff --git a/src/game-15-reconstruction/EpisodeRunner.ts b/src/game-15-reconstruction/EpisodeRunner.ts index 42bbb74..e7fca77 100644 --- a/src/game-15-reconstruction/EpisodeRunner.ts +++ b/src/game-15-reconstruction/EpisodeRunner.ts @@ -7,6 +7,7 @@ import { QTableUpdater } from './QTableUpdater'; import { Action } from './Action'; import { EnvironmentActionResult } from './EnvironmentActionResult'; import { Utils } from './utils/Utils'; +import { ConsoleUtils } from './utils/ConsoleUtils'; export class EpisodeRunner { private static experience: Set = new Set(); @@ -81,6 +82,7 @@ export class EpisodeRunner { Utils.prnt(message); Utils.prnt(""); await Utils.sleep(1000); + ConsoleUtils.clearScreen(); } public static replayExperience( diff --git a/src/game-15-reconstruction/utils/ConsoleUtils.ts b/src/game-15-reconstruction/utils/ConsoleUtils.ts index a332934..d5e4931 100644 --- a/src/game-15-reconstruction/utils/ConsoleUtils.ts +++ b/src/game-15-reconstruction/utils/ConsoleUtils.ts @@ -13,25 +13,20 @@ export class ConsoleUtils { } public static clearScreen() { - this.prnt("clearScrn ... ", "color: orange;"); + const consoleDiv = document.getElementById('console'); + if (consoleDiv) { consoleDiv.innerHTML = ''; } } public static prnt(message: string, style: string = ""): void { const consoleDiv = document.getElementById('console'); if (consoleDiv) { - // Create a new div element for each message const messageElement = document.createElement('div'); messageElement.textContent = message; - - // Apply custom styles if provided if (style) { messageElement.style.cssText = style; } - // Append the message to the custom Utils.prnt consoleDiv.appendChild(messageElement); - - // Scroll to the bottom of the Utils.prnt to show the latest message consoleDiv.scrollTop = consoleDiv.scrollHeight; } }