Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 5
0
func function(_ *config.Config) error {
	reference, err := database.Open(*referenceFile)
	if err != nil {
		return err
	}
	defer reference.Close()

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

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

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

	rvalues := []float64{}
	if err := reference.Get("values", &rvalues); err != nil {
		return err
	}

	ovalues := []float64{}
	if err := observe.Get("values", &ovalues); err != nil {
		return err
	}

	pvalues := []float64{}
	if err := predict.Get("values", &pvalues); err != nil {
		return err
	}

	active := []uint{}
	if err := predict.Get("active", &active); err != nil {
		return err
	}

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

	no := surrogate.Outputs
	nq := no / momentCount
	nk := uint(len(active))

	if ne := active[nk-1]; uint(len(ovalues))/no < ne {
		return errors.New(fmt.Sprintf("the number of observations should be at least %d", ne))
	}

	εo := make([]float64, 0, nq*nk*metricCount)
	εp := make([]float64, 0, nq*nk*metricCount)

	for i := uint(0); i < nq; i++ {
		r := slice(rvalues, no, i*momentCount, 1)

		o := cumulate(slice(ovalues, no, i*momentCount, 1), active)
		for j := uint(0); j < nk; j++ {
			εo = append(εo, assess(r, o[j])...)
		}

		p := divide(slice(pvalues, no, i*momentCount, 1), nk)
		for j := uint(0); j < nk; j++ {
			εp = append(εp, assess(r, p[j])...)
		}
	}

	if err := output.Put("active", active); err != nil {
		return err
	}
	if err := output.Put("observe", εo, metricCount, nk, nq); err != nil {
		return err
	}
	if err := output.Put("predict", εp, metricCount, nk, nq); err != nil {
		return err
	}

	return nil
}