public async Task TestRandomEntityTypesAsync <T>(string entitySetName) { // clear respository await this.ClearRepositoryAsync(entitySetName); // TODO: Get ride of random generator in test codes. It's bad idea to introduce random factors in functional test var rand = new Random(RandomSeedGenerator.GetRandomSeed()); T entityBaseline = await PostNewEntityAsync <T>(entitySetName, rand); T entityBeforeUpdate = await ReadFirstEntityAsync <T>(entitySetName); AssertExtension.PrimitiveEqual(entityBaseline, entityBeforeUpdate); DataServiceResponse responseUpdate = await UpdateEntityAsync <T>(entitySetName, entityBeforeUpdate, rand); T entityAfterUpdate = await ReadFirstEntityAsync <T>(entitySetName); AssertExtension.PrimitiveEqual(entityBeforeUpdate, entityAfterUpdate); DataServiceResponse responseDelete = await DeleteEntityAsync <T>(entitySetName, entityAfterUpdate); T[] entities = await ReadAllEntitiesAsync <T>(entitySetName); Assert.Empty(entities); }
public virtual void CreateAndDeleteLinkToDerivedNavigationPropertyOnBaseEntitySet() { // clear respository this.ClearRepository("InheritanceTests_Vehicles"); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var car = InstanceCreator.CreateInstanceOf <Car>(r); var vehicle = InstanceCreator.CreateInstanceOf <MiniSportBike>(r, new CreatorSettings() { NullValueProbability = 0.0 }); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); ctx.AddObject("InheritanceTests_Vehicles", car); ctx.AddObject("InheritanceTests_Vehicles", vehicle); ctx.SaveChangesAsync().Wait(); ctx.SetLink(car, "SingleNavigationProperty", vehicle); ctx.SaveChangesAsync().Wait(); ctx = ReaderClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); var cars = ctx.CreateQuery <Vehicle>("InheritanceTests_Vehicles").ExecuteAsync().Result.ToList().OfType <Car>(); var actual = cars.First(); ctx.LoadPropertyAsync(actual, "SingleNavigationProperty").Wait(); AssertExtension.PrimitiveEqual(vehicle, actual.SingleNavigationProperty); this.ClearRepository("InheritanceTests_Vehicles"); }
public async Task ShouldSupportDerivedComplexTypeAsync() { var settings = new CreatorSettings() { NullValueProbability = 0.0 }; var uri = new Uri(this.BaseAddress); var entitySetName = "ComplexTypeTests_Entity"; // clear respository await this.ClearRepositoryAsync("ComplexTypeTests_Entity"); var rand = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var baseline = InstanceCreator.CreateInstanceOf <ComplexTypeTests_Entity>(rand, settings); await PostNewEntityAsync(uri, entitySetName, baseline); int id = baseline.ID; var actual = (await GetEntitiesAsync(uri, entitySetName)).Where(t => t.ID == id).First(); AssertExtension.DeepEqual(baseline, actual); await UpdateEntityAsync(uri, entitySetName, actual, data => { data.ComplexType = InstanceCreator.CreateInstanceOf <ComplexTypeTests_ComplexType>(rand, settings); }); var afterUpdate = (await GetEntitiesAsync(uri, entitySetName)).Where(t => t.ID == id).First(); AssertExtension.DeepEqual(actual, afterUpdate); }
public async Task SupportPostCollectionPropertyByEntityPayload() { var settings = new CreatorSettings() { NullValueProbability = 0.0 }; var uri = new Uri(this.BaseAddress); var entitySetName = "CollectionProperty_Entity"; // clear respository this.ClearRepository("CollectionProperty_Entity"); var rand = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var baseline = InstanceCreator.CreateInstanceOf <CollectionProperty_Entity>(rand, settings); await PostNewEntityAsync(uri, entitySetName, baseline); int id = baseline.ID; var actual = (await GetEntitiesAsync(uri, entitySetName)).Where(t => t.ID == id).First(); AssertExtension.DeepEqual(baseline, actual); await UpdateEntityAsync(uri, entitySetName, actual, data => { data.StringList = InstanceCreator.CreateInstanceOf <List <string> >(rand, settings); data.ComplexTypeCollection = InstanceCreator.CreateInstanceOf <Collection <CollectionProperty_ComplexType> >(rand, settings); }); var afterUpdate = (await GetEntitiesAsync(uri, entitySetName)).Where(t => t.ID == id).First(); AssertExtension.DeepEqual(actual, afterUpdate); }
public static T CreateInstances <T>() { var results = InstanceCreator.CreateInstanceOf <T>(new Random(RandomSeedGenerator.GetRandomSeed()), new CreatorSettings { NullValueProbability = 0, AllowEmptyCollection = false }); return(results); }
public void RunQueryableOnAllPossibleTypes(Type type, string queryString) { int seed = RandomSeedGenerator.GetRandomSeed(); Random r = new Random(seed); Type generic = typeof(IEnumerable <>); var collectionType = generic.MakeGenericType(type); Type listType = typeof(List <>).MakeGenericType(type); var array = Activator.CreateInstance(listType); EnableQueryAttribute q = new EnableQueryAttribute(); var configuration = new HttpConfiguration(); configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; configuration.Count().Filter().OrderBy().Expand().MaxTop(null).Select(); configuration.Routes.MapHttpRoute("ApiDefault", "api/{controller}/{id}", new { id = RouteParameter.Optional }); configuration.EnableDependencyInjection(); var request = new HttpRequestMessage(HttpMethod.Get, "http://test/api/Objects?" + queryString); request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, configuration); var controllerContext = new HttpControllerContext( configuration, configuration.Routes.GetRouteData(request), request); var actionContext = new HttpActionContext(controllerContext, new ReflectedHttpActionDescriptor() { Configuration = configuration }); var context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); context.Response.Content = new ObjectContent(collectionType, array, new JsonMediaTypeFormatter()); try { q.OnActionExecuted(context); Console.WriteLine(context.Response.Content.ReadAsStringAsync().Result); Assert.Equal(HttpStatusCode.OK, context.Response.StatusCode); } catch (ArgumentException ae) { // For example: // The type 'System.DateTime' of property 'NotAfter' in the // 'System.Security.Cryptography.X509Certificates.X509Certificate2' type is not a supported type. // Change to use 'System.DateTimeOffset' or ignore this type by calling // Ignore<System.Security.Cryptography.X509Certificates.X509Certificate2>() // on 'System.Web.OData.Builder.ODataModelBuilder'. Assert.True(ae.Message.Contains("The type 'System.DateTime' of property") || ae.Message.Contains("System.Windows.Forms.AxHost") || ae.Message.Contains("Found more than one dynamic property container in type"), "The exception should contains \"The type 'System.DateTime' of property\", or " + "\"System.Windows.Forms.AxHost\" or" + "\"Found more than one dynamic property container in type\", but actually, it is:" + ae.Message); } }
public void Should_Generate_Seed_With_Length_Equals_81() { // Arrange var generator = new RandomSeedGenerator(); // Act var seed = generator.CreateNewSeed(); // Verify Assert.Equal(81, seed.Length); }
public async Task PostGetUpdateAndDelete(Type entityType, string entitySetNam) { var testMethod = this.GetType().GetMethods() .Where(method => method.IsGenericMethod) .Where(method => method.Name == "PostGetUpdateAndDelete") .FirstOrDefault(); var concreteTestMethod = testMethod.MakeGenericMethod(entityType); var rand = new Random(RandomSeedGenerator.GetRandomSeed()); await(Task) concreteTestMethod.Invoke(this, new object[] { entitySetNam, rand }); }
public void Should_Return_UpperCase_English_Letters_And_9() { // Arrange var generator = new RandomSeedGenerator(); var regex = new Regex("[A-Z9]"); // Act var seed = generator.CreateNewSeed(); // Verify Assert.Matches(regex, seed); }
public virtual void PostGetUpdateAndDelete(Type entityType, string entitySetName) { // clear respository this.ClearRepository(entitySetName); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var value = InstanceCreator.CreateInstanceOf(entityType, r, new CreatorSettings() { NullValueProbability = 0.0 }); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AddObject(entitySetName, value); ctx.SaveChanges(); // get collection of entities from repository ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); var entities = ctx.CreateQuery <Vehicle>(entitySetName); var beforeUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(value, beforeUpdate); // update entity and verify if it's saved ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, beforeUpdate); beforeUpdate.Name = InstanceCreator.CreateInstanceOf <string>(r); ctx.UpdateObject(beforeUpdate); ctx.SaveChanges(); // retrieve the updated entity ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <Vehicle>(entitySetName); var afterUpdate = entities.Where(e => e.Id == beforeUpdate.Id).First(); Assert.Equal(beforeUpdate.Name, afterUpdate.Name); // delete entity ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, afterUpdate); ctx.DeleteObject(afterUpdate); ctx.SaveChanges(); // ensure that the entity has been deleted ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <Vehicle>(entitySetName); Assert.Equal(0, entities.ToList().Count()); // clear repository this.ClearRepository(entitySetName); }
protected override void Run(CancellationToken cancellationToken) { var problemData = Problem.ProblemData; // set up and initialize everything if necessary var wdist = DistanceFunction as WeightedEuclideanDistance; if (wdist != null) { wdist.Initialize(problemData); } if (state == null) { if (SetSeedRandomly) { Seed = RandomSeedGenerator.GetSeed(); } var random = new MersenneTwister((uint)Seed); var dataset = problemData.Dataset; var allowedInputVariables = problemData.AllowedInputVariables.ToArray(); var allindices = Problem.ProblemData.AllIndices.ToArray(); // jagged array is required to meet the static method declarations of TSNEStatic<T> var data = Enumerable.Range(0, dataset.Rows).Select(x => new double[allowedInputVariables.Length]).ToArray(); var col = 0; foreach (var s in allowedInputVariables) { var row = 0; foreach (var d in dataset.GetDoubleValues(s)) { data[row][col] = d; row++; } col++; } if (Normalization) { data = NormalizeInputData(data); } state = TSNEStatic <double[]> .CreateState(data, DistanceFunction, random, NewDimensions, Perplexity, Theta, StopLyingIteration, MomentumSwitchIteration, InitialMomentum, FinalMomentum, Eta, RandomInitialization); SetUpResults(allindices); } while (state.iter < MaxIterations && !cancellationToken.IsCancellationRequested) { if (state.iter % UpdateInterval == 0) { Analyze(state); } TSNEStatic <double[]> .Iterate(state); } Analyze(state); }
public void TestRandomEntityTypes(Type entityType, string entitySetName) { this.BaseAddress = this.BaseAddress.Replace("localhost", Environment.MachineName); //var entitySetName = entityType.Name; // clear respository this.ClearRepository(entitySetName); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var value = Creator.GenerateClientRandomData(entityType, r); var ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AddObject(entitySetName, value); ctx.SaveChanges(); // get collection of entities from repository ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); IEnumerable <object> entities = ctx.CreateQuery(entityType, entitySetName); var beforeUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(value, beforeUpdate); // update entity and verify if it's saved ctx = WriterClient(new Uri(BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, beforeUpdate); var updatedProperty = UpdateNonIDProperty(beforeUpdate, r); ctx.UpdateObject(beforeUpdate); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); entities = ctx.CreateQuery(entityType, entitySetName); var afterUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(beforeUpdate, afterUpdate); //var afterUpdate = entities.Where(FilterByPk(entityType, GetIDValue(beforeUpdate))).First(); // delete entity ctx = WriterClient(new Uri(BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, afterUpdate); ctx.DeleteObject(afterUpdate); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); entities = ctx.CreateQuery(entityType, entitySetName); Assert.Equal(0, entities.ToList().Count()); // clear repository this.ClearRepository(entitySetName); }
protected override void Initialize(CancellationToken cancellationToken) { base.Initialize(cancellationToken); var random = new MersenneTwister(); if (SetSeedRandomly) { Seed = RandomSeedGenerator.GetSeed(); } random.Reset(Seed); stateScope = InitializeScope(random, Problem.ProblemData, Pruning, MinimalNodeSize, LeafModel, Splitter, GenerateRules, UseHoldout, HoldoutSize); stateScope.Variables.Add(new Variable("Algorithm", this)); Results.AddOrUpdateResult("StateScope", stateScope); }
public async Task ODataCRUDShouldWork() { var rand = new Random(RandomSeedGenerator.GetRandomSeed()); var entitySetName = "MixScenarioTests_OData"; var uri = new Uri(this.BaseAddress + "/odata"); this.ClearRepository(entitySetName); // post new entity to repository var baseline = InstanceCreator.CreateInstanceOf <Vehicle>(rand); await PostNewEntityAsync(uri, baseline, entitySetName); // get collection of entities from repository var entities = await GetEntitiesAsync(uri, entitySetName); var firstVersion = entities.ToList().FirstOrDefault(); Assert.NotNull(firstVersion); AssertExtension.PrimitiveEqual(baseline, firstVersion); // update entity and verify if it's saved await UpdateEntityAsync( uri, firstVersion, data => { data.Model = InstanceCreator.CreateInstanceOf <string>(rand); data.Name = InstanceCreator.CreateInstanceOf <string>(rand); data.WheelCount = InstanceCreator.CreateInstanceOf <int>(rand); return(data); }, entitySetName); var entitiesAgain = await GetEntitiesAsync(uri, entitySetName); var secondVersion = entitiesAgain.ToList().FirstOrDefault(); Assert.NotNull(secondVersion); // firstVersion is updated in UpdatedEntityAsync AssertExtension.PrimitiveEqual(firstVersion, secondVersion); // delete entity await DeleteEntityAsync(uri, secondVersion, entitySetName); var entitiesFinal = await GetEntitiesAsync(uri, entitySetName); Assert.Equal(0, entitiesFinal.ToList().Count()); }
public static IEnumerable <Product> CreateRandomProducts() { int seed = RandomSeedGenerator.GetRandomSeed(); var r = new Random(seed); var products = new List <Product>(); for (int i = 0; i < r.Next(5000); i++) { products.Add(new Product { ID = r.Next(1000), Name = InstanceCreator.CreateInstanceOf <string>(r), Price = InstanceCreator.CreateInstanceOf <Decimal>(r), Amount = InstanceCreator.CreateInstanceOf <double>(r), Rating = r.Next(5), ReleaseDate = InstanceCreator.CreateInstanceOf <DateTime?>(r), Taxable = InstanceCreator.CreateInstanceOf <bool?>(r) }); if (r.NextDouble() > .7) { products.Last().Supplier = new Supplier { ID = r.Next(1000), Name = InstanceCreator.CreateInstanceOf <string>(r), Address = new Address { City = InstanceCreator.CreateInstanceOf <string>(r), State = InstanceCreator.CreateInstanceOf <string>(r) } }; products.Last().Supplier.Products.Add(products.Last()); } else if (r.NextDouble() > .3) { products.Last().Supplier = new Supplier { ID = r.Next(1000), Name = InstanceCreator.CreateInstanceOf <string>(r), Address = null }; products.Last().Supplier.Products.Add(products.Last()); } } return(products); }
public void Test() { Random rndGen = new Random(RandomSeedGenerator.GetRandomSeed()); CreateTypes(20, rndGen); foreach (var type in this.entityTypes) { Console.WriteLine(type.FullName); foreach (var p in type.GetProperties()) { Console.WriteLine(" {0} {1}", p.PropertyType, p.Name); } Activator.CreateInstance(type); } }
public void Random1() { IRandomSeedGenerator seeder = new RandomSeedGenerator(521288629, 362436069); IRandom random = seeder.Random; var sb = new StringBuilder(); for (int i = 0; i < 10; i++) { sb.Append(random.ValueInRange(0, 10)).Append(" "); random = random.Next(); } string actual = sb.ToString(); Assert.AreEqual("4 0 1 6 5 0 9 8 2 7 ", actual); }
protected override void Initialize(CancellationToken cancellationToken) { if (SetSeedRandomly) { Seed = RandomSeedGenerator.GetSeed(); } random.Reset(Seed); gauss = new NormalDistributedRandom(random, 0, 1); InitResults(); InitStrategy(); InitSolutions(); Analyze(); ResultsIterations = 1; }
public void ODataCRUDShouldWork() { Random r = new Random(RandomSeedGenerator.GetRandomSeed()); var entitySetName = "MixScenarioTests_OData"; var uri = new Uri(this.BaseAddress + "/odata"); this.ClearRepository(entitySetName); // post new entity to repository var value = InstanceCreator.CreateInstanceOf <Vehicle>(r); var ctx = WriterClient(uri, DataServiceProtocolVersion.V3); ctx.AddObject(entitySetName, value); ctx.SaveChanges(); // get collection of entities from repository ctx = ReaderClient(uri, DataServiceProtocolVersion.V3); IEnumerable <Vehicle> entities = ctx.CreateQuery <Vehicle>(entitySetName); var beforeUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(value, beforeUpdate); // update entity and verify if it's saved ctx = WriterClient(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, beforeUpdate); beforeUpdate.Model = InstanceCreator.CreateInstanceOf <string>(r); beforeUpdate.Name = InstanceCreator.CreateInstanceOf <string>(r); beforeUpdate.WheelCount = InstanceCreator.CreateInstanceOf <int>(r); ctx.UpdateObject(beforeUpdate); ctx.SaveChanges(); ctx = ReaderClient(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <Vehicle>(entitySetName); var afterUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(beforeUpdate, afterUpdate); //var afterUpdate = entities.Where(FilterByPk(entityType, GetIDValue(beforeUpdate))).First(); // delete entity ctx = WriterClient(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, afterUpdate); ctx.DeleteObject(afterUpdate); ctx.SaveChanges(); ctx = ReaderClient(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <Vehicle>(entitySetName); Assert.Equal(0, entities.ToList().Count()); }
public void CRUDEntitySetShouldWork() { Random r = new Random(RandomSeedGenerator.GetRandomSeed()); var entitySetName = "UnicodeRouteTests_Todoü"; var uri = new Uri(this.BaseAddress + "/odataü"); // post new entity to repository var value = InstanceCreator.CreateInstanceOf <UnicodeRouteTests_Todoü>(r); var ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); ctx.AddObject(entitySetName, value); ctx.SaveChanges(); // get collection of entities from repository ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); IEnumerable <UnicodeRouteTests_Todoü> entities = ctx.CreateQuery <UnicodeRouteTests_Todoü>(entitySetName); var beforeUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(value, beforeUpdate); // update entity and verify if it's saved ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, beforeUpdate); beforeUpdate.Nameü = InstanceCreator.CreateInstanceOf <string>(r); ctx.UpdateObject(beforeUpdate); ctx.SaveChanges(); ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <UnicodeRouteTests_Todoü>(entitySetName); var afterUpdate = entities.ToList().First(); AssertExtension.PrimitiveEqual(beforeUpdate, afterUpdate); //var afterUpdate = entities.Where(FilterByPk(entityType, GetIDValue(beforeUpdate))).First(); var response = ctx.LoadProperty(afterUpdate, "Nameü"); Assert.Equal(200, response.StatusCode); // delete entity ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); ctx.AttachTo(entitySetName, afterUpdate); ctx.DeleteObject(afterUpdate); ctx.SaveChanges(); ctx = new DataServiceContext(uri, DataServiceProtocolVersion.V3); entities = ctx.CreateQuery <UnicodeRouteTests_Todoü>(entitySetName); Assert.Equal(0, entities.ToList().Count()); }
protected override void Run(CancellationToken cancellationToken) { IRegressionSolution bestSolution = null; if (InitializeParametersRandomly) { var qualityTable = new DataTable("RMSE table"); qualityTable.VisualProperties.YAxisLogScale = true; var trainRMSERow = new DataRow("RMSE (train)"); trainRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points; var testRMSERow = new DataRow("RMSE test"); testRMSERow.VisualProperties.ChartType = DataRowVisualProperties.DataRowChartType.Points; qualityTable.Rows.Add(trainRMSERow); qualityTable.Rows.Add(testRMSERow); Results.Add(new Result(qualityTable.Name, qualityTable.Name + " for all restarts", qualityTable)); if (SetSeedRandomly) { Seed = RandomSeedGenerator.GetSeed(); } var rand = new MersenneTwister((uint)Seed); bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, ApplyLinearScaling, rand); trainRMSERow.Values.Add(bestSolution.TrainingRootMeanSquaredError); testRMSERow.Values.Add(bestSolution.TestRootMeanSquaredError); for (int r = 0; r < Restarts; r++) { var solution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, ApplyLinearScaling, rand); trainRMSERow.Values.Add(solution.TrainingRootMeanSquaredError); testRMSERow.Values.Add(solution.TestRootMeanSquaredError); if (solution.TrainingRootMeanSquaredError < bestSolution.TrainingRootMeanSquaredError) { bestSolution = solution; } } } else { bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, ApplyLinearScaling); } Results.Add(new Result(RegressionSolutionResultName, "The nonlinear regression solution.", bestSolution)); Results.Add(new Result("Root mean square error (train)", "The root of the mean of squared errors of the regression solution on the training set.", new DoubleValue(bestSolution.TrainingRootMeanSquaredError))); Results.Add(new Result("Root mean square error (test)", "The root of the mean of squared errors of the regression solution on the test set.", new DoubleValue(bestSolution.TestRootMeanSquaredError))); }
public void SupportPostCollectionPropertyByEntityPayload() { CreatorSettings settings = new CreatorSettings() { NullValueProbability = 0.0 }; // clear respository this.ClearRepository("CollectionProperty_Entity"); //this.Client.GetStringAsync(this.BaseAddress + "/$metadata").Wait(); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository var expected = InstanceCreator.CreateInstanceOf <CollectionProperty_Entity>(r, settings); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AddObject("CollectionProperty_Entity", expected); ctx.SaveChanges(); int id = expected.ID; ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); var actual = ctx.CreateQuery <CollectionProperty_Entity>("CollectionProperty_Entity").Where(t => t.ID == id).First(); AssertExtension.DeepEqual(expected, actual); expected = actual; ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.AttachTo("CollectionProperty_Entity", expected); expected.StringList = InstanceCreator.CreateInstanceOf <List <string> >(r, settings); expected.ComplexTypeCollection = InstanceCreator.CreateInstanceOf <Collection <CollectionProperty_ComplexType> >(r, settings); ctx.UpdateObject(expected); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); actual = ctx.CreateQuery <CollectionProperty_Entity>("CollectionProperty_Entity").Where(t => t.ID == id).First(); AssertExtension.DeepEqual(expected, actual); // clear respository this.ClearRepository("CollectionProperty_Entity"); }
public async Task CRUDEntitySetShouldWork() { var rand = new Random(RandomSeedGenerator.GetRandomSeed()); var entitySetName = "UnicodeRouteTests_Todoü"; var uri = new Uri(this.BaseAddress + "/odataü"); var context = new DataServiceContext(uri, ODataProtocolVersion.V4); // post new entity to repository CreatorSettings creatorSettings = new CreatorSettings() { NullValueProbability = 0, }; var baseline = InstanceCreator.CreateInstanceOf <UnicodeRouteTests_Todoü>(rand, creatorSettings); await PostNewEntityAsync(uri, entitySetName, baseline); // get collection of entities from repository var firstVersion = await GetFirstEntityAsync(uri, entitySetName); Assert.NotNull(firstVersion); AssertExtension.PrimitiveEqual(baseline, firstVersion); // update entity and verify if it's saved await UpdateEntityAsync(uri, entitySetName, firstVersion, data => { data.Nameü = InstanceCreator.CreateInstanceOf <string>(rand); }); var secondVersion = await GetFirstEntityAsync(uri, entitySetName); Assert.NotNull(secondVersion); AssertExtension.PrimitiveEqual(firstVersion, secondVersion); var response = await LoadPropertyAsync(uri, entitySetName, secondVersion, "Nameü"); Assert.Equal(200, response.StatusCode); // delete entity await DeleteEntityAsync(uri, entitySetName, secondVersion); var entities = await GetEntitiesAsync(uri, entitySetName); Assert.Empty(entities.ToList()); }
public virtual void AddAndRemoveBaseNavigationPropertyInDerivedType() { // clear respository this.ClearRepository("InheritanceTests_Cars"); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); // post new entity to repository CreatorSettings creatorSettings = new CreatorSettings() { NullValueProbability = 0, }; var car = InstanceCreator.CreateInstanceOf <Car>(r, creatorSettings); var vehicle = InstanceCreator.CreateInstanceOf <Vehicle>(r, creatorSettings); DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); ctx.AddObject("InheritanceTests_Cars", car); ctx.AddRelatedObject(car, "BaseTypeNavigationProperty", vehicle); ctx.SaveChangesAsync().Wait(); ctx = ReaderClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); var cars = ctx.CreateQuery <Car>("InheritanceTests_Cars"); var actual = cars.ExecuteAsync().Result.First(); ctx.LoadPropertyAsync(actual, "BaseTypeNavigationProperty").Wait(); AssertExtension.PrimitiveEqual(vehicle, actual.BaseTypeNavigationProperty[0]); ctx = WriterClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); ctx.AttachTo("InheritanceTests_Cars", actual); ctx.AttachTo("InheritanceTests_Vehicles", actual.BaseTypeNavigationProperty[0]); ctx.DeleteLink(actual, "BaseTypeNavigationProperty", actual.BaseTypeNavigationProperty[0]); ctx.SaveChangesAsync().Wait(); ctx = ReaderClient(new Uri(this.BaseAddress), ODataProtocolVersion.V4); cars = ctx.CreateQuery <Car>("InheritanceTests_Cars"); actual = cars.ExecuteAsync().Result.First(); ctx.LoadPropertyAsync(actual, "BaseTypeNavigationProperty").Wait(); Assert.Empty(actual.BaseTypeNavigationProperty); this.ClearRepository("InheritanceTests_Cars"); }
[TestMethod] //Very crude test to check that there is an approximately even distribution of 0, 1 results public void RandomSeedFromClock() { IRandomSeedGenerator seeder = new RandomSeedGenerator(); IRandom random = seeder.Random; int zeros = 0; int ones = 0; for (int i = 0; i < 1000; i++) { random = random.Next(); if (random.ValueInRange(0, 2) == 0) { zeros += 1; } else { ones += 1; } } Assert.AreEqual(1000, zeros + ones); Assert.IsTrue(zeros < 550); Assert.IsTrue(ones < 550); }
protected override void Initialize(CancellationToken cancellationToken) { // Set up the algorithm if (SetSeedRandomly) { Seed = RandomSeedGenerator.GetSeed(); } pyramid = new List <Population>(); seen.Clear(); random.Reset(Seed); tracker = new EvaluationTracker(Problem, MaximumEvaluations); // Set up the results display Results.Add(new Result("Iterations", new IntValue(0))); Results.Add(new Result("Evaluations", new IntValue(0))); Results.Add(new Result("Best Solution", new BinaryVector(tracker.BestSolution))); Results.Add(new Result("Best Quality", new DoubleValue(tracker.BestQuality))); Results.Add(new Result("Evaluation Best Solution Was Found", new IntValue(tracker.BestFoundOnEvaluation))); var table = new DataTable("Qualities"); table.Rows.Add(new DataRow("Best Quality")); var iterationRows = new DataRow("Iteration Quality"); iterationRows.VisualProperties.LineStyle = DataRowVisualProperties.DataRowLineStyle.Dot; table.Rows.Add(iterationRows); Results.Add(new Result("Qualities", table)); table = new DataTable("Pyramid Levels"); table.Rows.Add(new DataRow("Levels")); Results.Add(new Result("Pyramid Levels", table)); table = new DataTable("Stored Solutions"); table.Rows.Add(new DataRow("Solutions")); Results.Add(new Result("Stored Solutions", table)); base.Initialize(cancellationToken); }
public void RunQueryableOnAllPossibleTypes(Type type, string queryString) { int seed = RandomSeedGenerator.GetRandomSeed(); Random r = new Random(seed); Type generic = typeof(IEnumerable <>); var collectionType = generic.MakeGenericType(type); Type listType = typeof(List <>).MakeGenericType(type); var array = Activator.CreateInstance(listType); EnableQueryAttribute q = new EnableQueryAttribute(); var configuration = new HttpConfiguration(); configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; configuration.Routes.MapHttpRoute("ApiDefault", "api/{controller}/{id}", new { id = RouteParameter.Optional }); var request = new HttpRequestMessage(HttpMethod.Get, "http://test/api/Objects?" + queryString); request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, configuration); var controllerContext = new HttpControllerContext( configuration, configuration.Routes.GetRouteData(request), request); var actionContext = new HttpActionContext(controllerContext, new ReflectedHttpActionDescriptor() { Configuration = configuration }); var context = new HttpActionExecutedContext(actionContext, null); context.Response = new HttpResponseMessage(System.Net.HttpStatusCode.OK); context.Response.Content = new ObjectContent(collectionType, array, new JsonMediaTypeFormatter()); q.OnActionExecuted(context); Console.WriteLine(context.Response.Content.ReadAsStringAsync().Result); Assert.Equal(HttpStatusCode.OK, context.Response.StatusCode); }
public void Start(CancellationToken cancellationToken) { lock (locker) { if (startPending) { return; } startPending = true; } try { if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused)) { throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState)); } seed = RandomSeedGenerator.GetSeed(); if (Algorithm == null) { return; } //create cloned algorithms if (clonedAlgorithms.Count == 0) { int testSamplesCount = (SamplesEnd.Value - SamplesStart.Value) / Folds.Value; IDataset shuffledDataset = null; for (int i = 0; i < Folds.Value; i++) { var cloner = new Cloner(); if (ShuffleSamples.Value) { var random = new FastRandom(seed); var dataAnalysisProblem = (IDataAnalysisProblem)algorithm.Problem; var dataset = (Dataset)dataAnalysisProblem.ProblemData.Dataset; shuffledDataset = shuffledDataset ?? dataset.Shuffle(random); cloner.RegisterClonedObject(dataset, shuffledDataset); } IAlgorithm clonedAlgorithm = cloner.Clone(Algorithm); clonedAlgorithm.Name = algorithm.Name + " Fold " + i; IDataAnalysisProblem problem = clonedAlgorithm.Problem as IDataAnalysisProblem; ISymbolicDataAnalysisProblem symbolicProblem = problem as ISymbolicDataAnalysisProblem; int testStart = (i * testSamplesCount) + SamplesStart.Value; int testEnd = (i + 1) == Folds.Value ? SamplesEnd.Value : (i + 1) * testSamplesCount + SamplesStart.Value; problem.ProblemData.TrainingPartition.Start = SamplesStart.Value; problem.ProblemData.TrainingPartition.End = SamplesEnd.Value; problem.ProblemData.TestPartition.Start = testStart; problem.ProblemData.TestPartition.End = testEnd; DataAnalysisProblemData problemData = problem.ProblemData as DataAnalysisProblemData; if (problemData != null) { problemData.TrainingPartitionParameter.Hidden = false; problemData.TestPartitionParameter.Hidden = false; } if (symbolicProblem != null) { symbolicProblem.FitnessCalculationPartition.Start = SamplesStart.Value; symbolicProblem.FitnessCalculationPartition.End = SamplesEnd.Value; } clonedAlgorithm.Prepare(); clonedAlgorithms.Add(clonedAlgorithm); } } OnStarted(); } finally { if (startPending) { startPending = false; } } availableWorkers = new SemaphoreSlim(NumberOfWorkers.Value, NumberOfWorkers.Value); allAlgorithmsFinished = new ManualResetEventSlim(false); var startedTasks = new List <Task>(clonedAlgorithms.Count); //start prepared or paused cloned algorithms foreach (IAlgorithm clonedAlgorithm in clonedAlgorithms) { if (pausePending || stopPending || ExecutionState != ExecutionState.Started) { break; } if (clonedAlgorithm.ExecutionState == ExecutionState.Prepared || clonedAlgorithm.ExecutionState == ExecutionState.Paused) { availableWorkers.Wait(); lock (locker) { if (pausePending || stopPending || ExecutionState != ExecutionState.Started) { break; } var task = clonedAlgorithm.StartAsync(cancellationToken); startedTasks.Add(task); } } } allAlgorithmsFinished.Wait(); Task.WaitAll(startedTasks.ToArray()); // to get exceptions not handled within the tasks }
public void TestApplyPatchOnIndividualProperty() { // clear respository this.ClearRepository("DeltaTests_Todoes"); this.Client.GetStringAsync(this.BaseAddress + "/$metadata").Wait(); Random r = new Random(RandomSeedGenerator.GetRandomSeed()); var s = new CreatorSettings() { NullValueProbability = 0.0, MaxArrayLength = 100 }; // post new entity to repository var todo = InstanceCreator.CreateInstanceOf <DeltaTests_TodoClient>(r, s); todo.NullableBool = true; todo.NullableInt = 100000; todo.Enum = "One"; todo.Estimation = new DeltaTests_Estimation() { CompletedBy = new DateTime(2012, 10, 18), EstimatedTime = TimeSpan.FromDays(1) }; todo.XElement = @"<a><b/></a>"; DataServiceContext ctx = WriterClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.ResolveName = ResolveName; ctx.ResolveType = ResolveType; ctx.AddObject("DeltaTests_Todoes", todo); ctx.SaveChanges(); int id = todo.ID; todo.ID = InstanceCreator.CreateInstanceOf <int>(r, s); todo.Name = InstanceCreator.CreateInstanceOf <string>(r, s); todo.Enum = "Two"; todo.NullableBool = null; todo.Items = InstanceCreator.CreateInstanceOf <DeltaTests_TodoItems>(r, s); todo.Tags = InstanceCreator.CreateInstanceOf <List <DeltaTests_TodoTag> >(r, s); todo.Estimation.CompletedBy = new DateTime(2012, 11, 18); todo.NullableInt = 999999; todo.Bool = InstanceCreator.CreateInstanceOf <bool>(r, s); todo.Byte = InstanceCreator.CreateInstanceOf <Byte>(r, s); todo.ByteArray = InstanceCreator.CreateInstanceOf <byte[]>(r, s); todo.DateTime = InstanceCreator.CreateInstanceOf <DateTime>(r, s); todo.DateTimeOffset = InstanceCreator.CreateInstanceOf <DateTimeOffset>(r, s); todo.Decimal = InstanceCreator.CreateInstanceOf <Decimal>(r, s); todo.Double = InstanceCreator.CreateInstanceOf <Double>(r, s); todo.Float = InstanceCreator.CreateInstanceOf <float>(r, s); todo.Guid = InstanceCreator.CreateInstanceOf <Guid>(r, s); todo.Integer = InstanceCreator.CreateInstanceOf <Int32>(r, s); todo.Long = InstanceCreator.CreateInstanceOf <long>(r, s); todo.Short = InstanceCreator.CreateInstanceOf <short>(r, s); todo.String = InstanceCreator.CreateInstanceOf <string>(r, s); todo.TimeSpan = InstanceCreator.CreateInstanceOf <TimeSpan>(r, s); todo.XElement = @"<b><a/></b>"; ctx.UpdateObject(todo); ctx.SaveChanges(); ctx = ReaderClient(new Uri(this.BaseAddress), DataServiceProtocolVersion.V3); ctx.ResolveName = ResolveName; ctx.ResolveType = ResolveType; var actual = ctx.CreateQuery <DeltaTests_TodoClient>("DeltaTests_Todoes").Where(t => t.ID == id).First(); //Assert.Equal(id, actual.ID); Assert.Equal(todo.Name, actual.Name); Assert.Equal(todo.Estimation.CompletedBy, actual.Estimation.CompletedBy); Assert.Equal(todo.Estimation.EstimatedTime, actual.Estimation.EstimatedTime); Assert.Equal(todo.NullableBool, actual.NullableBool); Assert.Equal(todo.NullableInt, actual.NullableInt); Assert.Equal(todo.Bool, actual.Bool); Assert.Equal(todo.Byte, actual.Byte); Assert.Equal(todo.ByteArray, actual.ByteArray); Assert.Equal(todo.DateTime, actual.DateTime); Assert.Equal(todo.DateTimeOffset, actual.DateTimeOffset); Assert.Equal(todo.Decimal, actual.Decimal); Assert.Equal(todo.Double, actual.Double); Assert.Equal(todo.Float, actual.Float); Assert.Equal(todo.Guid, actual.Guid); Assert.Equal(todo.Integer, actual.Integer); Assert.Equal(todo.Long, actual.Long); Assert.Equal(todo.Short, actual.Short); Assert.Equal(todo.String, actual.String); Assert.Equal(todo.TimeSpan, actual.TimeSpan); Assert.Equal(todo.XElement, actual.XElement.Replace(" ", string.Empty).Replace(Environment.NewLine, string.Empty)); // clear respository this.ClearRepository("DeltaTests_Todoes"); }
protected override void Run(CancellationToken cancellationToken) { // Set up the algorithm if (SetSeedRandomly) { Seed = RandomSeedGenerator.GetSeed(); } var rand = new MersenneTwister((uint)Seed); // Set up the results display var iterations = new IntValue(0); Results.Add(new Result("Iterations", iterations)); var table = new DataTable("Qualities"); table.Rows.Add(new DataRow("R² (train)")); table.Rows.Add(new DataRow("R² (test)")); Results.Add(new Result("Qualities", table)); var curLoss = new DoubleValue(); var curTestLoss = new DoubleValue(); Results.Add(new Result("R² (train)", curLoss)); Results.Add(new Result("R² (test)", curTestLoss)); var runCollection = new RunCollection(); if (StoreRuns) { Results.Add(new Result("Runs", runCollection)); } // init var problemData = Problem.ProblemData; var targetVarName = problemData.TargetVariable; var activeVariables = problemData.AllowedInputVariables.Concat(new string[] { problemData.TargetVariable }); var modifiableDataset = new ModifiableDataset( activeVariables, activeVariables.Select(v => problemData.Dataset.GetDoubleValues(v).ToList())); var trainingRows = problemData.TrainingIndices; var testRows = problemData.TestIndices; var yPred = new double[trainingRows.Count()]; var yPredTest = new double[testRows.Count()]; var y = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).ToArray(); var curY = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).ToArray(); var yTest = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TestIndices).ToArray(); var curYTest = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TestIndices).ToArray(); var nu = Nu; var mVars = (int)Math.Ceiling(M * problemData.AllowedInputVariables.Count()); var rRows = (int)Math.Ceiling(R * problemData.TrainingIndices.Count()); var alg = RegressionAlgorithm; List <IRegressionModel> models = new List <IRegressionModel>(); try { // Loop until iteration limit reached or canceled. for (int i = 0; i < Iterations; i++) { cancellationToken.ThrowIfCancellationRequested(); modifiableDataset.RemoveVariable(targetVarName); modifiableDataset.AddVariable(targetVarName, curY.Concat(curYTest).ToList()); SampleTrainingData(rand, modifiableDataset, rRows, problemData.Dataset, curY, problemData.TargetVariable, problemData.TrainingIndices); // all training indices from the original problem data are allowed var modifiableProblemData = new RegressionProblemData(modifiableDataset, problemData.AllowedInputVariables.SampleRandomWithoutRepetition(rand, mVars), problemData.TargetVariable); modifiableProblemData.TrainingPartition.Start = 0; modifiableProblemData.TrainingPartition.End = rRows; modifiableProblemData.TestPartition.Start = problemData.TestPartition.Start; modifiableProblemData.TestPartition.End = problemData.TestPartition.End; if (!TrySetProblemData(alg, modifiableProblemData)) { throw new NotSupportedException("The algorithm cannot be used with GBM."); } IRegressionModel model; IRun run; // try to find a model. The algorithm might fail to produce a model. In this case we just retry until the iterations are exhausted if (TryExecute(alg, rand.Next(), RegressionAlgorithmResult, out model, out run)) { int row = 0; // update predictions for training and test // update new targets (in the case of squared error loss we simply use negative residuals) foreach (var pred in model.GetEstimatedValues(problemData.Dataset, trainingRows)) { yPred[row] = yPred[row] + nu * pred; curY[row] = y[row] - yPred[row]; row++; } row = 0; foreach (var pred in model.GetEstimatedValues(problemData.Dataset, testRows)) { yPredTest[row] = yPredTest[row] + nu * pred; curYTest[row] = yTest[row] - yPredTest[row]; row++; } // determine quality OnlineCalculatorError error; var trainR = OnlinePearsonsRCalculator.Calculate(yPred, y, out error); var testR = OnlinePearsonsRCalculator.Calculate(yPredTest, yTest, out error); // iteration results curLoss.Value = error == OnlineCalculatorError.None ? trainR * trainR : 0.0; curTestLoss.Value = error == OnlineCalculatorError.None ? testR * testR : 0.0; models.Add(model); } if (StoreRuns) { runCollection.Add(run); } table.Rows["R² (train)"].Values.Add(curLoss.Value); table.Rows["R² (test)"].Values.Add(curTestLoss.Value); iterations.Value = i + 1; } // produce solution if (CreateSolution) { // when all our models are symbolic models we can easily combine them to a single model if (models.All(m => m is ISymbolicRegressionModel)) { Results.Add(new Result("Solution", CreateSymbolicSolution(models, Nu, (IRegressionProblemData)problemData.Clone()))); } // just produce an ensemble solution for now (TODO: correct scaling or linear regression for ensemble model weights) var ensembleSolution = CreateEnsembleSolution(models, (IRegressionProblemData)problemData.Clone()); Results.Add(new Result("EnsembleSolution", ensembleSolution)); } } finally { // reset everything alg.Prepare(true); } }