public override void Load() { path = Application.persistentDataPath + "\\" + filePath; // On récupère notre QTable si elle existe déjà try { BinaryFormatter formatter = new BinaryFormatter(); Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); qTable = (QTable)formatter.Deserialize(stream); stream.Close(); stream.Dispose(); // Si elle n'existe pas, on l'initialise :) } catch (Exception e) { Debug.LogError(e.Message); // Initialiser la QTable qTable = new QTable(); foreach (GameState gs in GameState.AllStates()) { foreach (MyInput input in MyInput.AllInputs()) { qTable.table.Add(new QValue(gs, input)); } } } Debug.Log("Taille de la QTable = " + qTable.table.Count); }
//-------------------------------------------------------------------// /// <summary> /// This function applies data received from Q process to in-memory cache. /// All operations are structured in a way that only data that has been subscribed is kept, /// i.e.: data from columns that were not requested is simply discarded. /// </summary> /// <param name="alias">alias of the connection producing the data</param> /// <param name="table">name of the table</param> /// <param name="data">table data</param> private static void UpdateCache(string alias, string table, QTable data) { var symIdx = data.GetColumnIndex(_symColName); var cols = data.Columns; foreach (QTable.Row row in data) { var ra = row.ToArray(); var symName = ra[symIdx].ToString(); var symId = GetSymbolId(alias, table, symName); if (WildCardMapping.ContainsKey(alias) /*&& symId != null*/) { for (var i = 0; i < cols.Length; i++) { Cache.UpdateData(alias, table, symName, cols[i], Conversions.Convert2Excel(ra[i])); AllSymbols[alias][table].Add(symName); if (symId == null) { continue; } if (!WildCardMapping[alias].ContainsColumn(table, symId, cols[i])) { continue; } foreach (var ti in WildCardMapping[alias].GetTopics(table, symId, cols[i])) { DataOut.AddOrUpdate(ti, "", (k, v) => ""); var val = string.IsNullOrEmpty(ti.History) ? Conversions.Convert2Excel(ra[i]) : Cache.GetData(alias, table, symName, cols[i], ti.History); ti.Topic.UpdateValue(val ?? ExcelEmpty.Value); } } } if (!Mapping.ContainsKey(alias) || !Mapping[alias].ContainsSymbol(table, symName)) { continue; } for (var i = 0; i < cols.Length; i++) { if (!Mapping[alias].ContainsColumn(table, symName, cols[i])) { continue; } Cache.UpdateData(alias, table, symName, cols[i], Conversions.Convert2Excel(ra[i])); foreach (var ti in Mapping[alias].GetTopics(table, symName, cols[i])) { DataOut.AddOrUpdate(ti, "", (k, v) => ""); var val = string.IsNullOrEmpty(ti.History) ? Conversions.Convert2Excel(ra[i]) : Cache.GetData(alias, table, symName, cols[i], ti.History); ti.Topic.UpdateValue(val ?? ExcelEmpty.Value); } } } }
private static QTable ConvertToQTable <T>(IEnumerable <T> dataModels, Dictionary <string, PropertyInfo> propertiesDict, MappingContext cxt) { var rowCount = dataModels.Count(); var columns = propertiesDict.Keys.ToArray(); var propertiesArray = propertiesDict.Values.ToArray(); var data = new object[propertiesDict.Count]; for (int i = 0; i < data.Length; ++i) { data[i] = Array.CreateInstance(ToQType(propertiesArray[i].PropertyType), rowCount); } int rowIndex = 0; var result = new QTable(columns, data); foreach (var dataModel in dataModels) { for (int propIndex = 0; propIndex < propertiesArray.Length; propIndex++) { var propInfo = propertiesArray[propIndex]; var dataModelValue = cxt.TypeAccessor[dataModel, propInfo.Name]; ((Array)data.GetValue(propIndex)).SetValue(ToQObject(dataModelValue, propInfo), rowIndex); } rowIndex++; } return(result); }
private async Task DoSelectAsync <T>(QTable <T> tb, bool single = false, bool singleOrDefault = false, bool first = false) where T : class { if (single) { await tb.SingleAsync(); } if (singleOrDefault) { await tb.SingleOrDefaultAsync(); } if (first) { await tb.FirstAsync(); } await tb.FirstOrDefaultAsync(); await tb.SelectAsync(); await tb.SelectAsync(10); await tb.SelectAsync(2, 10); await tb.PageAsync(2, 10); //Microsoft.Data.Sqlite Bug //https://github.com/aspnet/Microsoft.Data.Sqlite/issues/435 //await tb.ExecuteDataTableAsync(); }
public void TestQTable() { var columns = new[] { "pos", "dates" }; var data = new object[] { new[] { "d1", "d2", "d3" }, new[] { 1001, 1002, 1003 } }; var t = new QTable(columns, data); Assert.AreEqual(t, new QTable(columns, data)); Assert.IsTrue(t.HasColumn("pos")); Assert.IsFalse(t.HasColumn("unknown")); Assert.AreEqual(1, t.GetColumnIndex("dates")); Assert.Throws <NullReferenceException>(() => t.GetColumnIndex("unknown")); int i = 0; var e = t.GetEnumerator(); while (e.MoveNext()) { var r = e.Current; Assert.AreEqual(t[i].ToArray(), r.ToArray()); i++; } Assert.AreEqual(i, t.RowsCount); }
public void importQtableText(QTable qtable) { // open a reading session StreamReader readObj = new StreamReader(textFPath); string currentValue; int CurrentNumValue; while ((currentValue = readObj.ReadLine()) == (null) || (currentValue = readObj.ReadLine()) == "") { } for (int i = 0; i < 15; i++) { for (int j = 0; j < 4; j++) { // write to textFile Debug.Log(currentValue); int.TryParse(currentValue, out CurrentNumValue); // update the qtable with values from textFile qtable.qTable[i, j] = CurrentNumValue; currentValue = readObj.ReadLine(); } } // close reading session readObj.Close(); }
/// <summary> /// 获取组 /// </summary> /// <param name="joinTable"></param> /// <param name="incDisabled"></param> /// <param name="incUnVisible"></param> /// <param name="p_pgids"></param> /// <returns></returns> protected DataTable DoGetGroups(QTable joinTable, bool incDisabled, bool incUnVisible, params int[] p_pgids) { if (p_pgids == null || p_pgids.Length < 1) { return(null); } //------------------------------------- GroupM g = new GroupM(); SQuery SQ = new SQuery(); SQ.From(g.NoLock()); if (joinTable != null) { SQ.InnerJoin(joinTable.NoLock()).On(g.PGID == joinTable["PGID"]); } SQ.Where(g.P_PGID.In(p_pgids)); if (incDisabled == false) { SQ.And(g.Is_Disabled != 1); } if (incUnVisible == false) { SQ.And(g.Is_Visibled != 0); } return(SQ.OrderBy(g.Order_Index) .Select()); }
public void StoreInfo(QData data) { var qt = new QTable { Name = "a_Tes", Comment = "", Pk = "Id", PkComment = "Идентификатор" }; RootType.StoreInfo(qt, null, "", null, "a_Tes_", null, data); }
public void Join(QTable table, JoinType joinType = JoinType.Inner) { if (_jump) { _jump = false; return; } this.AddTable(table); table._joinType = joinType; }
internal void AddTable(QTable table) { _tables.Add(table); table._asName = "t" + _tables.Count; if (string.IsNullOrEmpty(table._schemaName) == false) { SetUsedSchemaName(); } }
public void FullJoin(QTable table) { if (_jump) { _jump = false; return; } this.AddTable(table); table._joinType = JoinType.Full; }
public void RightJoin(QTable table) { if (_jump) { _jump = false; return; } this.AddTable(table); table._joinType = JoinType.Right; }
public void LeftJoin(QTable table) { if (_jump) { _jump = false; return; } AddTable(table); table._joinType = JoinType.Left; }
public void InnerJoin(QTable table) { if (_jump) { _jump = false; return; } AddTable(table); table._joinType = JoinType.Inner; }
private static void AssignToPropertyFromQColumn <T>(MappingContext cxt, QTable table, T[] result, int row) { for (int col = 0; col < table.Data.Length; ++col) { Array x = (Array)table.Data.GetValue(col); var propName = cxt.ColumnsToProperties[table.Columns[col]]; cxt.TypeAccessor[result[row], propName.Name] = x.GetValue(row); } }
public void FullJoin(QTable table, QJoinCondition @on) { if (_jump) { _jump = false; return; } this.AddTable(table); table._joinType = JoinType.Full; table._joinCondition = @on; }
public void InnerJoin(QTable table, QJoinCondition @on) { if (_jump) { _jump = false; return; } AddTable(table); table._joinType = JoinType.Inner; table._joinCondition = @on; }
public void Join(QTable table, JoinType joinType, QJoinCondition @on) { if (_jump) { _jump = false; return; } AddTable(table); table._joinType = joinType; table._joinCondition = @on; }
public void setUp() { // simple qlearning example from // http://people.revoledu.com/kardi/tutorial/ReinforcementLearning/index. // html // List<String> states = Arrays.asList(new String[] { "A", "B", "C", // "D", // "E", "F" }); List<String> actions = new List<String>() { "toA", "toB", "toC", "toD", "toE", "toF" }; qt = new QTable<String, String>(actions); }
private static T[] FromQTable <T>(MappingContext cxt, QTable table) { var result = new T[table.RowsCount]; for (int row = 0; row < result.Length; ++row) { result[row] = Activator.CreateInstance <T>(); AssignToPropertyFromQColumn(cxt, table, result, row); } return(result); }
public QLearningOrientationOffsetHandler( float minOffset = -180, float maxOffset = 180, float offsetIncrements = 1, float minThrottle = -0.3f, float maxThrottle = 0.3f, float throttleIncrement = 0.01f ) { MinThrottle = minThrottle; MaxThrottle = maxThrottle; ThrottleIncrement = throttleIncrement; this.qTable = new QTable(minOffset, maxOffset, offsetIncrements, minThrottle, maxThrottle, throttleIncrement); this.isFirst = true; }
protected override string GetTableName(string tableName) { if (SqlClientFactory.NameBrackets) { QTable table = (QTable)tableName; if (!String.IsNullOrEmpty(table.Alias)) { return(FormatInBrackets(table.Name) + " " + table.Alias); } return(FormatInBrackets(table.Name)); } return(base.GetTableName(tableName)); }
public static void RenderEnvironment( QTable qtable, State state) { _canvas.Dispatcher.Invoke( () => { foreach (var field in _maze.Fields) { UpdateField(field, qtable, state); } var mw = (MainWindow)Application.Current.MainWindow; mw.UpdateLayout(); }); }
public void setUp() { // simple qlearning example from // http://people.revoledu.com/kardi/tutorial/ReinforcementLearning/index. // html // List<String> states = Arrays.asList(new String[] { "A", "B", "C", // "D", // "E", "F" }); List <String> actions = new List <String>() { "toA", "toB", "toC", "toD", "toE", "toF" }; qt = new QTable <String, String>(actions); }
private static void UpdateField(MazeField field, QTable qtable, State state) { var qCells = qtable.Cells.Where(f => f.State.Column == field.Column && f.State.Row == field.Row); var rect = _canvas.Children.OfType <Rectangle>().Single(f => f.Tag == field); rect.Fill = MapBrush(field, state); var dp = _canvas.Children.OfType <DockPanel>().Single(f => f.Tag == field); var textBlocks = dp.Children.OfType <TextBlock>(); foreach (var txb in textBlocks) { var action = (Domain.Models.Action)txb.Tag; var qValue = Math.Round(qCells.Single(f => f.Action == action).QValue, 2).ToString(CultureInfo.InvariantCulture); txb.Text = qValue; } }
void Start() { dataObj = GetComponent <DataCollect>(); lookupTable = GetComponent <QTable>(); lookupTable.qTableInit(); positions = dataObj.getPositions(); directionsTaken = dataObj.getdDirectionTaken(); distToGoal = dataObj.getDistToGoal(); canMoveLeft = dataObj.getCanMoveLeft(); canMoveRight = dataObj.getCanMoveRight(); canMoveBack = dataObj.getCanMoveBack(); canMoveForward = dataObj.getCanMoveForward(); rewardGiven = dataObj.getRewardGiven(); }
void Start() { energyScriptRef = energyBarr.GetComponent <energyScript>(); lookupTable = GetComponent <QTable>(); lookupTable.qTableInit(); tableMessenger = GetComponent <TextWriteTest>(); // keep reference to numIterations alive when scene reload variableForPrefab = Resources.Load("prefabs/MainCube", typeof(GameObject)) as GameObject; numIterationsScriptRef = variableForPrefab.GetComponent <numIterationsScript>(); // run the read function to update Qtable from local text files read(); numIterationsScriptRef.numIterations += 1; // increment numIterations completeText.text = ""; StartCoroutine(runStartText()); // if aiEnabled = false hide the aiCube and render in the playerCube if (aiEnabled == false) { cube.SetActive(false); CubeController scriptRef = GetComponent <CubeController>(); scriptRef.enabled = false; // disable the mlScript so computations dont use resources in background } if (playerEnabled == false) { //playerCube.SetActive(false); GameObject.Destroy(playerCube); //CubeController scriptRefTwo = GetComponent<playerController>(); //scriptRefTwo.enabled = false; } else { aiCam.enabled = false; } dataObj = GetComponent <DataCollect>(); positions = dataObj.getPositions(); directionsTaken = dataObj.getdDirectionTaken(); distToGoal = dataObj.getDistToGoal(); canMoveLeft = dataObj.getCanMoveLeft(); canMoveRight = dataObj.getCanMoveRight(); canMoveBack = dataObj.getCanMoveBack(); canMoveForward = dataObj.getCanMoveForward(); rewardGiven = dataObj.getRewardGiven(); }
//-------------------------------------------------------------------// private static object[,] QTable2Excel(QTable table) { var res = new object[table.RowsCount + 1, table.ColumnsCount]; for (var i = 0; i < table.ColumnsCount; i++) { for (var j = 0; j < table.RowsCount + 1; j++) { if (j == 0) //write header { res[j, i] = table.Columns.GetValue(i).ToString(); } else //write data { res[j, i] = Convert2Excel(((Array)table.Data.GetValue(i)).GetValue(j - 1)); } } } return(res); }
public void testQLearningAgent() { QLearningAgent <CellWorldPosition, String> qla = new QLearningAgent <CellWorldPosition, string>( fourByThree); Randomizer r = new MockRandomizer(new double[] { 0.1, 0.9, 0.2, 0.8, 0.3, 0.7, 0.4, 0.6, 0.5 }); // Randomizer r = new JavaRandomizer(); Dictionary <Pair <CellWorldPosition, String>, Double> q = null; QTable <CellWorldPosition, String> qTable = null; for (int i = 0; i < 100; i++) { qla.executeTrial(r); q = qla.getQ(); qTable = qla.getQTable(); } // qTable.normalize(); // System.Console.WriteLine(qTable); // System.Console.WriteLine(qTable.getPolicy()); }
static void PrintResult(QTable t, Func <string, Task> func) { var rowsToShow = Math.Min(t.RowsCount, 20); var dataBuffer = new object[1 + rowsToShow][]; var columnWidth = new int[t.ColumnsCount]; dataBuffer[0] = new string[t.ColumnsCount]; for (int j = 0; j < t.ColumnsCount; j++) { dataBuffer[0][j] = t.Columns[j]; columnWidth[j] = t.Columns[j].Length + 1; } for (int i = 1; i < rowsToShow; i++) { dataBuffer[i] = new string[t.ColumnsCount]; for (int j = 0; j < t.ColumnsCount; j++) { var value = t[i - 1][j].ToString(); dataBuffer[i][j] = value; columnWidth[j] = Math.Max(columnWidth[j], value.Length + 1); } } var formatting = ""; for (int i = 0; i < columnWidth.Length; i++) { formatting += "{" + i + ",-" + columnWidth[i] + "}"; } Console.WriteLine(formatting, dataBuffer[0]); Console.WriteLine(new string('-', columnWidth.Sum())); for (int i = 1; i < rowsToShow; i++) { Console.WriteLine(formatting, dataBuffer[i]); func(dataBuffer[i].ToString()); } }
private void DoSelect <T, OutT>(QTable <T> tb, bool single = false, bool singleOrDefault = false, bool first = false) where T : class where OutT : class { if (single) { tb.Single <OutT>(); } if (singleOrDefault) { tb.SingleOrDefault <OutT>(); } if (first) { tb.First <OutT>(); } tb.FirstOrDefault <OutT>(); tb.Select <OutT>(); tb.Select <OutT>(10); tb.Select <OutT>(2, 10); tb.Page <OutT>(2, 10); }
public void TestQDictionary() { string[] keys = new string[] { "foo", "bar", "z" }; object[] values = new object[] { 1, "val", null }; var d = new QDictionary(keys, values); Assert.IsTrue(d.Equals(new QDictionary(keys, values))); var e = d.GetEnumerator(); int i = 0; while (e.MoveNext()) { var kv = e.Current; Assert.AreEqual(keys[i], kv.Key); Assert.AreEqual(values[i], kv.Value); i++; } Assert.AreEqual(i, d.Count); var table = new QTable(new[] { "eid" }, new object[] { new[] { 1001, 1002, 1003 } }); d = new QDictionary(keys, table); Assert.IsTrue(d.Equals(new QDictionary(keys, table))); e = d.GetEnumerator(); i = 0; while (e.MoveNext()) { var kv = e.Current; Assert.AreEqual(keys[i], kv.Key); Assert.AreEqual(table[i].ToArray(), ((QTable.Row)kv.Value).ToArray()); i++; } Assert.AreEqual(i, d.Count); }
/// <summary> /// Generates a <see cref="QLearnerModel"/> based on states/actions with transitions and rewards. /// </summary> /// <param name="X1">Initial State matrix.</param> /// <param name="y">Action label vector.</param> /// <param name="X2">Transition State matrix.</param> /// <param name="r">Reward values.</param> /// <returns>QLearnerModel.</returns> public override IReinforcementModel Generate(Matrix X1, Vector y, Matrix X2, Vector r) { this.Preprocess(X1, y, X2, r); var examples = MDPConverter.GetStates(X1, y, X2, this.FeatureProperties, this.FeatureDiscretizer); var states = examples.Item1; var actions = examples.Item2; var statesP = examples.Item3; QTable Q = new QTable(); // construct Q table for (int i = 0; i < states.Count(); i++) { var state = states.ElementAt(i); var action = actions.ElementAt(i); var stateP = statesP.ElementAt(i); Q.AddOrUpdate(state, action, r[i]); if (!Q.ContainsKey(stateP)) Q.AddKey(stateP); } double count = states.Select(s => s.Id).Distinct().Count(); double change = 0; for (int pass = 0; pass < this.MaxIterations; pass++) { change = 0; for (int i = 0; i < states.Count(); i++) { IState state = states.ElementAt(i); IAction action = actions.ElementAt(i); IState stateP = statesP.ElementAt(i); double reward = r[i]; double q = (1.0 - this.LearningRate) * Q[state, action] + this.LearningRate * (reward + this.Lambda * Q[stateP, Q.GetMaxAction(stateP)]); change += (1.0 / count) * System.Math.Abs((Q[state, action] - q)); Q[state, action] = q; } if (change <= this.Epsilon) break; } return new QLearnerModel() { Descriptor = this.Descriptor, TransitionDescriptor = this.TransitionDescriptor, NormalizeFeatures = this.NormalizeFeatures, FeatureNormalizer = this.FeatureNormalizer, FeatureProperties = this.FeatureProperties, FeatureDiscretizer = this.FeatureDiscretizer, LearningRate = this.LearningRate, Lambda = this.Lambda, Q = Q }; }
public void TestQTable() { var columns = new[] { "pos", "dates" }; var data = new object[] { new[] { "d1", "d2", "d3" }, new[] { 1001, 1002, 1003 } }; var t = new QTable(columns, data); Assert.AreEqual(t, new QTable(columns, data)); Assert.IsTrue(t.HasColumn("pos")); Assert.IsFalse(t.HasColumn("unknown")); Assert.AreEqual(1, t.GetColumnIndex("dates")); Assert.Throws<NullReferenceException>(() => t.GetColumnIndex("unknown")); int i = 0; var e = t.GetEnumerator(); while (e.MoveNext()) { var r = e.Current; Assert.AreEqual(t[i].ToArray(), r.ToArray()); i++; } Assert.AreEqual(i, t.RowsCount); }
public void TestCompressedObjectDeserializationQ2() { var expressions = new QExpressions(new Dictionary<string, string> {{"q2", "..\\..\\test\\QCompressedExpressions.out"}}); var reference = new Dictionary<string, object>(); var q1000 = new string[1000]; var q200 = new object[] {new int[200], new int[200], new string[200]}; for (int i = 0; i < q1000.Length; i++) { q1000[i] = "q"; } for (int i = 0; i < 200; i++) { ((int[]) q200[0])[i] = i; ((int[]) q200[1])[i] = i + 25; ((string[]) q200[2])[i] = "a"; } reference["1000#`q"] = q1000; reference["([] q:1000#`q)"] = new QTable(new[] {"q"}, new object[] {q1000}); reference["([] a:til 200;b:25+til 200;c:200#`a)"] = new QTable(new[] {"a", "b", "c"}, q200); foreach (string expr in expressions.GetExpressions("q2")) { var stream = new MemoryStream(); var writer = new BinaryWriter(stream); var reader = new QReader(stream, Encoding.ASCII, QBasicConnection.DefaultMaxReadingChunk); byte[] binaryExpr = expressions.GetBinaryExpression("q2", expr); writer.Write((byte) 1); // little endian writer.Write((byte) 0); writer.Write((byte) 1); // compressed writer.Write((byte) 0); writer.Write(binaryExpr.Length + 8); writer.Write(binaryExpr); writer.Flush(); stream.Seek(0, SeekOrigin.Begin); try { object obj = reader.Read().Data; if (obj is QDictionary || obj is QTable || obj is QLambda) { // force usage of Equals method Assert.IsTrue(reference[expr].Equals(obj), "Deserialization failed for q expression: " + expr); } else { Assert.AreEqual(reference[expr], obj, "Deserialization failed for q expression: " + expr); } } catch (System.Exception e) { Assert.Fail("Deserialization failed for q expression: " + expr + " caused by: " + e); } finally { stream.Close(); } } }
static void PrintResult(QTable t) { var rowsToShow = Math.Min(t.RowsCount + 1, 20); var dataBuffer = new object[1 + rowsToShow][]; var columnWidth = new int[t.ColumnsCount]; dataBuffer[0] = new string[t.ColumnsCount]; for (int j = 0; j < t.ColumnsCount; j++) { dataBuffer[0][j] = t.Columns[j]; columnWidth[j] = t.Columns[j].Length + 1; } for (int i = 1; i < rowsToShow; i++) { dataBuffer[i] = new string[t.ColumnsCount]; for (int j = 0; j < t.ColumnsCount; j++) { var value = t[i - 1][j].ToString(); dataBuffer[i][j] = value; columnWidth[j] = Math.Max(columnWidth[j], value.Length + 1); } } var formatting = ""; for (int i = 0; i < columnWidth.Length; i++) { formatting += "{" + i + ",-" + columnWidth[i] + "}"; } Console.WriteLine(formatting, dataBuffer[0]); Console.WriteLine(new string('-', columnWidth.Sum())); for (int i = 1; i < rowsToShow; i++) { Console.WriteLine(formatting, dataBuffer[i]); } }