<commit_before>package nars.lab.microworld;

import nars.storage.Memory;
import nars.NAR;
//import nars.nal.nal8.Operation;
//import nars.nal.nal8.operator.SyncOperator;
//import nars.nar.Default;
//import nars.task.Task;
import processing.core.PApplet;
import processing.core.PImage;
import processing.event.MouseEvent;

import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import nars.entity.Concept;
import nars.entity.Sentence;
import nars.entity.Task;
import nars.gui.NARSwing;
import nars.io.Narsese;
import nars.lab.testutils.ConceptMonitor;
import nars.language.Term;
import nars.operator.Operation;
import nars.operator.Operator;

public class Pong extends Frame {

    public Pong() {
        String[] args = {"Pong"};
        MyPapplet mp = new MyPapplet ();
        mp.setSize(800,600);
        PApplet.runSketch(args, mp);
    }
    
    static MyPapplet.Obj agent;
        static MyPapplet.Obj ball;

    public class MyPapplet extends PApplet {

        float mouseScroll = 0;
        public void mouseWheel(MouseEvent event) {
            mouseScroll = -event.getCount();
            hamlib.mouseScrolled();
        }

        class Hai
        {
            Hai(){}
            public Obj obj;
            NAR nar;
            String LastInput ="";
            int nActions = 3;
            int direction = 0;
            Hai(int nactions,int nstates)
            {
                this.nActions = nactions; //for actions since we allow the same randomization phase as in QL
                nar = new NAR();
                nar.memory.addOperator(new Right("^Right"));
                nar.memory.addOperator(new Left("^Left"));
                (nar.param).noiseLevel.set(0);
                new NARSwing(nar); 
                //nar.start(0);
                Memory m = nar.memory;
               // m.conceptForgetDurations.setValue(1.0); //better for declarative reasoning tasks: 2
                //m.taskLinkForgetDurations.setValue(1.0); //better for declarative reasoning tasks: 4
                //m.termLinkForgetDurations.setValue(1.0); //better for declarative reasoning tasks: 10
                //NARide.show(nar.loop(), (i) -> {});
            }


            int lastAction=0;
            public class Right extends Operator {
                public Right(String name) {
                    super(name);
                }

                @Override
                public List<Task> execute(Operation operation, Term[] args, Memory memory) {
                    lastAction = 1;
                    memory.allowExecution = false;
                    //System.out.println("NAR decide left");
                    return null;
                }
            }
            public class Left extends Operator {
                public Left(String name) {
                    super(name);
                }

                @Override
                public List<Task> execute(Operation operation, Term[] args, Memory memory) {
                    lastAction = 2;
                    memory.allowExecution = false;
                    //System.out.println("NAR decide right");
                    return null;
                }
            }

