Example #1
0
func TestSolutionCompute(t *testing.T) {
	config, _ := config.New("fixtures/002_020.json")
	system, _ := system.New(&config.System)
	uncertainty, _ := uncertainty.NewEpistemic(system, &config.Uncertainty)

	quantity, _ := quantity.New(system, uncertainty, &config.Quantity)
	ni, no := quantity.Dimensions()

	solution, _ := New(ni, no, &config.Solution)
	surrogate := solution.Compute(quantity, quantity)

	nn := surrogate.Surrogate.Nodes

	assert.Equal(nn, uint(841), t)

	grid := grid.NewClosed(ni)
	nodes := grid.Compute(surrogate.Surrogate.Indices)

	values := make([]float64, nn*no)
	for i := uint(0); i < nn; i++ {
		quantity.Compute(nodes[i*ni:(i+1)*ni], values[i*no:(i+1)*no])
	}

	assert.EqualWithin(values, solution.Evaluate(surrogate, nodes), 1e-15, t)
}
Example #2
0
func function(config *config.Config) error {
	config.Uncertainty.Variance = 1.0

	if len(*sampleSeed) > 0 {
		if number, err := strconv.ParseInt(*sampleSeed, 0, 64); err != nil {
			return err
		} else {
			config.Assessment.Seed = number
		}
	}
	if len(*sampleCount) > 0 {
		if number, err := strconv.ParseUint(*sampleCount, 0, 64); err != nil {
			return err
		} else {
			config.Assessment.Samples = uint(number)
		}
	}

	if config.Assessment.Samples == 0 {
		return errors.New("the number of samples should be positive")
	}

	output, err := database.Create(*outputFile)
	if err != nil {
		return err
	}
	defer output.Close()

	system, err := system.New(&config.System)
	if err != nil {
		return err
	}

	auncertainty, err := uncertainty.NewAleatory(system, &config.Uncertainty)
	if err != nil {
		return err
	}
	aquantity, err := quantity.New(system, auncertainty, &config.Quantity)
	if err != nil {
		return err
	}

	ni, no := aquantity.Dimensions()
	ns := config.Assessment.Samples

	points := support.Generate(ni, ns, config.Assessment.Seed)

	log.Printf("Evaluating the original model at %d points...\n", ns)
	values := quantity.Invoke(aquantity, points)

	if err := output.Put("points", points, ni, ns); err != nil {
		return err
	}
	if err := output.Put("values", values, no, ns); err != nil {
		return err
	}

	return nil
}
Example #3
0
func TestNewEpistemic(t *testing.T) {
	const (
		nt = 10
		σ  = 0.2
	)

	config, _ := config.New("fixtures/001_010.json")
	system, _ := system.New(&config.System)
	reference := system.ReferenceTime()
	uncertainty, _ := NewEpistemic(system, &config.Uncertainty)

	point := make([]float64, nt)
	value := make([]float64, nt)
	for i := 0; i < nt; i++ {
		α := float64(i) / (nt - 1)
		point[i] = α
		value[i] = (1.0 - σ + 2.0*σ*α) * reference[i]
	}

	assert.EqualWithin(uncertainty.Backward(point), value, 1e-15, t)
}
Example #4
0
func TestNewAleatory(t *testing.T) {
	const (
		nt = 10
		σ  = 0.2
	)

	config, _ := config.New("fixtures/001_010.json")
	system, _ := system.New(&config.System)
	uncertainty, _ := NewAleatory(system, &config.Uncertainty)

	point := make([]float64, nt)
	for i := 0; i < nt; i++ {
		point[i] = 0.5
	}

	assert.EqualWithin(uncertainty.Backward(point), []float64{
		3.1402438661763954e-02, 1.7325483399593899e-02,
		2.7071067811865485e-02, 3.1402438661763954e-02,
		4.0065180361560912e-02, 3.2485281374238568e-02,
		1.7325483399593888e-02, 2.5988225099390850e-02,
		1.6242640687119302e-02, 3.2485281374238568e-02,
	}, 1e-15, t)
}
Example #5
0
func function(config *config.Config) error {
	if len(*sampleSeed) > 0 {
		if number, err := strconv.ParseInt(*sampleSeed, 0, 64); err != nil {
			return err
		} else {
			config.Assessment.Seed = number
		}
	}
	if len(*sampleCount) > 0 {
		if number, err := strconv.ParseUint(*sampleCount, 0, 64); err != nil {
			return err
		} else {
			config.Assessment.Samples = uint(number)
		}
	}

	if config.Assessment.Samples == 0 {
		return errors.New("the number of samples should be positive")
	}

	approximate, err := database.Open(*approximateFile)
	if err != nil {
		return err
	}
	defer approximate.Close()

	output, err := database.Create(*outputFile)
	if err != nil {
		return err
	}
	defer output.Close()

	system, err := system.New(&config.System)
	if err != nil {
		return err
	}

	auncertainty, err := uncertainty.NewAleatory(system, &config.Uncertainty)
	if err != nil {
		return err
	}
	aquantity, err := quantity.New(system, auncertainty, &config.Quantity)
	if err != nil {
		return err
	}

	euncertainty, err := uncertainty.NewEpistemic(system, &config.Uncertainty)
	if err != nil {
		return err
	}
	equantity, err := quantity.New(system, euncertainty, &config.Quantity)
	if err != nil {
		return err
	}

	var target, proxy quantity.Quantity
	if config.Solution.Aleatory {
		target, proxy = aquantity, aquantity // noop
	} else {
		target, proxy = equantity, aquantity
	}

	ni, no := target.Dimensions()
	ns := config.Assessment.Samples

	surrogate := new(solution.Surrogate)
	if err = approximate.Get("surrogate", surrogate); err != nil {
		return err
	}

	solution, err := solution.New(ni, no, &config.Solution)
	if err != nil {
		return err
	}

	points := generate(target, proxy, ns, config.Assessment.Seed)

	log.Printf("Evaluating the surrogate model at %d points...\n", ns)
	log.Printf("%5s %15s\n", "Step", "Nodes")

	nk := uint(len(surrogate.Active))

	cumsum := append([]uint(nil), surrogate.Active...)
	for i := uint(1); i < nk; i++ {
		cumsum[i] += cumsum[i-1]
	}
	indices := choose.UniformUint(cumsum, maxSteps)

	nk = uint(len(indices))

	active := make([]uint, nk)
	for i := uint(0); i < nk; i++ {
		active[i] = cumsum[indices[i]]
	}

	values := make([]float64, 0, ns*no)
	for i := uint(0); i < nk; i++ {
		log.Printf("%5d %15d\n", i, active[i])

		s := *surrogate
		s.Nodes = active[i]
		s.Indices = s.Indices[:active[i]*ni]
		s.Surpluses = s.Surpluses[:active[i]*no]

		if !solution.Validate(&s) {
			panic("something went wrong")
		}

		values = append(values, solution.Evaluate(&s, points)...)
	}

	if err := output.Put("surrogate", *surrogate); err != nil {
		return err
	}
	if err := output.Put("points", points, ni, ns); err != nil {
		return err
	}
	if err := output.Put("values", values, no, ns, nk); err != nil {
		return err
	}
	if err := output.Put("active", active); err != nil {
		return err
	}

	return nil
}
Example #6
0
func function(config *config.Config) error {
	output, err := database.Create(*outputFile)
	if err != nil {
		return err
	}
	defer output.Close()

	system, err := system.New(&config.System)
	if err != nil {
		return err
	}

	var anuncertainty uncertainty.Uncertainty
	if config.Solution.Aleatory {
		anuncertainty, err = uncertainty.NewAleatory(system, &config.Uncertainty)
	} else {
		anuncertainty, err = uncertainty.NewEpistemic(system, &config.Uncertainty)
	}
	if err != nil {
		return err
	}

	aquantity, err := quantity.New(system, anuncertainty, &config.Quantity)
	if err != nil {
		return err
	}

	ni, no := aquantity.Dimensions()

	index, err := detect(ni, *parameterIndex)
	if err != nil {
		return err
	}

	points, err := generate(ni, *nodeCount, config.Solution.Rule, index)
	if err != nil {
		return err
	}

	np := uint(len(points)) / ni

	log.Println(system)
	log.Println(aquantity)

	var values []float64
	if len(*approximateFile) > 0 {
		approximate, err := database.Open(*approximateFile)
		if err != nil {
			return err
		}
		defer approximate.Close()

		asolution, err := solution.New(ni, no, &config.Solution)
		if err != nil {
			return err
		}

		surrogate := new(solution.Surrogate)
		if err = approximate.Get("surrogate", surrogate); err != nil {
			return err
		}

		log.Printf("Evaluating the approximation at %d points...\n", np)
		values = asolution.Evaluate(surrogate, points)
	} else {
		log.Printf("Evaluating the original model at %d points...\n", np)
		values = quantity.Invoke(aquantity, points)
	}

	if err := output.Put("values", values, no, np); err != nil {
		return err
	}
	if err := output.Put("points", points, ni, np); err != nil {
		return err
	}

	return nil
}
Example #7
0
func function(config *config.Config) error {
	output, err := database.Create(*outputFile)
	if err != nil {
		return err
	}
	defer output.Close()

	system, err := system.New(&config.System)
	if err != nil {
		return err
	}

	auncertainty, err := uncertainty.NewAleatory(system, &config.Uncertainty)
	if err != nil {
		return err
	}
	aquantity, err := quantity.New(system, auncertainty, &config.Quantity)
	if err != nil {
		return err
	}

	euncertainty, err := uncertainty.NewEpistemic(system, &config.Uncertainty)
	if err != nil {
		return err
	}
	equantity, err := quantity.New(system, euncertainty, &config.Quantity)
	if err != nil {
		return err
	}

	var target, reference quantity.Quantity
	if config.Solution.Aleatory {
		target, reference = aquantity, equantity
	} else {
		target, reference = equantity, aquantity
	}

	ni, no := target.Dimensions()

	solution, err := solution.New(ni, no, &config.Solution)
	if err != nil {
		return err
	}

	log.Println("System", system)
	log.Println("Quantity", target)

	if config.Solution.Aleatory {
		log.Println("Constructing an aleatory surrogate...")
	} else {
		log.Println("Constructing an epistemic surrogate...")
	}

	surrogate := solution.Compute(target, reference)

	log.Println("Surrogate", surrogate)

	if err := output.Put("surrogate", *surrogate); err != nil {
		return err
	}

	return nil
}