Skip to content

Commit

Permalink
Merge pull request #152 from RoboTutorLLC/2023-07-13-implement-MAB
Browse files Browse the repository at this point in the history
Merge 2023 07 13 implement mab
  • Loading branch information
j50ju authored Sep 26, 2024
2 parents a9c030d + 029afca commit 52c263f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 108 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public enum MenuType {STUDENT_CHOICE, CYCLE_CONTENT};

final static private String TAG = "CTutorEngine";

static public HashMap<String, String> translationTable = new HashMap<>();


/**
* TutorEngine is a Singleton
Expand Down Expand Up @@ -815,32 +817,43 @@ private static TransitionMatrixModel loadTransitionMatrixModel() {
//String dataFile = "dev_data.json";
String dataFile = RoboTutor.MATRIX_FILE;

// simpler way to refer to languge
// simpler way to refer to language
String lang = TCONST.langMap.get(CTutorEngine.language);

String dataPath = TCONST.TUTORROOT + "/" + tutorName + "/" + TCONST.TASSETS;
dataPath += "/" + TCONST.DATA_PATH + "/" + lang + "/";

String jsonData = JSON_Helper.cacheData(dataPath + dataFile);

//
// Load the datasource into a separate class...
TransitionMatrixModel matrix = new TransitionMatrixModel(dataPath + dataFile, mRootScope);
matrix.validateAll();
System.out.println("dataPath: " + dataPath);
System.out.println("Log pointer");
System.out.println("Log pointer"); //to locate required data in log

loadTranslationTable(Activity.getApplicationContext());
System.out.println("translationTable : " + translationTable);

String oldActivity = "write.missingLtr:lc.begin.ka.2";
String nextActivity = getTranslatedActivityID(oldActivity);

HashMap<String, String> h2 = loadTranslationTable(Activity.getApplicationContext());
System.out.println("h2 : " + h2);
System.out.println("Translated activity: " + nextActivity);
return matrix;
}



private static void getArm() {

Log.d("MAB", "Entering getArm() method.");

String tutorName = "activity_selector";
String dataPath = TCONST.TUTORROOT + "/" + tutorName;
String dataFile = RoboTutor.ARM_WEIGHTS_FILE;

MABHandler.getArm(dataPath + "/" + dataFile, mRootScope);

Log.d("MAB", "Exiting getArm() method.");
}


Expand All @@ -860,8 +873,13 @@ public void loadJSON(JSONObject jsonObj, IScope scope) {
loadJSON(jsonObj, (IScope2) scope);

}
public static HashMap<String, String> loadTranslationTable(Context context) {
HashMap<String, String> hm = new HashMap<>();

public static String getTranslatedActivityID(String oldActivityID){
return translationTable.get(oldActivityID);
}


static void loadTranslationTable(Context context) {

try {
//csv file containing data
Expand All @@ -871,21 +889,37 @@ public static HashMap<String, String> loadTranslationTable(Context context) {
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String strLine;
StringTokenizer st;
int lineNumber = 0, tokenNumber = 0;
int lineNumber = 0;

//Sample of csv file
//unfiltered_activity_name,unfiltered_row,unfiltered_column,filtered_activity_name,filtered_row,filtered_column
//write.ltr.uc.dic:vow.asc.A..Z.3,0,49,akira:vow.ltr.uc:A..Z.vow.5.asc.say.11,0,50

// Skip the first line
br.readLine();

//read comma-separated file line by line
// read comma-separated file line by line
while ((strLine = br.readLine()) != null) {
lineNumber++;

//break comma-separated line using ","
// break comma-separated line using ","
st = new StringTokenizer(strLine, ",");

while (st.hasMoreTokens()) {
tokenNumber++;
String a = st.nextToken();
tokenNumber+=2;
String b = st.nextToken();
hm.put(a, b);
// Make sure there are at least 4 tokens before accessing 0th and 3rd indices
if (st.countTokens() >= 4) {
// Skip the first three tokens
String currentActivity = st.nextToken(); // 1st token
st.nextToken(); // Skipping 2nd token
st.nextToken(); // Skipping 3rd token

String nextActivity = st.nextToken(); // 4th token
// Skip the next two tokens
st.nextToken(); // Skipping 5th token
st.nextToken(); // Skipping 6th token

translationTable.put(currentActivity, nextActivity);
} else {
System.err.println("Invalid format in line " + lineNumber + ": " + strLine);
}
}
} catch (FileNotFoundException e) {
Expand All @@ -894,10 +928,9 @@ public static HashMap<String, String> loadTranslationTable(Context context) {
e.printStackTrace();
}

return hm;
System.out.println("translationTable : " + translationTable);
}


}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import cmu.xprize.robotutor.tutorengine.graph.vars.IScope2;

public class ArmWeight {
public class Arm {

public String name;
public Float weight;
public String matrix;

public ArmWeight(JSONObject jsonObject, IScope2 scope) {
public Arm(JSONObject jsonObject, IScope2 scope) {
try {
loadJSON(jsonObject);
} catch (Exception e) {
Expand All @@ -27,7 +27,7 @@ public void loadJSON(JSONObject jsonObj) throws JSONException {

@Override
public String toString() {
return "ArmWeight{" +
return "Arm{" +
"name='" + name + '\'' +
", weight=" + weight +
", matrix='" + matrix + '\'' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import cmu.xprize.comp_ask.CAsk_Data;
import cmu.xprize.comp_session.AS_CONST;
import cmu.xprize.robotutor.RoboTutor;
import cmu.xprize.robotutor.tutorengine.CTutorEngine;
import cmu.xprize.util.CAt_Data;
import cmu.xprize.util.CPlacementTest_Tutor;

Expand Down Expand Up @@ -274,10 +275,15 @@ public CAt_Data getTutorToLaunch(String buttonBehavior) {
Log.wtf(MENU_BUG_TAG, chosenTutorId + " " + activeSkill);

// This is just to make sure we go somewhere if there is a bad link - which
// there shuoldn't be :)
// there shouldn't be :)
//
if (tutorToLaunch == null) {
tutorToLaunch = (CAt_Data) transitionMap.get(rootTutor);
String nextTutorId = CTutorEngine.getTranslatedActivityID(chosenTutorId);
tutorToLaunch = (CAt_Data) transitionMap.get(nextTutorId);

if (tutorToLaunch == null) {
tutorToLaunch = (CAt_Data) transitionMap.get(rootTutor);
}
}
return tutorToLaunch;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public class MABHandler {
private static final String TAG = "MABHandler";

public static String getArm(String dataSource, IScope2 scope) {
List<ArmWeight> armWeights = getArmWeights(dataSource, scope);
ArmWeight selectedArm = selectArm(armWeights);
Log.d(TAG, "getArm: list = "+armWeights);
Log.d(TAG, "getArm: selected = "+selectedArm);
List<Arm> arms = getarms(dataSource, scope);
Arm selectedArm = selectArm(arms);
Log.d(TAG, "getArm: list = " + arms);
Log.d(TAG, "getArm: selected = " + selectedArm);
return "";
}

// Selects an arm from a list of arms
private static ArmWeight selectArm(List<ArmWeight> armWeights) {
private static Arm selectArm(List<Arm> arms) {
float sum = 0;
for (ArmWeight arm : armWeights) {
for (Arm arm : arms) {
sum += arm.weight;
}

Expand All @@ -50,7 +50,7 @@ private static ArmWeight selectArm(List<ArmWeight> armWeights) {

// find out where p lies
float bottom = 0;
for (ArmWeight arm : armWeights) {
for (Arm arm : arms) {
float top = bottom + arm.weight;
if (bottom <= p && p <= top) {
return arm;
Expand All @@ -66,27 +66,34 @@ private static float getRandom(float min, float max) {
}


private static List<ArmWeight> getArmWeights(String dataSource, IScope2 scope) {
private static List<Arm> getarms(String dataSource, IScope2 scope) {
String jsonData = JSON_Helper.cacheData(dataSource);
List<ArmWeight> armWeights = new ArrayList<>();
List<Arm> arms = new ArrayList<>();
try {
JSONObject rootObject = new JSONObject(jsonData);
JSONArray rootArray = rootObject.getJSONArray(KEY_ARRAY);
armWeights = parseArray(rootArray, scope);
arms = parseArray(rootArray, scope);

// Adding logging to print arms
for (Arm arm : arms) {
Log.d(TAG, "Arm: " + arm.name + ", Weight: " + arm.weight + ", Matrix Path" + arm.matrix);
}

} catch (Exception e) {
Log.d(TAG, "getArmWeights: "+e);
Log.e(TAG, "Error in getarms: " + e.getMessage());
}
return armWeights;
}
return arms;
}


private static List<ArmWeight> parseArray(JSONArray array, IScope2 scope) throws JSONException {
List<ArmWeight> armWeights = new ArrayList<>();
private static List<Arm> parseArray(JSONArray array, IScope2 scope) throws JSONException {
List<Arm> arms = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
JSONObject armWeightJSON = array.getJSONObject(i);
ArmWeight armWeight = new ArmWeight(armWeightJSON, scope);
armWeights.add(armWeight);
JSONObject armJSON = array.getJSONObject(i);
Arm arm = new Arm(armJSON, scope);
arms.add(arm);
}
return armWeights;
return arms;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cmu.xprize.comp_ask.CAsk_Data;
import cmu.xprize.comp_session.AS_CONST;
import cmu.xprize.robotutor.RoboTutor;
import cmu.xprize.robotutor.tutorengine.CTutorEngine;
import cmu.xprize.util.CAt_Data;

import static cmu.xprize.comp_session.AS_CONST.BEHAVIOR_KEYS.SELECT_MATH;
Expand Down Expand Up @@ -156,14 +157,20 @@ public CAt_Data getTutorToLaunch(String buttonBehavior) {
CAt_Data tutorToLaunch = (CAt_Data) transitionMap.get(activeTutorId);

// This is just to make sure we go somewhere if there is a bad link - which
// there shuoldn't be :)
// there shouldn't be :)
//
if (tutorToLaunch == null) {
tutorToLaunch = (CAt_Data) transitionMap.get(rootTutor);
String nextTutorId = CTutorEngine.getTranslatedActivityID(activeTutorId);
tutorToLaunch = (CAt_Data) transitionMap.get(nextTutorId);

if (tutorToLaunch == null) {
tutorToLaunch = (CAt_Data) transitionMap.get(rootTutor);
}
}
return tutorToLaunch;
}


@Override
public String getDebugMenuSkill() {
return _student.getActiveSkill();
Expand Down

0 comments on commit 52c263f

Please sign in to comment.