Beispiel #1
0
// PostProcess performs a post-processing of the just read json file
func (o *Data) PostProcess(dir, fn string, erasefiles bool) {
	o.FnameDir = os.ExpandEnv(dir)
	o.FnameKey = io.FnKey(fn)
	if o.DirOut == "" {
		o.DirOut = "/tmp/gofem/" + o.FnameKey
	}
	if o.Encoder != "gob" && o.Encoder != "json" {
		o.Encoder = "gob"
	}
	err := os.MkdirAll(o.DirOut, 0777)
	if err != nil {
		chk.Panic("cannot create directory for output results (%s): %v", o.DirOut, err)
	}
	if erasefiles {
		io.RemoveAll(io.Sf("%s/%s_*.vtu", o.DirOut, o.FnameKey))
		io.RemoveAll(io.Sf("%s/%s_*.log", o.DirOut, o.FnameKey))
		io.RemoveAll(io.Sf("%s/%s_*.gob", o.DirOut, o.FnameKey))
		io.RemoveAll(io.Sf("%s/%s_*.json", o.DirOut, o.FnameKey))
	}
}
Beispiel #2
0
// RunMany runs many trials in order to produce statistical data
func (o *Optimiser) RunMany(dirout, fnkey string) {

	// benchmark
	t0 := time.Now()
	defer func() {
		o.SysTimeTot = time.Now().Sub(t0)
		var tmp int64
		for _, dur := range o.SysTimes {
			tmp += dur.Nanoseconds()
		}
		tmp /= int64(o.Nsamples)
		o.SysTimeAve = time.Duration(tmp)
	}()

	// disable verbose flag temporarily
	if o.Verbose {
		defer func() {
			o.Verbose = true
		}()
		o.Verbose = false
	}

	// remove previous results
	if fnkey != "" {
		io.RemoveAll(dirout + "/" + fnkey + "-*.res")
	}

	// allocate variables
	o.SysTimes = make([]time.Duration, o.Nsamples)
	o.BestOvas = make([][]float64, o.Nova)
	o.BestFlts = make([][]float64, o.Nflt)
	o.BestInts = make([][]int, o.Nint)
	o.BestOfBestOva = make([]float64, o.Nova)
	o.BestOfBestFlt = make([]float64, o.Nflt)
	o.BestOfBestInt = make([]int, o.Nint)

	// perform trials
	for itrial := 0; itrial < o.Nsamples; itrial++ {

		// re-generate solutions
		o.Nfeval = 0
		if itrial > 0 {
			o.generate_solutions(itrial)
		}

		// save initial solutions
		if fnkey != "" {
			WriteAllValues(dirout, io.Sf("%s-%04d_ini", fnkey, itrial), o)
		}

		// message
		if o.VerbStat {
			io.Pf(". . . running trial # %d\n", itrial)
		}

		// solve
		timeIni := time.Now()
		o.Solve()
		o.SysTimes[itrial] = time.Now().Sub(timeIni)

		// sort
		if o.Nova > 1 { // multi-objective
			SortByFrontThenOva(o.Solutions, 0)
		} else { // single-objective
			SortByOva(o.Solutions, 0)
		}

		// feasible solution
		if o.Solutions[0].Feasible() {

			// best solution
			best := o.Solutions[0]
			for i := 0; i < o.Nova; i++ {
				o.BestOvas[i] = append(o.BestOvas[i], best.Ova[i])
			}
			for i := 0; i < o.Nflt; i++ {
				o.BestFlts[i] = append(o.BestFlts[i], best.Flt[i])
			}
			for i := 0; i < o.Nint; i++ {
				o.BestInts[i] = append(o.BestInts[i], best.Int[i])
			}

			// best of all trials
			first_best := len(o.BestOvas[0]) == 1
			if first_best {
				copy(o.BestOfBestOva, best.Ova)
				copy(o.BestOfBestFlt, best.Flt)
				copy(o.BestOfBestInt, best.Int)
			} else {
				if best.Ova[0] < o.BestOfBestOva[0] {
					copy(o.BestOfBestOva, best.Ova)
					copy(o.BestOfBestFlt, best.Flt)
					copy(o.BestOfBestInt, best.Int)
				}
			}

			// check multi-objective results
			if o.F1F0_func != nil {
				var rms_err float64
				var nfeasible int
				for _, sol := range o.Solutions {
					if sol.Feasible() && sol.FrontId == 0 {
						f0, f1 := sol.Ova[0], sol.Ova[1]
						f1_cor := o.F1F0_func(f0)
						rms_err += math.Pow(f1-f1_cor, 2.0)
						nfeasible++
					}
				}
				if nfeasible > 0 {
					rms_err = math.Sqrt(rms_err / float64(nfeasible))
					o.F1F0_err = append(o.F1F0_err, rms_err)
				}
			}

			// arc-length along Pareto front
			if o.Nova == 2 {
				if best.Feasible() && best.FrontId == 0 && o.Solutions[1].FrontId == 0 {
					dist := 0.0
					for i := 1; i < o.Nsol; i++ {
						if o.Solutions[i].FrontId == 0 {
							F0, F1 := o.Solutions[i-1].Ova[0], o.Solutions[i-1].Ova[1]
							f0, f1 := o.Solutions[i].Ova[0], o.Solutions[i].Ova[1]
							if o.F1F0_f0ranges != nil {
								a := o.find_f0_spot(F0)
								b := o.find_f0_spot(f0)
								if a == -1 || b == -1 {
									continue
								}
								if a != b {
									//io.Pforan("\nF0=%g is in [%g,%g]\n", F0, o.F1F0_f0ranges[a][0], o.F1F0_f0ranges[a][1])
									//io.Pfpink("f0=%g is in [%g,%g]\n", f0, o.F1F0_f0ranges[b][0], o.F1F0_f0ranges[b][1])
									continue
								}
							}
							dist += math.Sqrt(math.Pow(f0-F0, 2.0) + math.Pow(f1-F1, 2.0))
						}
					}
					o.F1F0_arcLen = append(o.F1F0_arcLen, dist)
				}
			}

			// multiple OVAs
			if o.Nova > 1 && o.Multi_fcnErr != nil {
				var rms_err float64
				var nfeasible int
				for _, sol := range o.Solutions {
					if sol.Feasible() && sol.FrontId == 0 {
						f_err := o.Multi_fcnErr(sol.Ova)
						rms_err += f_err * f_err
						nfeasible++
					}
				}
				if nfeasible > 0 {
					rms_err = math.Sqrt(rms_err / float64(nfeasible))
					o.Multi_err = append(o.Multi_err, rms_err)
				}
			}

			// IGD metric
			if o.Nova > 1 && len(o.Multi_fStar) > 0 {
				o.Multi_IGD = append(o.Multi_IGD, StatIgd(o, o.Multi_fStar))
			}

			// save final solutions
			if fnkey != "" {
				f0min := best.Ova[0]
				for _, sol := range o.Solutions {
					f0min = utl.Min(f0min, sol.Ova[0])
				}
				WriteAllValues(dirout, io.Sf("%s-%04d_f0min=%g", fnkey, itrial, f0min), o)
			}
		}
	}
}