def z_maxent(self, method): ''' actiion for Maximum Entropy button ''' t = tb.tableau() try: t.readString(self.y_input) dcfg = cfg.get_config_as_dict(self.f_fcfg) except tb.InputError as e: tk.messagebox.showerror(title="INPUT ERROR", message=e) except Exception as e: tk.messagebox.showerror(title="ERROR", message=e) self.init_queue() cnt = [0] repgap = max( 1, int((100 if method == 'CG' else 2000) / len(t.constraints))) self.z_abort = False def rep(w): cnt[0] += 1 if cnt[0] % repgap == 0: self.put_queue('Iteration count: %d\n' % (cnt[0])) if self.z_abort: self.put_queue('Aborted.') raise KeyboardInterrupt try: self.queue = queue.Queue() def task(): try: ans, tim = timing(maxent.MaximumEntropy, t, method=method, callback=rep, **dcfg[method]) def toString(a): s = sorted(((t.get_constraint(index=i).abbr, -w) for i, w in a.items()), key=(lambda a: a[1]), reverse=True) return '\n'.join( ('%' + str(max(len(abbr) for abbr, _ in s)) + 's\t%s') % x for x in s) self.put_queue({ 'caller': self.z_maxent, 'value': ans, 'time': tim, 'toString': toString, 'method': method }) except Exception as e: self.put_queue(e) self.y_running = th.Thread(target=task) self.y_running.start() self.process_queue() except Exception as e: tk.messagebox.showerror(title="ERROR", message=e)
def z_fred(self): ''' actiion for Fusional Reduction button ''' self.y_output = {'value': 'computing...'} t = tb.tableau() self.init_queue() try: t.readString(self.y_input) self.y_tab = t def task(): try: ans, tim = timing(fred.FRed, fred.erc.get_ERClist(t)) self.put_queue({ 'caller': self.z_fred, 'value': ans, 'time': tim, }) except Exception as e: self.put_queue(e) self.y_running = th.Thread(target=task) self.y_running.start() self.process_queue() except tb.InputError as e: tk.messagebox.showerror(title="INPUT ERROR", message=e) except Exception as e: tk.messagebox.showerror(title="ERROR", message=e)
def z_cd(self) : ''' actiion for Constraint Demotion button ''' del self.y_output t = tb.tableau() try : t.readString(self.y_input) ans, tim = timing(cd.ConstraintsDemotion, t) self.y_output = {'caller':self.z_cd, 'value':ans, 'time':tim, 'toString':cd.toString} except tb.InputError as e : tk.messagebox.showerror(title="INPUT ERROR", message=e) except Exception as e : tk.messagebox.showerror(title="ERROR", message=e)
def z_tableau(self) : caller = self._y_output['caller'].__name__[2:] method = self._y_output.get('method') aff = caller+'_'+method if method else caller print('in z_tableau: aff="%s"'%(aff)) fhtml = self.getResouceFileName(self.y_datainfo['inputnamebase'], 'tableau_%s.html'%(aff)) f = open(fhtml, 'w') f.write(self.z_HTMLheader()) f.write('<link rel="stylesheet" type="text/css" href="../tableau.css" />') f.write(tb.tableau(string=self.y_input).toHTML(**{caller:self.y_output})) f.close() # th.Thread(target=os.system, args=(fhtml,)).start() wbb.open_new_tab(fhtml)
def z_tableau(self): caller = self._y_output['caller'].__name__[2:] method = self._y_output.get('method') aff = caller + '_' + method if method else caller print('in z_tableau: aff="%s"' % (aff)) fhtml = self.getResouceFileName(self.y_datainfo['inputnamebase'], 'tableau_%s.html' % (aff)) f = open(fhtml, 'w') f.write(self.z_HTMLheader()) f.write( '<link rel="stylesheet" type="text/css" href="../tableau.css" />') f.write( tb.tableau(string=self.y_input).toHTML(**{caller: self.y_output})) f.close() # th.Thread(target=os.system, args=(fhtml,)).start() wbb.open_new_tab(fhtml)
def z_cd(self): ''' actiion for Constraint Demotion button ''' del self.y_output t = tb.tableau() try: t.readString(self.y_input) ans, tim = timing(cd.ConstraintsDemotion, t) self.y_output = { 'caller': self.z_cd, 'value': ans, 'time': tim, 'toString': cd.toString } except tb.InputError as e: tk.messagebox.showerror(title="INPUT ERROR", message=e) except Exception as e: tk.messagebox.showerror(title="ERROR", message=e)
def z_maxent(self, method) : ''' actiion for Maximum Entropy button ''' t = tb.tableau() try : t.readString(self.y_input) dcfg = cfg.get_config_as_dict(self.f_fcfg) except tb.InputError as e : tk.messagebox.showerror(title="INPUT ERROR", message=e) except Exception as e : tk.messagebox.showerror(title="ERROR", message=e) self.init_queue() cnt = [0] repgap = max(1, int((100 if method=='CG' else 2000 ) / len(t.constraints))) self.z_abort = False def rep(w) : cnt[0] += 1 if cnt[0] % repgap == 0 : self.put_queue('Iteration count: %d\n'%(cnt[0])) if self.z_abort : self.put_queue('Aborted.') raise KeyboardInterrupt try : self.queue = queue.Queue() def task() : try : ans, tim = timing(maxent.MaximumEntropy, t, method=method, callback=rep, **dcfg[method]) def toString(a) : s = sorted( ((t.get_constraint(index=i).abbr, -w) for i, w in a.items()), key=(lambda a:a[1]), reverse=True) return '\n'.join(('%'+str(max(len(abbr) for abbr, _ in s))+'s\t%s')%x for x in s) self.put_queue({ 'caller':self.z_maxent, 'value':ans, 'time':tim, 'toString':toString, 'method':method}) except Exception as e : self.put_queue(e) self.y_running = th.Thread(target=task) self.y_running.start() self.process_queue() except Exception as e : tk.messagebox.showerror(title="ERROR", message=e)
def z_fred(self) : ''' actiion for Fusional Reduction button ''' self.y_output = {'value':'computing...'} t = tb.tableau() self.init_queue() try : t.readString(self.y_input) self.y_tab = t def task() : try : ans, tim = timing(fred.FRed, fred.erc.get_ERClist(t)) self.put_queue({'caller':self.z_fred, 'value':ans, 'time':tim,}) except Exception as e : self.put_queue(e) self.y_running = th.Thread(target=task) self.y_running.start() self.process_queue() except tb.InputError as e : tk.messagebox.showerror(title="INPUT ERROR", message=e) except Exception as e : tk.messagebox.showerror(title="ERROR", message=e)
def test(fname) : print('-----test for "%s"------'%fname) try : t.readFile(fname) print('read file done:') print(t.get_constraint_indices()) print(*(cand.abbr for cand in t.constraints)) #print(*(d.candidates for d in t.datum)) ## MaxEnt method='SCGIS' ans = maxent.MaximumEntropy(t, method=method, trim0=True, tol=1e-6)#, **cfg[method]) #ans = {0:-233, 1:-88, 2:92} print(*sorted((w, t.get_constraint(index=i).abbr) for i, w in (ans).items()), sep='\n') ### Tableau fhtml='res\\tableau_maxent.html' print(tb.tableau(fname).toHTML(maxent=ans, **cfg['HTML']), file=open(fhtml,'w')) th.Thread(target=os.system, args=(fhtml,)).start() ## FRed # ercs = fred.erc.get_ERClist(t) # print('ERCs:', *ercs, sep='\n') # fredans = fred.FRed(ercs) # print(fredans) ### Hasse Diagram # root, ext = os.path.splitext(fname) # fred.hasse.hasse(t, fredans.SKB).write(root+'.png', format='png') ## CD (last because it will modify the tableau) # rank_stratum = cd.ConstraintsDemotion(t) # print(cd.toString(rank_stratum)) # ans = rank_stratum # ### Tableau # fhtml='res\\tableau_cd.html' # print(tb.tableau(fname).toHTML(cd=ans), file=open(fhtml,'w')) # th.Thread(target=os.system, args=(fhtml,)).start() except tb.InputError as e : print('Error when reading file:', e) except (cd.UnsatisfiableError, fred.UnsatisfiableError) as e : print('Error when processing:', e)
# -*- coding: utf-8 -*- import os.path import threading as th import tableau as tb import cd import fred import maxent import cfg cfg = cfg.get_config_as_dict('res\\config.ini') print(cfg) t = tb.tableau() def test(fname) : print('-----test for "%s"------'%fname) try : t.readFile(fname) print('read file done:') print(t.get_constraint_indices()) print(*(cand.abbr for cand in t.constraints)) #print(*(d.candidates for d in t.datum)) ## MaxEnt method='SCGIS' ans = maxent.MaximumEntropy(t, method=method, trim0=True, tol=1e-6)#, **cfg[method]) #ans = {0:-233, 1:-88, 2:92} print(*sorted((w, t.get_constraint(index=i).abbr) for i, w in (ans).items()), sep='\n') ### Tableau
from tableau import tableau from chess_simple import chess_simple from chess_linux import chess_linux from chess_tkinter import chess_tkinter import sys inter_input = "" if len(sys.argv) == 1: #pragma: no cover print("Choose the interface you want to use") print(" - simple : text-based interface") print(" - linux : text based with colors") print(" - windows : windows interface with mouse management") inter_input = input("interface ? (simple, linux, windows) : ") if len(sys.argv) == 2 and sys.argv[1] == "linux" or inter_input == "linux": interface = chess_linux() elif len(sys.argv) == 2 and sys.argv[1] == "simple" or inter_input == "simple": interface = chess_simple() elif len(sys.argv) == 2 and sys.argv[ 1] == "tkinter" or inter_input == "windows": #pragma: no cover interface = chess_tkinter() else: #pragma: no cover print("interface not found") tab = tableau(interface) tab.start()
def solution_steps(self, linear_program): tableau_obj = tableau.tableau(linear_program) return itertools.chain(self._phase1_steps(tableau_obj), self._phase2_steps(tableau_obj))