Exemple #1
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
}
Exemple #2
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
}
Exemple #3
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
}
Exemple #4
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
}