/// <summary> /// Learn <paramref name="k" /> top-ranked Transformation.Text programs for a given set of input-output examples. /// </summary> /// <param name="trainingExamples"> /// The set of input-output examples as a Tuple of the input and the output. /// </param> /// <param name="additionalInputs"> /// The set of additional inputs that do not have output examples, which helps rank learnt programs. /// </param> /// <param name="k">the number of top programs</param> /// <returns>The top-k ranked programs as <see cref="TextTransformationProgram" />s</returns> public static IEnumerable <TextTransformationProgram> LearnTopK(IDictionary <string, string> trainingExamples, IEnumerable <string> additionalInputs = null, int k = 1) { if (trainingExamples == null) { throw new ArgumentNullException(nameof(trainingExamples)); } // Load Transformation.Text grammar Grammar grammar = Language.Grammar; DomainLearningLogic learningLogic = new Witnesses(grammar, // This is currently required as a workaround for a bug. ((RankingScore)Learner.Instance.ScoreFeature).Clone()); // Setup configuration of synthesis process. var engine = new SynthesisEngine(grammar, new SynthesisEngine.Config { // Strategies perform the actual logic of the synthesis process. Strategies = new[] { new DeductiveSynthesis(learningLogic) }, UseThreads = false, CacheSize = int.MaxValue }); // Convert the examples in the format expected by Microsoft.ProgramSynthesis. // Internally, Transformation.Text represents strings as ValueSubstrings to save on // allocating new strings for each substring. // Could also use InputRow.AsState() to construct the input state. Dictionary <State, object> trainExamples = trainingExamples.ToDictionary( t => State.Create(grammar.InputSymbol, new[] { ValueSubstring.Create(t.Key) }), t => (object)ValueSubstring.Create(t.Value)); var spec = new ExampleSpec(trainExamples); // Learn an entire Transformation.Text program (i.e. start at the grammar's start symbol) // for the specification consisting of the examples. // Learn the top-k programs according to the score feature used by Transformation.Text by default. // You could define your own feature on the Transformation.Text grammar to rank programs differently. var task = new LearningTask(grammar.StartSymbol, spec, k, Learner.Instance.ScoreFeature); if (additionalInputs != null) { task.AdditionalInputs = additionalInputs.Select( input => State.Create(grammar.InputSymbol, new[] { ValueSubstring.Create(input) })) .ToList(); } IEnumerable <ProgramNode> topk = engine.Learn(task).RealizedPrograms; // Return the generated programs wraped in a TextTransformationProgram object. return(topk.Select(prog => new TextTransformationProgram(prog))); }
public JObject ToJson() { JObject json = new JObject(); json["txid"] = Hash.ToString(); json["size"] = Size; json["version"] = Version; json["script"] = Script.ToHexString(); json["sender"] = Sender.ToAddress(); json["gas"] = new BigDecimal(Gas, (byte)NativeContract.GAS.Decimals).ToString(); json["net_fee"] = new BigDecimal(NetworkFee, (byte)NativeContract.GAS.Decimals).ToString(); json["attributes"] = Attributes.Select(p => p.ToJson()).ToArray(); json["witnesses"] = Witnesses.Select(p => p.ToJson()).ToArray(); return(json); }
public virtual JObject ToJson() { JObject json = new JObject(); json["txid"] = Hash.ToString(); json["size"] = Size; json["type"] = Type; json["version"] = Version; json["nonce"] = Nonce.ToString(); json["account"] = Account.ToString(); json["attributes"] = Attributes.Select(p => p.ToJson()).ToArray(); json["sys_fee"] = SystemFee.ToString(); json["scripts"] = Witnesses.Select(p => p.ToJson()).ToArray(); return(json); }
public virtual JObject ToJson() { JObject json = new JObject(); json["txid"] = Hash.ToString(); json["size"] = Size; json["type"] = Type; json["version"] = Version; json["attributes"] = Attributes.Select(p => p.ToJson()).ToArray(); json["vin"] = Inputs.Select(p => p.ToJson()).ToArray(); json["vout"] = Outputs.Select((p, i) => p.ToJson((ushort)i)).ToArray(); json["sys_fee"] = SystemFee.ToString(); json["net_fee"] = NetworkFee.ToString(); json["scripts"] = Witnesses.Select(p => p.ToJson()).ToArray(); return(json); }
public JObject ToJson() { JObject json = new JObject(); json["hash"] = Hash.ToString(); json["size"] = Size; json["version"] = Version; json["nonce"] = Nonce; json["sender"] = Sender.ToAddress(); json["sys_fee"] = new BigDecimal(SystemFee, NativeContract.GAS.Decimals).ToString(); json["net_fee"] = new BigDecimal(NetworkFee, NativeContract.GAS.Decimals).ToString(); json["valid_until_block"] = ValidUntilBlock; json["attributes"] = Attributes.Select(p => p.ToJson()).ToArray(); json["script"] = Script.ToHexString(); json["witnesses"] = Witnesses.Select(p => p.ToJson()).ToArray(); return(json); }
public JObject ToJson() { JObject json = new JObject(); json["hash"] = Hash.ToString(); json["size"] = Size; json["version"] = Version; json["nonce"] = Nonce; json["sender"] = Sender.ToAddress(); json["sys_fee"] = SystemFee.ToString(); json["net_fee"] = NetworkFee.ToString(); json["valid_until_block"] = ValidUntilBlock; json["attributes"] = Attributes.Select(p => p.ToJson()).ToArray(); json["cosigners"] = Cosigners.Select(p => p.ToJson()).ToArray(); json["script"] = Convert.ToBase64String(Script); json["witnesses"] = Witnesses.Select(p => p.ToJson()).ToArray(); return(json); }
public static SimData ScoreStates(bool ground_truth, State A, State B) { Stopwatch s = new Stopwatch(); s.Start(); ProgramNode program = Synthesizer.Learn(1, A, B); s.Stop(); Synthesizer.Engine.ClearLearningCache(); return(new SimData { A = Synthesizer.StateToString(A), B = Synthesizer.StateToString(B), GroundTruth = ground_truth, SynthesisTime = Convert.ToUInt32(s.ElapsedMilliseconds), Pattern = (program == null ? "<NULL>" : program.AcceptVisitor(new TokensCollector()).CombinedDescription()), Cost = Convert.ToSingle(program == null ? -DefaultTokens.Any.Score : -program.GetFeatureValue(Learner.Instance.ScoreFeature)), Score = Convert.ToSingle(1.0 / (program == null ? Witnesses.ScoreTransform(-DefaultTokens.Any.Score) : Witnesses.ScoreTransform(-program.GetFeatureValue(Learner.Instance.ScoreFeature)))) }); }