            int k=0;
            float Alpha=0.08f;
            boolean lastWasMiddle = false;
            int UpdateSOM(float[] viewField,float reward) //input and reward
            {
                /*for(int i=0;i<viewField.length;i++) {
                    if(viewField[i]>0.1f) {
                        String s = "<{\""+String.valueOf(i)+"\"} --> [on]>. :|:";  // %"+String.valueOf(0.5f+0.5f*viewField[i])+"%";
                        if(this.LastInput != s) {
                            nar.addInput(s);
                            System.out.println(s);
                        }
                        this.LastInput = s;

                        //System.out.println("perceive "+s);
                    }
                }*/
                lastAction = 0;
                k++;
                if(k%4==0 && agent.y < 50) {
                    nar.addInput("<{\"1\"} --> [on]>! :|:"); //future extension: change to self good
                    //System.out.println("food urge input");
                }
                //if(reward > 0) { //even more delayed reward (for the future)
                
                //show what the system perceived about the ballposition:
                /*Sentence left = ConceptMonitor.strongestProjectedInputEventBelief(nar, "<{left} --> [on]>");
                Sentence right = ConceptMonitor.strongestProjectedInputEventBelief(nar, "<{right} --> [on]>");
                Sentence middle = ConceptMonitor.strongestProjectedInputEventBelief(nar, "<{\"1\"} --> [on]>");
                if(left != null) {
                    System.out.print("left: ");
                    System.out.print(left.truth);
                    System.out.print(" ");
                }
                if(middle != null) {
                    System.out.print("middle: ");
                    System.out.print(middle.truth);
                    System.out.print(" ");
                }
                if(right != null) {
                    System.out.print("right: ");
                    System.out.print(right.truth);
                    System.out.print(" ");
                }
                if(left != null || middle != null || right != null) {
                    System.out.println();
                }*/
                                                   //                                         <(&/,<{right} --> [on]>,+12,(^Left,SELF),+13) =/> <{"1"} --> [on]>>
                                                   
                
                if(k%4 == 0) {
                Sentence hypo_left = ConceptMonitor.strongestPrecondition(nar, "<{\"1\"} --> [on]>",
                        "<(&/,<{left} --> [on]>,+1,(^Right,SELF),+1) =/> <{\"1\"} --> [on]>>");
                if(hypo_left != null) {
                    System.out.println("HypLeft: " + hypo_left.truth);
                }
                
                Sentence hypo_right = ConceptMonitor.strongestPrecondition(nar, "<{\"1\"} --> [on]>",
                        "<(&/,<{right} --> [on]>,+12,(^Left,SELF),+13) =/> <{\"1\"} --> [on]>>");
                if(hypo_right != null) {
                    System.out.println("HypRight: " + hypo_right.truth);
                }
                
                Sentence hypo_left_false = ConceptMonitor.strongestPrecondition(nar, "<{\"1\"} --> [on]>",
                        "<(&/,<{left} --> [on]>,+1,(^Left,SELF),+1) =/> <{\"1\"} --> [on]>>");
                if(hypo_left_false != null) {
                    System.out.println("HypLefFalse: " + hypo_left_false.truth);
                }
                
                Sentence hypo_right_false = ConceptMonitor.strongestPrecondition(nar,  "<{\"1\"} --> [on]>",
                        "<(&/,<{right} --> [on]>,+12,(^Right,SELF),+13) =/> <{\"1\"} --> [on]>>");
                if(hypo_right_false != null) {
                    System.out.println("HypRigFalse: " + hypo_right_false.truth);
                }
                }
                
                float accepted_distance = 20.0f;
                if(Math.abs(agent.x - ball.x) < accepted_distance ) {
                    String s = "<{\"1\"} --> [on]>. :|:";
                    
                    if(!lastWasMiddle || !s.equals(this.LastInput)) {
                        System.out.println("good mr_nars");
                        nar.addInput(s);
                        System.out.println(s);
                    }
                    lastWasMiddle = true;
                    this.LastInput = s;
                } else {
                    if(agent.x < ball.x) {
                        String s = "<{right} --> [on]>. :|:";
                        lastWasMiddle = false;
                        if(k%20 == 0 || !s.equals(this.LastInput)) {
                            nar.addInput(s);
                            System.out.println(s);
                        }
                        this.LastInput = s;
                    } else {
                        String s = "<{left} --> [on]>. :|:";
                        lastWasMiddle = false;
                        if(k%20 == 0 || !s.equals(this.LastInput)) {
                            nar.addInput(s);
                            System.out.println(s);
                        }
                        this.LastInput = s;
                    }
                }
                /*/if(reward < 0) {
                    System.out.println("bad mr_nars");
                    nar.addInput("(--,<SELF --> [good]>). :|:");
                }*/<commit_msg>Pong example update, less inference steps per frame necessary, more FPS, and since the control strategy works fine also for delayed rewards, there is no need to get reward whenever the ball crosses the Y-line.<commit_after>package nars.lab.microworld;

import nars.storage.Memory;
import nars.NAR;
//import nars.nal.nal8.Operation;
//import nars.nal.nal8.operator.SyncOperator;
//import nars.nar.Default;
//import nars.task.Task;
import processing.core.PApplet;
import processing.core.PImage;
import processing.event.MouseEvent;

import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import nars.entity.Concept;
import nars.entity.Sentence;
import nars.entity.Task;
import nars.gui.NARSwing;
import nars.io.Narsese;
import nars.lab.testutils.ConceptMonitor;
import nars.language.Term;
import nars.operator.Operation;
import nars.operator.Operator;

public class Pong extends Frame {

    public Pong() {
        String[] args = {"Pong"};
        MyPapplet mp = new MyPapplet ();
        mp.setSize(800,600);
        PApplet.runSketch(args, mp);
    }
    
    static MyPapplet.Obj agent;
        static MyPapplet.Obj ball;

    public class MyPapplet extends PApplet {

        float mouseScroll = 0;
        public void mouseWheel(MouseEvent event) {
            mouseScroll = -event.getCount();
            hamlib.mouseScrolled();
        }

        class Hai
        {
            Hai(){}
            public Obj obj;
            NAR nar;
            String LastInput ="";
            int nActions = 3;
            int direction = 0;
            Hai(int nactions,int nstates)
            {
                this.nActions = nactions; //for actions since we allow the same randomization phase as in QL
                nar = new NAR();
                nar.memory.addOperator(new Right("^Right"));
                nar.memory.addOperator(new Left("^Left"));
                (nar.param).noiseLevel.set(0);
                new NARSwing(nar); 
                //nar.start(0);
                Memory m = nar.memory;
               // m.conceptForgetDurations.setValue(1.0); //better for declarative reasoning tasks: 2
                //m.taskLinkForgetDurations.setValue(1.0); //better for declarative reasoning tasks: 4
                //m.termLinkForgetDurations.setValue(1.0); //better for declarative reasoning tasks: 10
                //NARide.show(nar.loop(), (i) -> {});
            }


            int lastAction=0;
            public class Right extends Operator {
                public Right(String name) {
                    super(name);
                }

                @Override
                public List<Task> execute(Operation operation, Term[] args, Memory memory) {
                    lastAction = 1;
                    memory.allowExecution = false;
                    //System.out.println("NAR decide left");
                    return null;
                }
            }
            public class Left extends Operator {
                public Left(String name) {
                    super(name);
                }

                @Override
                public List<Task> execute(Operation operation, Term[] args, Memory memory) {
                    lastAction = 2;
                    memory.allowExecution = false;
                    //System.out.println("NAR decide right");
                    return null;
                }
            }

            int k=0;
            float Alpha=0.08f;
            boolean lastWasMiddle = false;
            int UpdateSOM(float[] viewField,float reward) //input and reward
            {
                /*for(int i=0;i<viewField.length;i++) {
                    if(viewField[i]>0.1f) {
                        String s = "<{\""+String.valueOf(i)+"\"} --> [on]>. :|:";  // %"+String.valueOf(0.5f+0.5f*viewField[i])+"%";
                        if(this.LastInput != s) {
                            nar.addInput(s);
                            System.out.println(s);
                        }
                        this.LastInput = s;

                        //System.out.println("perceive "+s);
                    }
                }*/
                lastAction = 0;
                k++;
                if(k%4==0) {
                    nar.addInput("<{\"1\"} --> [on]>! :|:"); //future extension: change to self good
                    //System.out.println("food urge input");
                }
                //if(reward > 0) { //even more delayed reward (for the future)
                
                //show what the system perceived about the ballposition:
                /*Sentence left = ConceptMonitor.strongestProjectedInputEventBelief(nar, "<{left} --> [on]>");
                Sentence right = ConceptMonitor.strongestProjectedInputEventBelief(nar, "<{right} --> [on]>");
                Sentence middle = ConceptMonitor.strongestProjectedInputEventBelief(nar, "<{\"1\"} --> [on]>");
                if(left != null) {
                    System.out.print("left: ");
                    System.out.print(left.truth);
                    System.out.print(" ");
                }
                if(middle != null) {
                    System.out.print("middle: ");
                    System.out.print(middle.truth);
                    System.out.print(" ");
                }
                if(right != null) {
                    System.out.print("right: ");
                    System.out.print(right.truth);
                    System.out.print(" ");
                }
                if(left != null || middle != null || right != null) {
                    System.out.println();
                }*/
                                                   //                                         <(&/,<{right} --> [on]>,+12,(^Left,SELF),+13) =/> <{"1"} --> [on]>>
                                                   
                
                if(k%4 == 0) {
                Sentence hypo_left = ConceptMonitor.strongestPrecondition(nar, "<{\"1\"} --> [on]>",
                        "<(&/,<{left} --> [on]>,+1,(^Right,SELF),+1) =/> <{\"1\"} --> [on]>>");
                if(hypo_left != null) {
                    System.out.println("HypLeftWrong: " + hypo_left.truth);
                }
                
                Sentence hypo_right = ConceptMonitor.strongestPrecondition(nar, "<{\"1\"} --> [on]>",
                        "<(&/,<{right} --> [on]>,+12,(^Left,SELF),+13) =/> <{\"1\"} --> [on]>>");
                if(hypo_right != null) {
                    System.out.println("HypRightWrong: " + hypo_right.truth);
                }
                
                Sentence hypo_left_false = ConceptMonitor.strongestPrecondition(nar, "<{\"1\"} --> [on]>",
                        "<(&/,<{left} --> [on]>,+1,(^Left,SELF),+1) =/> <{\"1\"} --> [on]>>");
                if(hypo_left_false != null) {
                    System.out.println("HypLeft: " + hypo_left_false.truth);
                }
                
                Sentence hypo_right_false = ConceptMonitor.strongestPrecondition(nar,  "<{\"1\"} --> [on]>",
                        "<(&/,<{right} --> [on]>,+12,(^Right,SELF),+13) =/> <{\"1\"} --> [on]>>");
                if(hypo_right_false != null) {
                    System.out.println("HypRight: " + hypo_right_false.truth);
                }
                }
                
                float accepted_distance = 20.0f;
                if(Math.abs(agent.x - ball.x) < accepted_distance) {
                    String s = "<{\"1\"} --> [on]>. :|:";
                    if(agent.y < 50) {
                        if(!lastWasMiddle || !s.equals(this.LastInput)) {
                            System.out.println("good mr_nars");
                            nar.addInput(s);
                            System.out.println(s);
                        }
                        lastWasMiddle = true;
                    }
                    this.LastInput = s;
                } else {
                    if(agent.x < ball.x) {
                        String s = "<{right} --> [on]>. :|:";
                        lastWasMiddle = false;
                        if(k%20 == 0 || !s.equals(this.LastInput)) {
                            nar.addInput(s);
                            System.out.println(s);
                        }
                        this.LastInput = s;
                    } else {
                        String s = "<{left} --> [on]>. :|:";
                        lastWasMiddle = false;
                        if(k%20 == 0 || !s.equals(this.LastInput)) {
                            nar.addInput(s);
                            System.out.println(s);
                        }
                        this.LastInput = s;
                    }
                }
                /*/if(reward < 0) {
                    System.out.println("bad mr_nars");
                    nar.addInput("(--,<SELF --> [good]>). :|:");
                }*/
{'fraction_non_alphanumeric': 0.14100541027953112, 'fraction_numerical': 0.009355275022542832, 'mean_word_length': 1.197795392618281, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3090460', 'n_tokens_mistral': 5078, 'n_tokens_neox': 4653, 'n_words': 892}